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.