I looked into the difference between action and filter hooks, and as far as I understand, it is not possible to have a function, which is added to an action hook, return a (custom) value.

My scenario:

  • Upon saving a post, I try to save another post. Therefore I use add_action( 'save_post', 'create_another_post');
  • Depending on some conditions, saving this additional post takes place (for that case I'd like to return true) or it doesn't (return false)

How would I go about implementing this true/false scenario? I appreciate your help and ideas.

  • What are you going to do based on if it's true or false? It might make sense to call another function, or continue the process depending on what happens? If not, you might have to store the data to be retrieved at a later time. – Steven Jones May 19 '14 at 14:01

Correct. Action hooks do not return content, and honestly if you need an action hook to return content there is a pretty good chance that you are doing something wrong. However, if it turns out to be your only option the cleanest way to do it is with a static variable:

function action_value_grab() {
  static $value = false;
  $is_hooked = current_filter();
  if (!empty($is_hooked)) {
    $value = true;
  return $value;

// var_dump(action_value_grab()); // debug


// var_dump(action_value_grab()); // debug

Just call the function-- static_value_grab()-- after the hook fires to get the value of $value.


in my opinion this is a valid scenario, its a solid way to offer your plugin as an interface to other plugins but still each plugin can be active or inactive and wp will not crash (in comparison to calling a method directly which may not exist because a plugin was not activated

i solved it by passing an object, like so:

// define action with param for result transport

function some_action($someParam, $transport) {
    // do stuff
    if($someParam > 10) {
         $transport->return = true;
    } else {
         $transport->return = 5;
add_action('some_action', 'some_action', 10, 2);

// call action

do_action('some_action', 5, $transport = new stdClass());
// output true

do_action('some_action', 11, $transport = new stdClass());
// output 5
  • Ofcourse this works, but you are not returning anything within the function, so technically, you are just setting the property of a global object. – Solomon Closson Jun 26 '15 at 18:01
  • technically its not a global object, its a param to the method and its in my current scope – greenone83 Mar 22 '16 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.