Sivel.net  Throwing Hot Coals


WordPress One Liner to Customize Author Permalink Redeux

Nearly 2 years ago I wrote a one liner for someone in the WordPress IRC channel to change the author permalink structure. At the time I had not taken the time to really understand WP_Rewrite and as such didn’t understand the implications of flushing the rewrite rules on each page load.

It is sufficient to say that since then, I have taken the time to understand it better and I am fully aware of the negative implications of performing flushes every time someone hits your site. For one thing the default behavior of flush_rules() is to also update the .htaccess file as well as updating the serialized array in your wp_options table that contain the internal WordPress rewrites. Assuming you are using a nasty permalink structure that starts with something like %category% or %postname% that serialized array can grow exponentially with the number of pages you have 1.

To make a long story sort, I have known that I should have changed the code for almost as long as the post has been published, but I was too lazy to do anything about it. It took a few carefully placed pokes and prods to get me moving, and as such I have updated the post to reflect the removal of using flush_rules().

Notes:

  1. Otto explained this a bit more in depth at http://ottopress.com/2010/category-in-permalinks-considered-harmful/ #
Code One Liner PHP Snippet WordPress

WordPress One Liner to Remove Style Tags from Posts using [gallery] Shortcode

One of the biggest things that irritates me when using the [gallery] shortcode is that style tags are inserted into the post content. Why does this irritate me? Because this does not validate as XHTML 1.0. Luckily the fix is quite easy. Add the following code as a plugin or to your themes functions.php and say goodbye to the style tags in your post content.

add_filter('gallery_style', create_function('$a', 'return preg_replace("%%s", "", $a);'));

One side effect to doing this is that the CSS normally outputted to into your post content is no longer output at all. You will need to add the CSS to your themes css file, usually style.css. The following css is the default output:

The width for .gallery-item is a dynamic value determined by the columns attribute specified in the [gallery] shortcode. The default is 3 columns which makes the width 33%, floor(100/$columns).

I have also written a plugin called Gallery Shortcode Style to Head which does a forward lookup to determine if a post uses the [gallery] shortcode and if it does, prints the default CSS to the head. While the plugin works and performs its function, I still prefer a solution that has less overhead associated with it, hence the code provided in this post.

Code One Liner PHP Snippet WordPress

WordPress One Liner to Customize Author Permalink

Several people have asked me recently how to customize the author permalink from being /author/admin to something like /profile/admin. I have created a simple one line piece of code that you can drop in your themes functions.php to achieve this.

add_filter('init', create_function('$a', 'global $wp_rewrite; $wp_rewrite->author_base = "profile";'));

Drop this into your themes functions.php wrapped in <?php ?> tags, then visit the WordPress admin, go to Settings->Permalinks then off you go. By visiting the permalinks settings page it should flush the rewrite rules, but if for some reason it doesn’t go ahead and click “Save Changes”. If you want /author/ to become something other than /profile/ replace ‘profile’ in that one liner with the string of your choice.

Code One Liner PHP Snippet WordPress