admin管理员组文章数量:1327982
So there is the following scenario.
I add an action to clean logs from the database:
add_action( 'myplugin_clean_logs', array( 'MyPlugin_Logs', 'clean_logs' ) );
Now I want to run this action periodically:
wp_schedule_event( current_time( 'timestamp' ), 'daily', 'myplugin_clean_logs' );
and execute it manually:
do_action( 'myplugin_clean_logs' );
The method MyPlugin_Logs::clean_logs
returns the count of affected rows or false if something went the other direction.
Now I want to display the number of rows that have been deleted. I would imagine something like this:
$affected_rows = do_action( 'myplugin_clean_logs' );
echo $affected_rows . ' entries have been deleted.';
But as do_action
will not return any value, I have no idea how to get the return value.
Should I execute the method directly on a manual run, but use the action on schedule events?
So there is the following scenario.
I add an action to clean logs from the database:
add_action( 'myplugin_clean_logs', array( 'MyPlugin_Logs', 'clean_logs' ) );
Now I want to run this action periodically:
wp_schedule_event( current_time( 'timestamp' ), 'daily', 'myplugin_clean_logs' );
and execute it manually:
do_action( 'myplugin_clean_logs' );
The method MyPlugin_Logs::clean_logs
returns the count of affected rows or false if something went the other direction.
Now I want to display the number of rows that have been deleted. I would imagine something like this:
$affected_rows = do_action( 'myplugin_clean_logs' );
echo $affected_rows . ' entries have been deleted.';
But as do_action
will not return any value, I have no idea how to get the return value.
Should I execute the method directly on a manual run, but use the action on schedule events?
Share Improve this question edited Apr 22, 2016 at 7:06 Aley asked Apr 21, 2016 at 16:47 AleyAley 3811 gold badge3 silver badges8 bronze badges 1- 1 You don't want to be echo'ing anything on a scheduled event, so yes, I would execute the method directly on a manual run (I'm assuming the administrator would trigger this, and you want to show them the output). – Tim Malone Commented Apr 21, 2016 at 22:55
3 Answers
Reset to default 16The cool thing is a filter is the same as an action, only it returns a value, so just set it up as a filter instead:
add_filter( 'myplugin_clean_logs', array( 'MyPlugin_Logs', 'clean_logs' ) );
Then something like:
$affected_rows = '';
$affected_rows = apply_filters( 'myplugin_clean_logs', $affected_rows );
should pass $affected_rows
to clean_logs()
(and whatever other functions you may have hooked to myplugin_clean_logs
) and assign the return value back to $affected_rows
.
This is a very old question but to answer the original question "How to do_action and get a return value?" for anyone looking you can do this using output buffering.
ob_start();
do_action( 'myplugin_clean_logs' );
$action_data = ob_get_clean();
This way you can store the do_action content in a variable.
Never used this function and haven't tested this, but might it work? do_action_ref_array().
function myplugin_clean_logs_fn() {
$args = array(
'param1' => 'val1',
'param2' => 'val2',
'affected_rows' => 0,
);
do_action_ref_array( 'myplugin_clean_logs', &$args );
return $args['affected_rows'];
}
// CALL IT
$affected_rows = my_plugin_clean_logs();
echo $affected_rows .' entr'. ($args['affected_rows']*1===1?'y':'ies') .' deleted.';
// SCHEDULE IT
add_action('myplugin_clean_logs_call_fn', 'myplugin_clean_logs_fn');
wp_schedule_event( current_time( 'timestamp' ), 'daily', 'myplugin_clean_logs_call_fn' );
// A SAMPLE FILTER
add_action('myplugin_clean_logs', function($args) {
// Cleaning process
// For each log affected, increment $args['affected_rows'] accordingly
}, 10, 3);
If that doesn't work, why not just filter the thing like Caspar suggested? I mean, that is the purpose of a filter, and in this case the number of affected rows is the thing being filtered. (I miss the old MortCore. Anyone remember how it handled return values, pass-by-reference, and arguments with just a single three parameter function?)
本文标签: actionsHow to doaction and get a return value
版权声明:本文标题:actions - How to do_action and get a return value? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742249275a2440442.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论