Echo data from Meta Box not working? Why?

I am trying to simply echo some data from a custom meta box, i have no idea why it is not working.

functions.php

function global_notice_meta_box() {

add_meta_box(
‘global-notice’,
__( ‘Global Notice’, ‘textdomain’ ),
‘global_notice_meta_box_callback’,
‘post’
);

}
add_action( ‘add_meta_boxes’, ‘global_notice_meta_box’ );

We register the metabox.

Then we add text field…

function global_notice_meta_box_callback( $post ) {
wp_nonce_field( ‘global_notice_nonce’, ‘global_notice_nonce’ );
$value = get_post_meta( $post->ID, ‘_global_notice’, true );
echo ‘<textarea style=”width:100%” id=”global_notice” name=”global_notice”>’ . esc_attr( $value ) . ‘</textarea>’;

We add so custom meta box data is saved when post updated etc…

function save_global_notice_meta_box_data( $post_id ) {
if ( ! isset( $_POST[‘global_notice_nonce’] ) ) {
return;
}
if ( ! wp_verify_nonce( $_POST[‘global_notice_nonce’], ‘global_notice_nonce’ ) ) {
return;
}
if ( defined( ‘DOING_AUTOSAVE’ ) && DOING_AUTOSAVE ) {
return;
}
if ( isset( $_POST[‘post_type’] ) && ‘page’ == $_POST[‘post_type’] ) {

if ( ! current_user_can( ‘edit_page’, $post_id ) ) {
return;
}
}
else {
if ( ! current_user_can( ‘edit_post’, $post_id ) ) {
return;
}
}
if ( ! isset( $_POST[‘global_notice’] ) ) {
return;
}
$my_data = sanitize_text_field( $_POST[‘global_notice’] );
update_post_meta( $post_id, ‘_global_notice’, $my_data );

In editor page data is written to the database, i can see the custom field containing the newly entered data in the text field upon refreshing the page.

Now i try to echo this text field data within the loop

<?php $meta = get_post_meta($post->ID,’_global_notice’, true); ?>
<?php echo ‘meta box value: ‘ . $meta; ?>

No data is being echoed, just says “meta box value: “

Any ideas what i am missing? Must be something really simple?

Read more here:: Echo data from Meta Box not working? Why?

Leave a Reply

Your email address will not be published. Required fields are marked *