How do do_action and add_action work?

How do do_action and add_action work?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How do_action and add_action Work in WordPress

Understanding the relationship between add_action and do_action is critical for building maintainable WordPress plugins, yet the distinction often trips up developers new to the framework. The confusion usually stems from treating these hooks like standard function calls or file includes rather than a specific execution flow. When you attempt to modify a variable inside a hooked function without realizing how WordPress manages the call stack, your logic breaks silently.

In your scenario, you initialized $regularprice to 50 inside mainplugin_test. You correctly identified that executing code directly resulted in printing 100, but moving that logic into a separate function attached via add_action caused the final output to remain 50. This behavior occurs because do_action triggers a function call within the current scope, not by replacing the current execution path or including a file.

When you run your original code where the class check happens immediately:

function mainplugin_test() {
    $regularprice = 50;
    
    if (class_exists('rs_dynamic')) {
        $regularprice = 100;
    }
    
    echo $regularprice; // Prints 100
}

The variable is updated within the same function execution context before it is echoed. The scope of $regularprice remains local to mainplugin_test, and the assignment happens sequentially.

However, when you introduce hooks:

function mainplugin_test() {
    $regularprice = 50;
    
    do_action('testinghook');
    
    echo $regularprice; // Prints 50
}

function anothertest() {
    if (class_exists('rs_dynamic')) {
        $regularprice = 100;
    }
}
add_action('testinghook', 'anothertest');

The issue lies in variable scope. The $regularprice variable defined inside mainplugin_test is local to that specific function. When do_action('testinghook') executes, it calls anothertest. Inside anothertest, you are referencing a variable named $regularprice that does not exist yet. PHP treats this as an undefined variable, which defaults to null or empty string depending on context, rather than pulling the value from the outer scope. Even if the logic runs successfully in isolation, the assignment happens in a different function stack frame, leaving the original $regularprice untouched when control returns to mainplugin_test.

To fix this, you must either pass the variable as an argument or use a global scope (though globals are generally discouraged). A cleaner approach is to pass the value into the hook:

function mainplugin_test() {
    $regularprice = 50;
    
    // Pass the variable explicitly if the hook needs it
    do_action('testinghook', $regularprice);
    
    echo $regularprice; 
}

function anothertest($current_price) {
    if (class_exists('rs_dynamic')) {
        $current_price = 100;
    }
    return $current_price; // Return the new value to preserve context
}
add_action('testinghook', 'anothertest', 10, 1);

// In the main function, capture the result if needed

Alternatively, you can attach the hook to a global variable or use WordPress filters (apply_filters) if the goal is to modify a value before it is used. Filters are often more appropriate than actions when you intend to change data that flows through your logic. If you choose to stick with actions, ensure the modified state is either returned and captured or stored globally (e.g., in $_SESSION or a class property).

Another common pitfall involves the order of execution. add_action registers the callback, but do_action only triggers it when explicitly called. If you expect the hook to run automatically upon page load without a do_action call, the logic will never execute. Remember that hooks are passive until invoked.

For complex scenarios involving conditional class checks like rs_dynamic, consider refactoring the logic into a dedicated service class or using WordPress filters to modify the price dynamically based on available classes. This pattern avoids scoping issues entirely and aligns with WordPress best practices for extensibility.

Our network