— tomauger.com

Archive
WordPress

Okay, this has been a holy grail for me for a while, and just came to a head today as I accidentally copied a database containing about 15 separate WordPress installations (with different table prefixes) into what was supposed to be a dedicated single WP database. Ouch.

A little while ago I blogged about how you can avoid this situation altogether by only mysqldumping selected tables based on a pattern match of their prefix. Well, guess who didn’t drink his own milk (is that even an expression)?

So there I was, with a database full of wp_ and wpclientA_ wpclientB_ zg_ etc. All I wanted was to keep the original wp_ tables and just drop the hell out of the rest of ‘em.

Read More

Depending on your web hosting situation, you may find yourself having to share a database among multiple WordPress installations, or perhaps other tables that have nothing to do with WordPress whatsoever. This is usually not a significant issue for your day-to-day because WordPress allows you to prefix your WP tables to be anything you like.

The challenge comes with backup and migration, particularly if you’re doing it manually via the shell command line.

Suppose you have a whole bunch of WP installations on a staging server awaiting review, all sharing a database. Now client XYZ has approved the site and it’s time to go live. We’ll take a mysqldump of just those tables and migrate that to the new server.

mysqldump takes, in addition to the authentication (-u and -p – and possibly -h) and database arguments, can take a list of tables to be included in the export. Once you supply even one table, any table not named is ignored. The problem is, particularly if you use plugins that create their own tables, the fixed list of tables can be a moving target. Can’t we select all tables with a given prefix?

Read More

A real quickie for any Arras theme users out there. Okay, Arras is overly complicated and its actions are just plain convoluted. However, they were clever about implementing filter hooks where you need them to be.

Have you created a custom post type in WordPress and now you want to include it, along with other regular posts, in your home.php’s slideshow and featured articles list? Add this little snippet to your functions.php (or wherever you are defining your filters and actions):

add_filter('arras_slideshow_query', 'add_cpt_to_arras_queries'); 
add_filter('arras_featured_query', 'add_cpt_to_arras_queries'); 
 
function hdb_add_blog_to_arras_queries( $query_string ){ 
  // Arras gives us the query string in the &key=value format 
  parse_str( $query_string, $query_args );
  // but we need it in the Array format so we can add multiple post types 
  $query_args['post_type'] = array( 'post', OPINION_TYPE );
  // arras_render_posts accepts either format so we're good to go! 
  return $query_args; 
}

 

Read More
Flickr Photos Plugin screenshot

A styled version of the Simple Flickr Photos plugin

We recently implemented Onur Kocatas’ Simple Flickr Photos plugin, that displays a nice little widget of your Flickr photo stream. After testing everything on our development box, when we moved to our testing / staging site on a shared hosting environment, we started throwing errors like this lovely piece of work here:

Warning: file_get_contents() [function.file-get-contents.html]: URL file-access is
disabled in the server configuration in
/wp-content/plugins/simple-flickr-plugin.3.0/simpleflickrphotos.php on line 106

After a little digging, it appears that file_get_contents() has a dependency to the allow_url_fopen php.ini setting. If you are throwing this error and you have access to editing your php.ini file, just do a quick search for ‘fopen’ and set the allow_url_fopen to ‘ON’.

For those of us schmucks in shared hosting situations, this will not do. However, WordPress has been providing an alternative to file_get_contents() for some time, called wp_remote_fopen(). This helper function, defined as part of the WP_Http class replaces the lower-level fopen(0 or cUrl() calls that may or may not be supported on a given server. In fact, as of this writing, there are at least 5 different transports that are tested and may be automatically selected by the WP_Http class. This, along with some robust error checking (the methods will return a WP_Error instance which can be checked with is_wp_error()), makes this the only reliable solution for remote gets.

Updating the plugin to work is trivial, as of the version I’m using (billed as 3.0, but internally actually named 2.9.9.7.1 – seriously – how many decimals do you need in a versioning scheme?).

Replace this line on line 106 of simpleflickrphotos.php:

$html = file_get_contents($url);

with this:

$html = wp_remote_fopen($url);

I would have sent a request or a fork to the author to get his plugin updated (he should really be using built-in WordPress functions whenever possible), but have you seen his “plugin support site”? I’m not even going to do him the favour of providing a link here. Let’s just say, if you want a site dedicated to photos from Turkey… well, don’t go there. Just. Don’t go there. Period.

I sympathize with the guy. I mean, who wants to create a separate support site for a free plugin? It’s just begging for more work than you want. Which is why I haven’t released a plugin to the community yet. Ah well. Hope this fix helps someone out. Of course, once you manually make this fix, you have to be careful that the plugin is not updated, otherwise you’ll have to do it all over again, unless the author implements this fix in a future version (2.9.9.7.1.4.23.6.5.7 perhaps?)

Read More

Sometimes you may wish to have a section of your WordPress based site protected for exclusive access by logged-in users with a certain access level (in WordPress terminology: a role). I recently encountered this feature request on a client project, so I thought I’d share my results here, since I had to cobble together the information from a great variety of sources. Much thanks, in particular, to Toscho at the most excellent WordPress StackExchange site for setting me onto custom walkers as the way to customize the way your menu should display.

Since I’ll be limiting this blog post to my actual implementation, rather than a more general discussion of best practices, I should just quickly set up the particular business requirements I was dealing with:

  • I would only be protecting Pages, not Posts
  • The protected pages would only be accessed through a menu in the Footer of the site
  • If the user is logged in and has access to the pages, those pages should appear in the menu
  • If the user doesn’t have access to the pages, the menu item in the footer would be a Login link instead
  • Users that should have access to the pages are:  administrators, editors and a custom role (“members”)

Read More

 

I’m very excited to announce that I have been accepted as a speaker at WordCamp Montreal this July 9-10, 2011!

My talk, tentatively entitled ”WordPress Development Paradigms, Idiosyncracies and Best Practices” will be targeted at existing programmers and developers who are reasonably new to WordPress and want to jump in with both feet. What makes this different from other talks (or tutorials on the web) is that I want to round up all of the major conceptual hurdles that a new WordPress developer need to overcome in order to make best use of everything the framework provides, in a way that will be durable and sustainable as WordPress continues to evolve.

The idea for this talk came from my own experiences as a programmer of 30+ years (oh man, am I that old?) coming to the WordPress platform for the first time and running into roadblocks, confusion and misconceptions. I often find that learning a new platform is not so much about learning the API, objects or method calls, but getting the paradigm into your headspace.

That’s what this talk will be all about. I think it’s going to be a blast! Maybe I’ll see you there?

Read More

This is a continuation from a previous post that looks at custom development based on the GD Star Ratings plugin for WordPress.

You would think this would be the most highly documented feature – how to actually insert the ratings for a particular article right in your theme. Heck, maybe there’s a way to do it from the Admin panel that I haven’t even noticed, but I’m a back end kinda guy anyway. Here’s the full code. This one has been written to work within The Loop, but it’s trivial to modify it to work with a custom loop or query if you wanted:

$postRatingData = wp_gdsr_rating_article(get_the_ID());
gdsr_render_stars_custom(array(
    "max_value" => gdsr_settings_get('stars'),
    "size" => 12,
    "vote" => $postRatingData->rating
));

That’s basically it in a nutshell. Max Value is the “total” number of stars (eg: 5 out of 5), Size must be one of the default sizes ie: 12, 16, 20, 24 etc, Vote is the actual calculated result, which we got by passing the ID of the current post to wp_gdsr_rating_article.

Read More

GDSR, in case you don’t know it, is one of the best, most complete plugins, and APIs, for adding all sorts of  ratings, thumbs and good stuff to your blog. Anyway, check it out.

Of course, like most WP stuff out there, the documentation is a mess (though I must admit this one is far better than most, so props to the dev). As I desperately try to customize the way it works, I’ll post my findings here.

Getting Admin Settings

So you want to write good code and you want to leverage the extensive settings available in the Admin panel. How to access those settings? Well I don’t know if there’s an official way or not, but digging through the many pages of PHP code, I have found

gdsr_settings_get(SETTING_NAME);

This queries the global GDSR object and grabs the setting by name. The trouble is, I haven’t found any documentation for what these settings names are. There are a couple of strategies here:

1. Query the global $gdsr object. This is a real hack, but it worked for me:

global $gdsr;
print_r ($gdsr->o);

While this ain’t pretty, you’ll get a list of every single setting available. Now you have to figure out the one you want.

2. View source on the Admin page. This didn’t work for me exactly – I had been hoping that by going to the Admin page, and inspecting one of the textfields or pull-down menus and grabbing its name or id I could find the corresponding setting. Turns out, there isn’t any direct correlation – for example the setting that controls the # of stars shown on the article page is called “gdsr_stars” where the gdsr_settings_get string is ‘stars’. Hmmm. Come to think of it, maybe they’re all related after all, just remove the gdsr_ prefix. Worth experimenting with.

That’s all the time I have. If even 1 other person finds this useful it was worth it.

Read More

Important Note: I’ve learned a ton about WordPress since this post, and it actually does not follow best practices.

I will blog on this soon (as soon as my current workload customizing WordPress lets up), but in the meantime, please see my PowerPoint presentation from my recent WordCamp Montreal talk on WordPress best practices. The post below is actually an embarassment. Maybe I should just pull it…

This is a quickie – suppose you want to quickly select the latest comments across all posts within a specific category. This mySQL query gets you there:

select C.*
from wp_comments C
join wp_posts P
	on C.comment_post_ID = P.ID
	and P.post_status = 'publish'
	and P.post_type = 'post'
 
/* join posts to category */
join wp_term_relationships TR
	on TR.object_id = P.ID
 
join wp_term_taxonomy TT
	on TT.term_taxonomy_id = TR.term_taxonomy_id
	and TT.taxonomy = 'category'
	and TT.term_id = [CATEGORY ID]
 
where C.comment_approved = 1
 
order by comment_date_gmt desc
limit 1;

Just replace the [CATEGORY ID] with your $catID and you can change the limit if you want more than just one most recent comment. Limit 5 will give you the 5 most recent comments that you can then loop through and display.

Read More