Skip to content

Instantly share code, notes, and snippets.

@rilwis rilwis/code.php
Last active Aug 18, 2017

Embed
What would you like to do?
How to save WYSIWYG content as post content
<?php
/**
* This demo shows you how to save WYSIWYG field content created by Meta Box plugin as post content.
*
* @link https://metabox.io/docs/how-to-save-wysiwyg-content-as-post-content
*/
add_action( 'init', function () {
// Remove the editor for the post type.
remove_post_type_support( 'post', 'editor' ); // Change 'post' to your custom post type.
} );
add_filter( 'rwmb_meta_boxes', function ( $meta_boxes ) {
// Get the current post content and set as the default value for the wysiwyg field.
$default_content = '';
$post_id = filter_input( INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT );
if ( ! $post_id ) {
$post_id = filter_input( INPUT_POST, 'post_ID', FILTER_SANITIZE_NUMBER_INT );
}
if ( $post_id ) {
$default_content = get_post_field( 'post_content', $post_id );
}
$meta_boxes[] = [
'title' => 'Test',
'post_types' => 'post', // Change 'post' to your custom post type.
'fields' => [
// Register a wysiwyg field of which the content is saved as post content.
[
'type' => 'wysiwyg',
'id' => 'content', // This is the must!
'name' => 'Fake content',
'std' => $default_content,
],
// This custom html is used to output custom style to overwrite the editor style set by WordPress.
[
'type' => 'custom_html',
'std' => '<style>#wp-content-editor-tools { background: none; padding-top: 0; }</style>',
],
],
];
return $meta_boxes;
} );
// Do not save to post meta.
add_filter( 'rwmb_content_value', '__return_empty_string' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.