Skip to content

Instantly share code, notes, and snippets.

@jimfloss
Last active March 2, 2022 03:59
Show Gist options
  • Save jimfloss/9cc5cbfb979e34824d59318a5f33c430 to your computer and use it in GitHub Desktop.
Save jimfloss/9cc5cbfb979e34824d59318a5f33c430 to your computer and use it in GitHub Desktop.
Custom WYSIWYG editor
<?php
function add_custom_meta_boxes_to_page() {
add_meta_box( 'custom_wysiwyg', __('Custom WYSIWYG'), 'custom_wysiwyg', 'page', 'normal', 'high' );
}
add_action( 'add_meta_boxes_page', 'add_custom_meta_boxes_to_page' );
add_action( 'save_post', 'save_custom_wysiwyg' );
//Build
function custom_wysiwyg() {
global $post;
$wysiwyg = get_post_meta($post->ID, 'wysiwyg', true);
wp_nonce_field('wysiwyg_nonce', 'meta_box_nonce');
?>
<p>
<?php
//Custom WYSIWYG https://codex.wordpress.org/Function_Reference/wp_editor
$content = $long_description;
$editor_id = 'wysiwyg';
$settings = array(
'wpautop' => true //Still need wpautop() in theme for some reason
); ?>
<label for="<?php echo $editor_id; ?>"><strong>Description:</strong><br/>
<?php wp_editor( $content, $editor_id, $settings );?>
</p>
<?php
}
//Save
function save_custom_wysiwyg($post_id) {
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'wysiwyg_nonce' ] ) && wp_verify_nonce( $_POST[ 'wysiwyg_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
if ( $is_autosave || $is_revision || !$is_valid_nonce ) { return; }
if( isset( $_POST[ 'wysiwyg' ] ) ) { update_post_meta( $post_id, 'wysiwyg', wp_kses_post( $_POST[ 'wysiwyg' ] /* Allows all HTML that a post would allow for WYSIWYG */ ) ); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment