I'm new to creating meta boxes, but I've managed to create a good one for my needs. However, I need that two of the entry boxes be formatted text (shown in my code); saved as such and output to the site as such. I can't seem to make it work, though. I can get the data to save to the database, which is good - but it won't save the text with things such as breaks, bold, h1 tags, paragraphs and so forth. Though I've removed a lot of the other elements to shorten up the code for your review, here's what I have so far:
class Tour_Meta {
public function __construct() {
if ( is_admin() ) {
add_action( 'load-post.php', array( $this, 'init_metabox' ) );
add_action( 'load-post-new.php', array( $this, 'init_metabox' ) );
}
}
public function init_metabox() {
add_action( 'add_meta_boxes', array( $this, 'add_metabox' ) );
add_action( 'save_post', array( $this, 'save_metabox' ), 10, 2 );
}
public function add_metabox() {
add_meta_box(
'tourMeta',
__( 'Details for this tour', 'dappa' ),
array( $this, 'render_trmeta' ),
'tours',
'advanced',
'high'
);
}
public function render_trmeta( $post ) {
$trmet_itinerary = get_post_meta($post->ID, 'trmet_itinerary', true);
$trmet_overview = get_post_meta( $post->ID, 'trmet_overview', true );
if( empty( $trmet_itinerary ) ) $trmet_itinerary = '';
if( empty( $trmet_overview ) ) $trmet_overview = '';
echo '<div class="form-table" id="tourEntries">';
echo' <h2>Tour Overview</h2>';
wp_editor(htmlspecialchars_decode($trmet_overview) , 'trmet_overview', array(
"media_buttons" => true
));
echo' <h2>Tour Itinerary</h2>';
wp_editor(htmlspecialchars_decode($trmet_itinerary) , 'trmet_itinerary', array(
"media_buttons" => true
));
echo' </div>';
}
public function save_metabox( $post_id, $post ) {
$trmet_new_itinerary = isset( $_POST[ 'trmet_itinerary' ] ) ? sanitize_text_field( $_POST[ 'trmet_itinerary' ] ) : '';
$trmet_new_overview = isset( $_POST[ 'trmet_overview' ] ) ? sanitize_text_field( $_POST[ 'trmet_overview' ] ) : '';
}
}
new Tour_Meta
I also tried different ways of outputting the post meta to the page, but that doesn't matter much anyway if on the backend it's not saving correctly.
Any ideas you have are appreciated. Thanks!
Pfft. Okay, so it was simple.
I needed to use
esc_html()
instead ofsanitize_text_field()
on those fields.Thanks!