10+ WordPress Functions Every Developer Should Know About

add_action($tag, $function_to_add, $priority, $accepted_args)
do_action($tag, $arg)

add_action() attaches a function to a specific action, which is then triggered by a do_action() call somewhere during execution. This can be used to execute a group of functions using the $priority parameter to determine order (optional), and arguments can be passed through the $accepted_args parameter. Below are a few examples of how this is used in the WordPress platform:

WordPress Logo

  • do_action(‘deleted_post’)
    This is executed after a post or page is deleted, and post or page ID is passed as a parameter.  This allows you add custom functionality whenever a post is deleted.  For example, lets say you want to be notified every time a post is deleted.  You can create a function to send the e-mail, and execute add_action(‘deleted_post’, ‘notify_when_deleted’, 10, $email). After that, when do_action(‘deleted_post’) is called, the notify_when_deleted() function will be called.
  • do_action(‘wp_head’)
    The wp_head action is traditionally called within a theme’s header.  If you wanted to create an SEO plugin, you could very easily add an action to wp_head to display meta tags.

The Plugin/Action API is very powerful, and is important during theme and plugin development.  Read more about the Plugin/Action API on WordPress.org.

add_filter($tag, $function_to_add, $priority, $accepted_args)
apply_filters($tag, $value)

The apply_filters() function is most often used for filtering text.  A list of default filters can be seen in /wp-includes/default-filters.php; however, you will also find these scattered throughout the WordPress source files.  Line 50 marks the beginning of the filters for display URLs.  There are four functions added to this category, which is composed of five different filters.  The functions used for display URLs include:

  • strip_tags() – Used to strip HTML/PHP out of a string
  • trim() – Used to trim whitespace from a string
  • esc_url_raw() – WP function for filtering URLs for use in a database
  • wp_filter_kses() – Function from the Kses library, which “sanitizes content with allowed HTML Kses rules”*

So instead of having to execute four different functions every time a display URL needs filtering, you would enter something like…

// Add functions to use for 'text_filter'
add_filter('text_filter', 'strip_tags');
add_filter('text_filter', 'trim');
add_filter('text_filter', 'my_custom_filter_function');

// Pass $unfilteredText through 'text_filter'
$filteredText = apply_filters('text_filter', $unfilteredText);

* The Kses library helps prevent cross-site scripting attacks (XSS), and filter HTML elements & attributes.
** View WordPress.org’s Filter Reference

wp_register_script($handle, $src, $deps, $ver, $in_footer)
wp_enqueue_script($handle, $src, $deps, $ver, $in_footer)
wp_register_style($handle, $src, $deps, $ver, $media)
wp_enqueue_style($handle, $src, $deps, $ver, $media)

This is one of the most beautiful parts of WordPress.  WordPress will make sure that jQuery is loaded before any libraries that require it to function, as well as handle any other parent-dependent relationship.  You must, of course, specify the dependencies in an array using the $deps parameter for this to work.  jQuery UI Dialog illustrates this functionality very well.  See below for an example:

 * Add the scripts to our library (this does not enqueue them for display).
 * @note The scripts in this example are already included within the scripts-loader.php file,
 * so no need to include them again.  We are simply using them to illustrate a point.
wp_register_script('jquery', '/wp-includes/js/jquery/jquery.js', false, '1.3.2');
wp_register_script('jquery-ui-core', '/wp-includes/js/jquery/ui.core.js', array('jquery'), '1.7.1', true);
wp_register_script('jquery-ui-resizable', '/wp-includes/js/jquery/ui.resizable.js', array('jquery-ui-core'), '1.7.1', true);
wp_register_script('jquery-ui-draggable', '/wp-includes/js/jquery/ui.draggable.js', array('jquery-ui-core'), '1.7.1', true);
wp_register_script('jquery-ui-dialog', '/wp-includes/js/jquery/ui.dialog.js', array('jquery-ui-resizable', 'jquery-ui-draggable'), '1.7.1', true);

// Enqueue the jQuery UI Dialog script

// By default, the 'wp_head' action triggers 'wp_print_styles' and 'wp_print_head_scripts', which will print our scripts

When our page displays and the wp_head action is called, the jQuery UI Dialog script and it’s dependencies will be loaded.  Not to bad, eh?  This sure makes a lot of jobs simpler.  Dealing with JavaScript dependencies programmatically can help you to streamline your projects and become more efficient.  I have ported this model to Zend Framework and several other projects, and it has served its purpose quite well.  It beats writing out each script every time you need to use them.

add_option($name, $value, $deprecated, $autoload)
update_option($name, $value, $deprecated, $autoload)
get_bloginfo($show, $filter)

If you plan on having any custom settings within your themes, you’ll probably be using these functions.  Quite simply, they allow you to add, delete, edit, and retrieve WordPress options from the database.  Before doing anything with options, you should get to know the wp_options database table so you do not inadvertently delete the core WordPress functions.  Always use a prefix when creating a new option (i.e. mytheme_option1, mytheme_option2).  I included bloginfo() and get_bloginfo() because they work off of options, although they are limited in scope (see bloginfo entry on WP.org).  You can fetch any option with get_option().  Below are few working examples:

  • $siteurl = get_option(‘siteurl’); // returns base WP URL
  • bloginfo(‘url’); // outputs base WP URL
  • $blogName = get_bloginfo(‘name’); // returns blog name

This is really only the tip of the iceberg, but these are the core functions you’ll need to know in order to create a decent plugin or theme.  Feel free to post questions, or comments.  We’d love to hear your input!

One thought on “10+ WordPress Functions Every Developer Should Know About”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>