Monday, November 28, 2022
HomeWordPress Developmentphp - Displaying customized meta field worth in a customized publish web...

php – Displaying customized meta field worth in a customized publish web page


I’ve a difficulty with customized meta bins in a customized made plugin.
In reality, the duty is straightforward, however I’m new on this discipline. I made a plugin that permits person to enter the film title in a customized meta field. This title have to be seen on frontend, on a single customized publish web page.
My code is right here:

MOVIES.PHP

perform movie_cpt() {

$labels = array(
    'identify'                  => _x( 'Films', 'Put up Kind Basic Title', 'text_domain' ),
    'singular_name'         => _x( 'Film', 'Put up Kind Singular Title', 'text_domain' ),
    'menu_name'             => __( 'Films', 'text_domain' ),
    'name_admin_bar'        => __( 'Films', 'text_domain' ),
    'archives'              => __( 'Film Archive', 'text_domain' ),
    'attributes'            => __( 'Merchandise Attributes', 'text_domain' ),
    'parent_item_colon'     => __( 'Dad or mum Merchandise:', 'text_domain' ),
    'all_items'             => __( 'All Films', 'text_domain' ),
    'add_new_item'          => __( 'Add New Film', 'text_domain' ),
    'add_new'               => __( 'Add New Film', 'text_domain' ),
    'new_item'              => __( 'New Film', 'text_domain' ),
    'edit_item'             => __( 'Edit Film', 'text_domain' ),
    'update_item'           => __( 'Replace Film', 'text_domain' ),
    'view_item'             => __( 'View Film', 'text_domain' ),
    'view_items'            => __( 'View Film', 'text_domain' ),
    'search_items'          => __( 'Search Films', 'text_domain' ),
    'not_found'             => __( 'Film not Discovered', 'text_domain' ),
    'not_found_in_trash'    => __( 'Film not Present in Trash', 'text_domain' ),
    'featured_image'        => __( 'Featured Picture', 'text_domain' ),
    'set_featured_image'    => __( 'Set featured picture', 'text_domain' ),
    'remove_featured_image' => __( 'Take away featured picture', 'text_domain' ),
    'use_featured_image'    => __( 'Use as featured picture', 'text_domain' ),
    'insert_into_item'      => __( 'Insert into merchandise', 'text_domain' ),
    'uploaded_to_this_item' => __( 'Uploaded to this merchandise', 'text_domain' ),
    'items_list'            => __( 'Film Record', 'text_domain' ),
    'items_list_navigation' => __( 'Objects record navigation', 'text_domain' ),
    'filter_items_list'     => __( 'Filter objects record', 'text_domain' ),
);
$args = array(
    'label'                 => __( 'Film', 'text_domain' ),
    'description'           => __( 'Put up Kind Description', 'text_domain' ),
    'labels'                => $labels,
    'helps'              => array( 'title', 'editor'),
    'taxonomies'            => array( 'class', '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'       => 'web page',
    'show_in_rest'          => true,
    'register_meta_box_cb'  => 'movie_meta_box'
);
register_post_type( 'film', $args );

}
add_action( 'init', 'movie_cpt');

perform movie_meta_box() {
    add_meta_box(
        'movie-title',
        __('Film Title', 'sitepoint'),
        'movie_title_meta_box_callback',
    );
}

add_action('add_meta_boxes_movie', 'movie_meta_box');

perform movie_title_meta_box_callback($publish) {
    wp_nonce_field('movie_title_nonce', 'movie_title_nonce');
    $worth = get_post_meta($post->ID, '_movie_title', true);
    echo '<textarea fashion = "width:100%" id="movie_title" identify="movie_title">' . esc_attr( $worth ) . '</textarea>';
}
perform 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(outlined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    if(isset($_POST['post_type']) && 'web 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 right here:

<?php get_header();

$m_title = get_post_meta(get_the_ID(), 'movie-title', true)''

if(have_posts()) :
    whereas(have_posts()) : the_post();
    if(isset($m_title) && $m_title !== '') {
        echo printf('<h1><sturdy>Film Title: </sturdy>', $m_title, get_the_title());
    }
        the_content();
    endwhile;
endif;
get_footer();
?>

So, I feel I made this code good on the whole, however I am unable to see a price of customized meta discipline “Film Title” like I would like on frontend. What is usually a downside right here and how you can resolve it? Thanks prematurely.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments