can I do something like that? to pass arguments to my function? I already studied add_action doc but did not figure out how to do it. What the exact syntax to pass two argum
Instead of:
add_action('thesis_hook_before_post','recent_post_by_author',10,'author,2')
it should be:
add_action('thesis_hook_before_post','recent_post_by_author',10,2)
...where 2 is the number of arguments and 10 is the priority in which the function will be executed. You don't list your arguments in add_action. This initially tripped me up. Your function then looks like this:
function function_name ( $arg1, $arg2 ) { /* do stuff here */ }
Both the add_action and function go in functions.php and you specify your arguments in the template file (page.php for example) with do_action like so:
do_action( 'name-of-action', $arg1, $arg2 );
Hope this helps.
Pass in vars from the local scope FIRST, then pass the fn
SECOND:
$fn = function() use($pollId){
echo "<p>NO POLLS FOUND FOR POLL ID $pollId</p>";
};
add_action('admin_notices', $fn);
Do
function reset_header() {
ob_start();
}
add_action('init', 'reset_header');
then
reset_header();
wp_redirect( $approvalUrl);
More info https://tommcfarlin.com/wp_redirect-headers-already-sent/
I use closure for PHP 5.3+. I can then pass the default values and mine without globals. (example for add_filter)
...
$tt="try this";
add_filter( 'the_posts', function($posts,$query=false) use ($tt) {
echo $tt;
print_r($posts);
return $posts;
} );
If you want to pass parameters to the callable function, instead of the do_action, you can call an anonymous function. Example:
// Route Web Requests
add_action('shutdown', function() {
Router::singleton()->routeRequests('app.php');
});
You see that do_action('shutdown')
don't accept any parameters, but routeRequests
does.
I ran into the same issue and solved it by using global variables. Like so:
global $myvar;
$myvar = value;
add_action('hook', 'myfunction');
function myfunction() {
global $myvar;
}
A bit sloppy but it works.