Site icon T4Tutorials.com

How to add extra date option with published and last modified date?

How to add extra date option with published and last modified date?

To do this, put this code in functions.php of your wordpress site.

// Add custom meta box for selecting last date of post
function custom_last_date_meta_box() {
    add_meta_box(
        'custom-last-date-meta-box',
        'Last Date of Post',
        'custom_last_date_meta_box_callback',
        'post',
        'side',
        'default'
    );
}
add_action('add_meta_boxes', 'custom_last_date_meta_box');

// Callback function to display the custom meta box
function custom_last_date_meta_box_callback($post) {
    // Retrieve the current last date of the post, if available
    $last_date = get_post_meta($post->ID, 'custom_last_date', true);
    ?>
    <label for="custom_last_date">Select the last date of the post:</label>
    <input type="date" id="custom_last_date" name="custom_last_date" value="<?php echo esc_attr($last_date); ?>" />
    <?php
}

// Save custom meta box data
function save_custom_last_date_meta_box_data($post_id) {
    // Check if this is an autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }

    // Check if nonce is set
    if (!isset($_POST['custom_last_date_meta_box_nonce'])) {
        return;
    }

    // Verify that the nonce is valid
    if (!wp_verify_nonce($_POST['custom_last_date_meta_box_nonce'], 'custom_last_date_meta_box')) {
        return;
    }

    // Check if user has permissions to save post data
    if (!current_user_can('edit_post', $post_id)) {
        return;
    }

    // Save the custom last date
    if (isset($_POST['custom_last_date'])) {
        update_post_meta($post_id, 'custom_last_date', sanitize_text_field($_POST['custom_last_date']));
    }
}
add_action('save_post', 'save_custom_last_date_meta_box_data');

 

Exit mobile version