0

I want to schedule removal of a menu page and shortcode from wordpress after a certain time period. I am using below code but neither shortcode nor menu gets removed after a time period. Below is my code and if I run my code without scheduling with 'init' function both gets removed. Any idea what I am doing wrong.

add_action('init','s');
function s () {
    wp_schedule_event( current_time( 'timestamp' ),'mines', 'my_action');
}

add_action('my_action', array($this, 'zeeshan'));

function zeeshan(){  
    function custom_menu_page_removing() {
        remove_menu_page( 'booktaxi' );
    } //end menu page removing
    add_action( 'admin_menu', 'custom_menu_page_removing' );

    function remove_shortcode_from_index( $content ) {
        remove_shortcode('BOOKTAXI');
        $message = "Please buy the plugin license to continue";

        return $content.$message;
    } //end remove shortcode
    add_filter( 'the_content', 'remove_shortcode_from_index' );
}

And here is the custom time filter I am using to fire schedule actions after every 1 second.

add_filter('cron_schedules','my_cron_definer');    
function my_cron_definer($schedules) {  
    $schedules['mines'] = array( 'interval'=> 1, 'display'=>  __('Once Every 10 seconds')  );
    return $schedules;
}
0

First, unless zeeshan is actually a function inside a class, do not use array($this,'zeeshan'). (If it is in a class, nevermind.)

Second, even if this event is firing - it may or may not be? - the way you have it is only going to affect the current pageload when triggered, so is rather pointless. Instead you could update an option value when the event is fired.

add_action('my_action', 'zeeshan_switch');

function zeeshan_switch() {
    if (!add_option('remove_my_menu_and_shortcode','1')) {
        update_option('remove_my_menu_and_shortcode','1');
    }
}

Then add a switch check to the start of your actual function:

add_action('init', 'zeeshan');
function zeeshan(){  
    // check for the trigger value
    if (get_option('remove_my_menu_and_shortcode') != '1') {return;}

    function custom_menu_page_removing() {
        ............

By the way, I hope you are giving plenty of user warning / notification before having a plugin disable it's own functionality. It seems more like a crippleware strategy to me, an end user would probably prefer nagware over this! Instead you could consider a free / premium model, and use your time programming extra features for the premium version instead of on disabling the free ones. Just some random advice on what have been proven to work better.

| improve this answer | |
  • Thanks majick infact this code is running in a class .. secondly I want to schedule this page removing functionality ... so why does the scheduled event do not fire. – Zeeshan Mar 19 '16 at 8:28
  • Could be any number of reasons, You just need to debug more, if you use the option method described and then check on /wp-admin/options.php to see if has updated to 1? So you know for sure if the problem is the event or just the removal functions not firing. You could test the event is firing by writing a file in the function... maybe test time() instead of current_time..? Interval of 600 is 10 minutes by the way. Not sure but maybe functions inside class functions are not a good idea either. – majick Mar 19 '16 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.