Skip to content

Instantly share code, notes, and snippets.

@rilwis
Last active August 18, 2017 02:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rilwis/eacd78b507df951cb0450ebd38ee14ac to your computer and use it in GitHub Desktop.
Save rilwis/eacd78b507df951cb0450ebd38ee14ac to your computer and use it in GitHub Desktop.
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