Quick And Easy WordPress Tricks
While WordPress already offers its users a lot of customization options that allow them to fine-tune their sites and enhance user experience, there are still some other things that its user base wishes its built-in features is capable of. That’s where hacks or coding tricks come into play such as the ones that we’re going to show you in today’s post.
Below are a few of my favorite simple, yet clever, WordPress tricks that you can perform not quite easily but also quickly in a matter of seconds on your own site. All it takes of course is simply adding a few lines of codes into your functions.php file.
Human-readable Time Display – Performing this trick will change the way WordPress displays the time-and-date stamp on your posts and instead will show it in human-readable format, like “5 minutes ago” or “one month ago”. This is done by using the human_timed_diff() function.
$diff = human_time_diff( '2012-05-05 12:05:00', '2012-05-05 12:10:00' ); echo 'This comment was submitted ' . $diff . 'ago'; // Output: This comment was submitted 5 minutes ago
List all hooked WordPress functions – Listing all hooked WordPress functions can be very useful in case something goes wrong if/when you’re using WordPress hooks to “surcharge” any existing WP function with that of your own code.
function list_hooked_functions($tag=false){ global $wp_filter; if ($tag) { $hook[$tag]=$wp_filter[$tag]; if (!is_array($hook[$tag])) { trigger_error("Nothing found for '$tag' hook", E_USER_WARNING); return; } } else { $hook=$wp_filter; ksort($hook); } echo '<pre>'; foreach($hook as $tag => $priority){ echo "<br />>>>>>\t<strong>$tag</strong><br />"; ksort($priority); foreach($priority as $priority => $function){ echo $priority; foreach($function as $name => $properties) { echo "\t$name<br />"; } } } echo '</pre>'; return; }
Used without an argument, you’ll get a list of all hooked functions. This’ll be a bit long, so you can specify a hook to narrow the list. This can be useful when debugging or fiddling with hook priorities.
Enable shortcodes in text widgets – This trick makes text widgets much better, particularly for theme developers, in that it makes your product more flexible for the user.
add_filter( 'widget_text', 'do_shortcode' );
Don’t break WordPress loops – While using multiple loops can be great, it can also cause major problems if not used correctly/properly. To help ensure that your loop runs smoothly and you can still use all of the functions that rely on globals, store the original query in a temporary variable.
$tmp_query = $wp_query; query_posts('cat=5&order=ASC'); while( have_posts() ) : the_post() ?> <a href="<?php the_permalink() ?>'><?php the_title() ?></a><br /> <?php $wp_query = $tmp_query;