Đây là đoạn code thực hiện việc thay đổi text bài viết theo thời gian tự chọn mà tôi đã tạo ra.
add_action('admin_menu', 'custom_text_replace_menu');
function custom_text_replace_menu() {
add_submenu_page(
'options-general.php',
'Custom Text Replace Settings',
'Custom Text Replace',
'manage_options',
'custom-text-replace-settings',
'custom_text_replace_settings_page'
);
}
function custom_text_replace_settings_page() {
if (isset($_POST['custom_text_replace_submit'])) {
$start_date = strtotime($_POST['custom_text_replace_start_date']);
$end_date = strtotime($_POST['custom_text_replace_end_date']);
update_option('custom_text_replace_start_date', $start_date);
update_option('custom_text_replace_end_date', $end_date);
update_option('custom_text_replace_search_text', sanitize_text_field($_POST['custom_text_replace_search_text']));
update_option('custom_text_replace_replace_text', sanitize_text_field($_POST['custom_text_replace_replace_text']));
// Perform text replacement here
custom_text_replace_execute();
}
$start_date = get_option('custom_text_replace_start_date');
$end_date = get_option('custom_text_replace_end_date');
$search_text = get_option('custom_text_replace_search_text');
$replace_text = get_option('custom_text_replace_replace_text');
?>
<div class="wrap">
<h2>Custom Text Replace Settings</h2>
<form method="post">
<label for="custom_text_replace_start_date">Start Date:</label>
<input type="date" id="custom_text_replace_start_date" name="custom_text_replace_start_date" value="<?= date('Y-m-d', $start_date) ?>"><br>
<label for="custom_text_replace_end_date">End Date:</label>
<input type="date" id="custom_text_replace_end_date" name="custom_text_replace_end_date" value="<?= date('Y-m-d', $end_date) ?>"><br>
<label for="custom_text_replace_search_text">Search Text:</label>
<input type="text" id="custom_text_replace_search_text" name="custom_text_replace_search_text" value="<?= esc_attr($search_text) ?>"><br>
<label for="custom_text_replace_replace_text">Replace Text:</label>
<input type="text" id="custom_text_replace_replace_text" name="custom_text_replace_replace_text" value="<?= esc_attr($replace_text) ?>"><br>
<input type="submit" name="custom_text_replace_submit" value="Replace">
</form>
</div>
<?php
}
function custom_text_replace_execute() {
$start_date = get_option('custom_text_replace_start_date', 0);
$end_date = get_option('custom_text_replace_end_date', current_time('timestamp'));
$args = array(
'post_type' => 'post', // Change to your custom post type if needed
'posts_per_page' => -1,
'date_query' => array(
'after' => date('Y-m-d', $start_date),
'before' => date('Y-m-d', $end_date),
'inclusive' => true,
),
);
$query = new WP_Query($args);
$search_text = get_option('custom_text_replace_search_text');
$replace_text = get_option('custom_text_replace_replace_text');
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$post_id = get_the_ID();
$content = get_the_content();
$new_content = str_replace($search_text, $replace_text, $content);
// Update the post content
wp_update_post(array(
'ID' => $post_id,
'post_content' => $new_content,
));
}
wp_reset_postdata();
}
}
// Hook the function to the WordPress action 'init'
add_action('init', 'custom_text_replace');