Using WordPress ‘add_action()’ PHP function

The add_action() WordPress PHP function allows you to attach a callback function to a specified action hook, enabling the function to execute when that hook is called during the WordPress core process.

Usage

add_action($hook_name, $callback, $priority, $accepted_args);

Parameters

  • $hook_name (string): The name of the action hook to which the callback function will be added.
  • $callback (callable): The function to be executed when the action hook is called.
  • $priority (int, optional): Specifies the order in which functions associated with the hook are executed. Lower numbers execute earlier. Default is 10.
  • $accepted_args (int, optional): The number of arguments the function accepts. Default is 1.

More information

See WordPress Developer Resources: add_action()

Examples

Register a custom script

Register a custom JavaScript file to be included in the front-end.

function my_enqueue_scripts() {
    wp_enqueue_script('my-custom-script', get_template_directory_uri() . '/js/custom.js', array('jquery'), '1.0', true);
}
add_action('wp_enqueue_scripts', 'my_enqueue_scripts');

Add a custom column to the admin post list

Add a new column to the posts list in the WordPress admin dashboard.

function add_custom_column($columns) {
    $columns['custom_column'] = 'Custom Column';
    return $columns;
}
add_action('manage_posts_columns', 'add_custom_column');

Modify the post content

Add a custom message to the end of every post’s content.

function add_custom_message($content) {
    $custom_message = '**This is a custom message**';
    return $content . $custom_message;
}
add_action('the_content', 'add_custom_message');

Redirect user after login

Redirect the user to a custom page after they log in.

function redirect_after_login($redirect_to, $requested_redirect_to, $user) {
    if (!is_wp_error($user)) {
        return home_url('custom-page');
    }
    return $redirect_to;
}
add_action('login_redirect', 'redirect_after_login', 10, 3);

Modify the text displayed in the footer of the WordPress admin dashboard.

function custom_admin_footer_text() {
    echo 'Powered by **My Custom Plugin**';
}
add_action('admin_footer_text', 'custom_admin_footer_text');

Basic usage

This example sends an email to a set of friends every time a post is published.

function email_friends($post_id) {
  $friends = '[email protected], [email protected]';
  wp_mail($friends, 'New post published', 'Check out my new post!');
  return $post_id;
}
add_action('publish_post', 'email_friends');

Using with a Class

In this example, we’re adding an action within a class constructor.

class MyClass {
  public function __construct() {
    add_action('save_post', array($this, 'save_posts'));
  }

public function save_posts() { // Save post logic here... } } $myClass = new MyClass();

Using with static functions in a Class

Here’s an example of using add_action() with a static function within a class.

class MyStaticClass {
  public static function init() {
    add_action('save_post', array(get_called_class(), 'save_posts'));
  }

public static function save_posts() { // Save post logic here... } } MyStaticClass::init();

Passing parameters to the function

You can also pass parameters to your function. Here’s how to do that:

class MyClass {
  public function __construct() {
    add_action('init', array($this, 'call_somefunction'));
  }

public function call_somefunction() { $this->somefunction('Hello, World!'); }

public function somefunction($text) { echo $text; } }

Using closures

Lastly, here’s an example using closures. But remember, using closures in themes or plugins meant for distribution is not recommended as it would make it impossible to remove the action.

$myVar = 'Hello, World!';
$action_wp_footer = function() use ($myVar) {
  echo $myVar;
};
add_action('wp_footer', $action_wp_footer, 100, 1);

You can remove this action with the following line of code:

remove_action('wp_footer', $action_wp_footer, 100);

This approach is useful as any typos in the closure name will yield a helpful “Undefined variable” error.