sinanisler logo

Rich Editor Field, Custom Field with Editor, WordPress

// Add Metabox for Post Description
function add_description_meta_box() {
    add_meta_box('description_metabox_id', 'Description', 'description_meta_box_callback', 'post');
}
add_action('add_meta_boxes', 'add_description_meta_box');

// Metabox Callback for Description
function description_meta_box_callback($post) {
    // Add a nonce field for security
    wp_nonce_field('description_meta_box_nonce_action', 'description_meta_box_nonce');

    $description_value = get_post_meta($post->ID, 'description_metabox_text', true);

    // Editor settings for the description
    $editor_settings = array(
        'media_buttons' => true,
        'textarea_name' => 'description_metabox_text',
        'textarea_rows' => 8 // Adjust the number of rows as needed
    );
    
    // Display the editor for the description
    wp_editor($description_value, 'descriptionmetaboxeditor', $editor_settings);
}

// Save Metabox Content for Description
function save_description_meta_box_data($post_id) {
    // Verify nonce and user permissions, handle autosave
    if (!isset($_POST['description_meta_box_nonce']) ||
        !wp_verify_nonce($_POST['description_meta_box_nonce'], 'description_meta_box_nonce_action') ||
        (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) ||
        (isset($_POST['post_type']) && 'page' === $_POST['post_type'] && !current_user_can('edit_page', $post_id)) ||
        !current_user_can('edit_post', $post_id)) {
        return;
    }

    // Save the sanitized data
    if (isset($_POST['description_metabox_text'])) {
        $data = $_POST['description_metabox_text'];
        update_post_meta($post_id, 'description_metabox_text', $data);
    }
}
add_action('save_post', 'save_description_meta_box_data');

// Display the Metabox Content on the Front End
function display_description_meta_box_content($content) {
    global $post;
    $description_meta_box_content = get_post_meta($post->ID, 'description_metabox_text', true);
    if (!empty($description_meta_box_content)) {
        $content .= '<div class="description-meta-box-content">' . $description_meta_box_content . '</div>';
    }
    return $content;
}
add_filter('the_content', 'display_description_meta_box_content');

Recently made this code for our agency geopard digital client.

Leave the first comment