'How to use add action to call a wordpress function and still get a return value?

Lets say I have a random function with a return value:

function someFunction() { 
   return 1234;
}

And now I wanna call that function using add_action and set a variable to the return value of the function:

$somevar = add_action('plugins_loaded', 'someFunction');

However add_action always returns true, so the value of $somevar ends up being true instead of the return value. Is there a way to set the variable to the return value?

I cant really get around using add_action.



Solution 1:[1]

According to the WordPress documentation add_action() always returns true https://developer.wordpress.org/reference/functions/add_action/

This function is simply used to register a callback function to execute at the respecitve execution point during the wordrpess life cycle. Nothing more. see also https://codex.wordpress.org/Plugin_API/Action_Reference

the plugins_loaded action is saying "once all the wordrpess plugins have loaded, execute these additional tasks" before moving onto the rest of the wordpress code.

/**
 * Fires once activated plugins have loaded.
 *
 * Pluggable functions are also available at this point in the loading order.
 *
 * @since 1.5.0
 */
do_action( 'plugins_loaded' );

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Scuzzy