Skip to content

Instantly share code, notes, and snippets.

@iamdavidabayomi
Last active August 19, 2022 12:51
Show Gist options
  • Save iamdavidabayomi/8ae1fa00ee1b635d0f3066d6cdb60003 to your computer and use it in GitHub Desktop.
Save iamdavidabayomi/8ae1fa00ee1b635d0f3066d6cdb60003 to your computer and use it in GitHub Desktop.
Add a wysiwyg as a metabox in wordpress
As of now this is enough to add a WYSIWYG editor as a meta box and save its value.
@iamdavidabayomi
Copy link
Author

// add the meta box
add_action('add_meta_boxes', function () {
    add_meta_box('additional_meta', 'Product Info', 'additional_meta_cb', ['post'], 'normal');
});

// The function to show the editor and its content if available.
function addition_meta_cb($post) {
    // get the content
    $text = get_post_meta($post, 'additional_meta', true);
    // add wp_editor the the screen
    wp_editor(htmlspecialchars($text), additional_meta_ID', $settings = array('textarea_name' => 'additional_meta', 'media_buttons' => true, 'tinymce' => true, 'teeny' => false, 'wpautop' => true));
}

// save the metabox
add_action('save_post', function ($post_id) {
    if (!empty($_POST['additional_meta'])) {
        $data = sanitize_text_field($_POST['additional_meta']);
        update_post_meta($post_id, 'additional_meta', $data);
    }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment