0

I've hooked a function on to a CRON job called 'job_manager_check_for_expired_jobs', and I want it to run based on the value of a variable $create_sitemap. The value is true by default, but can be changed by a filter.

The problem is that if I use the site and then then change the filter to be false later, the hook still runs as though the variable is true - so the action isn't being removed.

Code I'm using is here, any help would be appreciated!

    // Check the WPJM plugin exists
    if( is_plugin_active( 'wp-job-manager/wp-job-manager.php') ){

    // Generate sitemap by default
    $create_sitemap = true;

    // Add filter so that users turn off the sitemap generation if they want
    if( has_filter('sitemap_filter_func') ) {
        $create_sitemap = apply_filters('sitemap_filter_func', $create_sitemap);
    }

    // If we want to ping Google
    if( $create_sitemap == true ){

        // Create the CRON job
        add_action( 'job_manager_check_for_expired_jobs', 'generate_sitemap' );

    } else {

        // Remove the CRON job to update the sitemap
        remove_action( 'job_manager_check_for_expired_jobs', 'generate_sitemap' );

    }

    }

function generate_sitemap() {

$sitemap = '<?xml version="1.0" encoding="UTF-8"?>';
$sitemap .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

// Get a query of all jobs that are available
$all_jobs = new WP_Query( array( 'post_type' => 'job_listing', 'post_status' => 'publish', 'posts_per_page' => -1 ) );

// Add the URL and last modified time (in GMT) to the sitemap
foreach( $all_jobs->posts as $post ){

  $sitemap .= '<url>';
  $sitemap .= '<loc>' . get_the_permalink( $post->ID ) . '</loc>';
  $sitemap .= '<lastmod>' . date( 'c', strtotime( $post->post_modified_gmt ) ) . '</lastmod>';
  $sitemap .= '</url>';

}

$sitemap .= '</urlset>';

// Write the sitemap to yoursite.com/job-sitemap.xml
$fp = fopen(ABSPATH . 'job-sitemap.xml', 'w');
fwrite($fp, $sitemap);
fclose($fp);

}

Example filter being used:

add_filter( 'sitemap_filter_func', 'hide_the_sitemap');
function hide_the_sitemap( $create_sitemap ){
    return false;
}
  • Where are you using/putting this conditional code? – Junaid Jul 3 '17 at 10:51
  • It's in the index file of a custom plugin. – stevenkellow Jul 3 '17 at 10:52
  • Maybe you can try prioritising your add_action and remove_action hooks? Also, does your generate_map function initiates any scheduled event or does it contain code for generating sitemap only? – Junaid Jul 3 '17 at 11:01
  • Can you post a full sample code we can use to reproduce the issue? – cybmeta Jul 3 '17 at 11:08
  • 1
    I am not sure why this is not working, but alternatively, you could add the conditional tag to your code inside generate_sitemap so if $create_sitemap is true run the code for sitemap generation, otherwise not. – Junaid Jul 3 '17 at 11:52
1

I am not sure why the remove_action isn't working because by the looks of your code, it should. Alternatively, you can add the conditional logic to your generate_sitemap()

function generate_sitemap() {

    global $create_sitemap;

    if ( $create_sitemap ) return false;

    $sitemap = '<?xml version="1.0" encoding="UTF-8"?>';
    $sitemap .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

    // Get a query of all jobs that are available
    $all_jobs = new WP_Query( array( 'post_type' => 'job_listing', 'post_status' => 'publish', 'posts_per_page' => -1 ) );

    // Add the URL and last modified time (in GMT) to the sitemap
    foreach( $all_jobs->posts as $post ){
        $sitemap .= '<url>';
        $sitemap .= '<loc>' . get_the_permalink( $post->ID ) . '</loc>';
        $sitemap .= '<lastmod>' . date( 'c', strtotime( $post->post_modified_gmt ) ) . '</lastmod>';
        $sitemap .= '</url>';
    }

    $sitemap .= '</urlset>';

    // Write the sitemap to yoursite.com/job-sitemap.xml
    $fp = fopen(ABSPATH . 'job-sitemap.xml', 'w');
    fwrite($fp, $sitemap);
    fclose($fp);
}

Make sure $create_sitemap is accessible within the function.

| improve this answer | |

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.