WordPress Tips I Wish Someone Had Told Me

Over the past few years, I have had the opportunity to work on several internal and external WordPress-based R/GA websites. Here are some things I wish I knew when I started.

WordPress Hooks

The backbone  for WordPress’ vast extensibility is the hooks API. A hook is a call to an external function at specific points during WordPress runtime. In WordPress there are two types of hooks – Action hooks and Filter hooks. The actions are used for executing functions, and the filters are used to manipulate the output of the code.

If you need to make customizations to WordPress you should NEVER edit core files. They will be overwritten the next time you upgrade to the latest version of WordPress. In addition, editing core files will make it more difficult for other developers to fix issues and could cause plugins to break.

To run your code without editing core files you need to detect the hook name you want to use. To do that, search the section(s) of the code where you want to make additions for these functions:
do_action( $tag, $arg ); or apply_filters( $tag, $value, $var … );

Add your function to the hook by calling add_action( $tag, $function_to_add, $priority, $accepted_args ); or add_filter( $tag, $function_to_add, $priority, $accepted_args );

  • $tag (required) –  the hook name
  • $function_to_add (required) – the name of your custom function
  • $priority (optional) –  used to specify the order in which the functions associated with a particular action are executed
  • $accepted_args (optional) – the number of arguments your custom function accepts
  • $value (required) – the value which the filters hooked to $tag may modify
  • $var (optional) – can be one or more additional variables passed to the filter functions.
  • $arg (optional) –  the list of arguments to send to the hook.

Below is an example that illustrates the use of the filter hook .

<?php wp_dropdown_categories( apply_filters(‘wpmfc_calendar_taxonomy_args‘, $taxonomy_args, $taxonomy ) ); ?>

The first parameter in the apply_filters function is the filter hook name, in this case it is the ‘wpmfc_calendar_taxonomy_args’ parameter.

<?php
add_filter( ‘wpmfc_calendar_taxonomy_args‘, ‘custom_taxonomy_dropdown‘, 10, 2 );
function custom_taxonomy_dropdown( $taxonomy_args, $taxonomy ) {
if ( ‘Event Tags‘ === $taxonomy->labels->name ) {
$taxonomy_args[‘show_option_all‘] = ‘All Locations‘;
} elseif ( ‘Event Categories‘ === $taxonomy->labels->name ) {
$taxonomy_args[‘show_option_all‘] = ‘All Events‘;
}
return $taxonomy_args;
}
?>

An excellent resource for the discovering hooks in WordPress is maintained by Adam Brown: http://adambrown.info/p/wp_hooks

Code Generators

There are online code generators that are very useful for speeding up the development process:
GenerateWP – generates custom code for WordPress using the latest WordPress coding standards and API’s.
WordPress Query Generator – simplifies the process of creating custom queries.

The “Developer” plugin

The Developer plugin is a plugin installer that recommends essential tools and plugins for theme/plugin development.  Consider it required if you’re serious about writing plugins.

BONUS TIPS:
Below are some WordPress functions I wish I knew about earlier!

 

Restored from Internet Archive · Capture: Sat, 19 Sep 2015 19:42:31 GMT