Identifying Functions within a Plugin

In a previous section, we explore how plugins interact with WordPress by using the plugin API. Here, we show you how functions are defined in a plugin. In PHP, a function is a predefined set of commands carried out when the function is called. It's the same in WordPress; some functions are common PHP functions, and some functions are specific to WordPress. We discuss how to hook into these functions through the plugin API so that you can call existing commands instead of having to write them over again in your plugin. However, you can also write your own functions and call them in your plugin.

So how do you identify functions within a plugin? Here's an example from the Hello Dolly plugin; this code function is found at the bottom, as the last line of the plugin file: hello.php:

add_action('admin_head', 'dolly_css');

The WordPress Codex documentation describes the add_action function as having the following arguments, or parameters:

add_action( $tag, $function_to_add, $priority, $accepted_args );
  • $tag is the name of the action you want to hook onto.
  • $function_to_add is the name of the function you're calling.
  • $priority is an optional value that represents how important the function is. The default is 10, a lower number makes the function run earlier, and a higher number makes the function run later.
  • $accepted_args is the number of arguments your function takes. This is optional, and the default is 1.

In PHP code, the dollar sign, $

Get WordPress® All-in-One For Dummies® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.