Last active
July 2, 2024 10:18
-
-
Save milbar/5f96f2d443b9998195e05ba75c789ab2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Clear WP Content On redesigned pages | |
* Description: Clears the default WP content if the ACF flexible content field is not empty and uses the specified template. | |
* Version: 1.0 | |
* Author: Milan Bartalovics | |
*/ | |
// Schedule the event on plugin activation | |
function yco_yearly_cron_schedule() { | |
// Schedule the event to run one year from now | |
$timestamp = strtotime('+1 year'); | |
if (!wp_next_scheduled('yco_clear_redesign_content')) { | |
wp_schedule_event($timestamp, 'yearly', 'yco_clear_redesign_content'); | |
} | |
} | |
register_activation_hook(__FILE__, 'yco_yearly_cron_schedule'); | |
// Clear the scheduled event on plugin deactivation | |
function yco_yearly_cron_unschedule() { | |
$timestamp = wp_next_scheduled('yco_clear_redesign_content'); | |
if ($timestamp) { | |
wp_unschedule_event($timestamp, 'yco_clear_redesign_content'); | |
} | |
} | |
register_deactivation_hook(__FILE__, 'yco_yearly_cron_unschedule'); | |
// Hook the custom function to the scheduled event | |
add_action('yco_clear_redesign_content', 'yco_clear_redesign_content_if_acf_not_empty'); | |
function yco_clear_redesign_content_if_acf_not_empty() { | |
// Query to get all posts or pages using the specified template | |
$args = array( | |
'post_type' => array('page', 'industry_solution'), // Add your custom post types here | |
'meta_query' => array( | |
array( | |
'key' => '_wp_page_template', | |
'value' => 'views/template-redesign-2024.blade.php' | |
) | |
), | |
'posts_per_page' => -1 // Get all matching posts | |
); | |
$query = new WP_Query($args); | |
// Loop through the posts | |
if ($query->have_posts()) { | |
while ($query->have_posts()) { | |
$query->the_post(); | |
$post_id = get_the_ID(); | |
// Get the ACF flexible content field | |
$acf_field = get_field('lpt_content_sections', $post_id); | |
// Check if the ACF flexible content field is not empty | |
if (!empty($acf_field)) { | |
// Clear the default WordPress content | |
wp_update_post(array( | |
'ID' => $post_id, | |
'post_content' => '' | |
)); | |
// Log the deletion | |
error_log('Cleared content for post ID: ' . $post_id); | |
} | |
} | |
wp_reset_postdata(); | |
} else { | |
// Log if no posts found | |
error_log('No posts found with the specified template.'); | |
} | |
} | |
// Add a custom yearly interval to the cron schedules | |
function add_custom_yearly_interval($schedules) { | |
$schedules['yearly'] = array( | |
'interval' => 31556926, // 1 year in seconds | |
'display' => __('Once Yearly') | |
); | |
return $schedules; | |
} | |
add_filter('cron_schedules', 'add_custom_yearly_interval'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment