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?

3

The 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.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *