wp-cron callback function types in OOP plugin (2 posts)

  1. Dan Collis-Puro
    Posted 5 years ago #

    So I'm writing a plugin where all the core functionality is OOP.

    This works fine for hooks and actions where the function is actually an instance method:

    global $wpdb;
    $cat_sub = new CategorySubscriptions($wpdb);
    add_action( 'edit_user_profile', array($cat_sub, 'show_profile_fields') );

    But it seems like I have to actually add my cron callback functions directly into the global namespace if wordpress is going to be able to execute them.

    I can't pass in an instance or static method hook, it looks like I can only pass in a string and that'd mean I have to have the function live in the global namespace.

    I understand why you can't pass in an instance method hook - it's not like you can serialize an instantiated object safely, especially one that changes. It would be nice to use a static method as a hook, though.

    Thanks for your thoughts in advance - really, I just want to confirm whether or not a wp-cron callback function has to live in the global namespace or not. Thanks!

  2. Dan Collis-Puro
    Posted 5 years ago #

    Actually, I am a dumbass. I have to add the custom action via add_action() and then wp_schedule_single_event() refers to the action name that I register. Quite elegant, actually, meaning I can use an instance method without issue.

Topic Closed

This topic has been closed to new replies.

About this Topic