php – Displaying custom meta box value in a custom post page
Question
I have an issue with custom meta boxes in a custom made plugin.
In fact, the task is simple, but I am new in this field. I made a plugin that allows user to enter the movie title in a custom meta box. This title must be seen on frontend, on a single custom post page.
My code is here:
MOVIES.PHP
function movie_cpt() {
$labels = array(
'name' => _x( 'Movies', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Movie', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Movies', 'text_domain' ),
'name_admin_bar' => __( 'Movies', 'text_domain' ),
'archives' => __( 'Movie Archive', 'text_domain' ),
'attributes' => __( 'Item Attributes', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'all_items' => __( 'All Movies', 'text_domain' ),
'add_new_item' => __( 'Add New Movie', 'text_domain' ),
'add_new' => __( 'Add New Movie', 'text_domain' ),
'new_item' => __( 'New Movie', 'text_domain' ),
'edit_item' => __( 'Edit Movie', 'text_domain' ),
'update_item' => __( 'Update Movie', 'text_domain' ),
'view_item' => __( 'View Movie', 'text_domain' ),
'view_items' => __( 'View Movie', 'text_domain' ),
'search_items' => __( 'Search Movies', 'text_domain' ),
'not_found' => __( 'Movie not Found', 'text_domain' ),
'not_found_in_trash' => __( 'Movie not Found in Trash', 'text_domain' ),
'featured_image' => __( 'Featured Image', 'text_domain' ),
'set_featured_image' => __( 'Set featured image', 'text_domain' ),
'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
'use_featured_image' => __( 'Use as featured image', 'text_domain' ),
'insert_into_item' => __( 'Insert into item', 'text_domain' ),
'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
'items_list' => __( 'Movie List', 'text_domain' ),
'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
'filter_items_list' => __( 'Filter items list', 'text_domain' ),
);
$args = array(
'label' => __( 'Movie', 'text_domain' ),
'description' => __( 'Post Type Description', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor'),
'taxonomies' => array( 'category', 'post_tag' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
'show_in_rest' => true,
'register_meta_box_cb' => 'movie_meta_box'
);
register_post_type( 'movie', $args );
}
add_action( 'init', 'movie_cpt');
function movie_meta_box() {
add_meta_box(
'movie-title',
__('Movie Title', 'sitepoint'),
'movie_title_meta_box_callback',
);
}
add_action('add_meta_boxes_movie', 'movie_meta_box');
function movie_title_meta_box_callback($post) {
wp_nonce_field('movie_title_nonce', 'movie_title_nonce');
$value = get_post_meta($post->ID, '_movie_title', true);
echo '<textarea style = "width:100%" id="movie_title" name="movie_title">' . esc_attr( $value ) . '</textarea>';
}
function save_movie_title_meta_box_data($post_id) {
if (!isset($_POST['movie_title_nonce'])) {
return;
}
if(!wp_verify_nonce( $_POST['movie_title_nonce'], 'movie_title_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['movie_title'])) {
return;
}
$my_data = sanitize_text_field( $_POST['movie_title'] );
update_post_meta( $post_id, '_movie_title', $my_data );
}
add_action('save_post', 'save_movie_title_meta_box_data');
File SINGLE-MOVIE.PHP is here:
<?php get_header();
$m_title = get_post_meta(get_the_ID(), 'movie-title', true)''
if(have_posts()) :
while(have_posts()) : the_post();
if(isset($m_title) && $m_title !== '') {
echo printf('<h1><strong>Movie Title: </strong>', $m_title, get_the_title());
}
the_content();
endwhile;
endif;
get_footer();
?>
So, I think I made this code good in general, but I can’t see a value of custom meta field “Movie Title” like I want on frontend. What can be a problem here and how to solve it? Thank you in advance.
0
2 months
2022-11-27T21:53:09-05:00
2022-11-27T21:53:09-05:00 0 Answers
0 views
0
Leave an answer