Metabox by theme not working with custom post type , can not save

by Phan Nam Trường   Last Updated April 16, 2018 12:08 PM

I used the toroplay theme and created a custom post with "Custom Post Type UI", but meta box of the theme did not work, please help me, thank you very much.

This is the backdrop image does not work plugins\tr-grabber\inc\functions.php

function tr_grabber_meta_box() {
    add_meta_box(
        'tr_grabber_featured_meta_box',
        __('Backdrop', 'tr-grabber'),
        'tr_grabber_featured_meta_box_function',
        'post',
        'side',
        'low'
    );  
    add_meta_box(
        'additional_information_meta_box',
        __('Additional Information', 'tr-grabber'),
        'show_additional_information_meta_box',
        'post',
        'normal',
        'high'
    );
    if( tr_grabber_type() == 1 ) {

        add_meta_box(
            'links_meta_box',
            __('Links', 'tr-grabber'),
            'show_links_meta_box',
            'post',
            'normal',
            'low'
        );

    }
}
add_action( 'add_meta_boxes', 'tr_grabber_meta_box' );

//Backdrop

function tr_grabber_featured_meta_box_function( $post ) {
    global $label_remove,$label_use,$label_set,$title,$id,$hide_remove_button,$link_title,$photo_id,$post;
    $id = 'backdrop';
    $title = __('Backdrop', 'tr-grabber');
    $label_set = __('Set backdrop image', 'tr-grabber');
    $label_use = __('Use as backdrop', 'tr-grabber');
    $label_remove = __('Remove backdrop', 'tr-grabber');

    $photo_id = get_post_meta( $post->ID, TR_GRABBER_FIELD_BACKDROP, true );

    if( $photo_id ) {
        $link_title = wp_get_attachment_image( $photo_id, 'medium', false, array( 'style' => 'width:100%;height:auto;', ) );
        $hide_remove_button = '';
    }
    else {
        $photo_id = -1;
        $link_title = $label_set;
        $hide_remove_button = 'display: none;';
    }
    ?>

</td>
    <p class="hide-if-no-js trgrabber-image-container-<?php echo $id; ?>"><a href="#" class="trgrabber-add-media trgrabber-media-edit trgrabber-media-edit-<?php echo $id; ?>" data-title="<?php echo $title; ?>" data-button="<?php echo $label_use; ?>" data-id="<?php echo $id; ?>" data-postid="<?php echo $post->ID; ?>"><?php echo $link_title; ?></a></p>

    <p style="<?php echo $hide_remove_button; ?>"><a href="#" data-title="<?php echo $label_set; ?>" class="trgrabber-media-delete"><?php echo $label_remove; ?></a></p>
<?php   
}
//End Backdrop

panel movie plugins\tr-grabber\inc\panel\movies\movies.php

function show_additional_information_meta_box() {
    global $post;

    $original_title = get_post_meta( $post->ID, TR_GRABBER_ORIGINAL_TITLE, true ) == '' ? '' : esc_textarea( get_post_meta( $post->ID, TR_GRABBER_ORIGINAL_TITLE, true ) );

    $duration = get_post_meta( $post->ID, TR_GRABBER_FIELD_RUNTIME, true ) == '' ? '' : get_post_meta( $post->ID, TR_GRABBER_FIELD_RUNTIME, true );

    $release_date = get_post_meta( $post->ID, TR_GRABBER_FIELD_DATE, true ) == '' ? '' : get_post_meta( $post->ID, TR_GRABBER_FIELD_DATE, true );

    $trailer = get_post_meta( $post->ID, TR_GRABBER_FIELD_TRAILER, true ) == '' ? '' : html_entity_decode( get_post_meta( $post->ID, TR_GRABBER_FIELD_TRAILER, true ) );

    $poster_hotlink = get_post_meta( $post->ID, TR_GRABBER_POSTER_HOTLINK, true ) == '' ? '' : get_post_meta( $post->ID, TR_GRABBER_POSTER_HOTLINK, true );

    $backdrop_hotlink = get_post_meta( $post->ID, TR_GRABBER_FIELD_BACKDROP_HOTLINK, true ) == '' ? '' : get_post_meta( $post->ID, TR_GRABBER_FIELD_BACKDROP_HOTLINK, true );

    $backdrop_id = get_post_meta( $post->ID, TR_GRABBER_FIELD_BACKDROP, true ) == '' ? '' : get_post_meta( $post->ID, TR_GRABBER_FIELD_BACKDROP, true );

    echo '<table class="tr_grabber_content form-table">
            <tbody>
                <tr>
                    <th>
                        <label><span class="dashicons dashicons-format-aside"></span>'.__('Original name', 'tr-grabber').'</label>
                    </th>
                    <td>
                        <input type="text" name="original_title" value="'.$original_title.'" placeholder="'.__('Original name', 'tr-grabber').'">
                    </td>
                </tr>

                <tr>
                    <th>
                        <label><span class="dashicons dashicons-admin-links"></span>'.__('Poster Hotlink', 'tr-grabber').'</label>
                    </th>
                    <td>
                        <input type="text" name="poster_hotlink" value="'.$poster_hotlink.'" placeholder="'.__('Poster Hotlink', 'tr-grabber').'">
                    </td>
                </tr>

                <tr>
                    <th>
                        <label><span class="dashicons dashicons-admin-links"></span>'.__('Backdrop Hotlink', 'tr-grabber').'</label>
                    </th>
                    <td>
                        <input type="text" name="backrop_hotlink" value="'.$backdrop_hotlink.'" placeholder="'.__('Backdrop Hotlink', 'tr-grabber').'">
                        <input type="hidden" name="backdrop_id" id="trgrabber_backdrop_id" value="'.$backdrop_id.'">
                    </td>
                </tr>

                <tr>
                    <th>
                        <label><span class="dashicons dashicons-clock"></span>'.__('Duration', 'tr-grabber').'</label>
                    </th>
                    <td>
                        <input type="text" name="duration" value="'.$duration.'" placeholder="'.__('Duration', 'tr-grabber').'">
                    </td>
                </tr>

                <tr>
                    <th>
                        <label><span class="dashicons dashicons-calendar-alt"></span>'.__('Release date', 'tr-grabber').'</label>
                    </th>
                    <td>
                        <input type="date" name="release_date" value="'.$release_date.'" placeholder="'.__('Release date', 'tr-grabber').'">
                    </td>
                </tr>

                <tr class="cldtrlr">
                    <th colspan="2">
                        <label><span class="dashicons dashicons-format-video"></span>'.__('Trailer', 'tr-grabber').'</label>
                    </th>
                </tr>
                <tr class="cldtrlr">
                    <td colspan="2">
                        <textarea name="trailer" placeholder="'.__('Insert code iframe here', 'tr-grabber').'">'.$trailer.'</textarea>
                    </td>
                </tr>

                <input type="hidden" name="tr_post_type" value="1">
            </tbody>
        </table>';
}

save-post-movies plugins\tr-grabber\inc\save-post-movies.php

function save_post_movies($post_id) {

    if ( $parent_id = wp_is_post_revision( $post_id ) ) 
        $post_id = $parent_id;

    $post_type = get_post_type($post_id);

    if ( "post" != $post_type ) return;

    if ( tr_grabber_type() != 1 ) return;

    if ( isset($_REQUEST['action']) and $_REQUEST['action'] == 'trash' or isset($_REQUEST['action']) and $_REQUEST['action'] == 'untrash' ) return;

    remove_action( 'save_post', 'save_post_movies' );

    if ( isset($_POST['trgrabber_id']) ) {

        $grabber = trgrabber_curl( esc_url_raw( 'https://api.themoviedb.org/3/movie/'.$_POST['trgrabber_id'].'?append_to_response=images,trailers&api_key='.TR_GRABBER_API_KEY.'&language='.TR_GRABBER_LANG ) );

        $grabber = json_decode($grabber, true);

        $grabber_credits = trgrabber_curl( esc_url_raw( 'https://api.themoviedb.org/3/movie/'.$_POST['trgrabber_id'].'/credits?api_key='.TR_GRABBER_API_KEY.'&language='.TR_GRABBER_LANG.'&append_to_response=images,trailers' ) );

        $grabber_credits = json_decode($grabber_credits, true);

        wp_update_post( array( 'ID' => $post_id, 'post_status' => TR_GRABBER_POST_STATUS, 'post_title' => isset($grabber['title']) ? stripslashes($grabber['title']) : '', 'post_name' => isset($grabber['title']) ? stripslashes($grabber['title']) : '', 'post_content' => isset($grabber['overview']) ? stripslashes($grabber['overview']) : '' ) );

        if( isset( $grabber['trailers']['youtube'] ) ) {
            $trailer = $grabber['trailers']['youtube'];
            foreach ($trailer as $key) {
                $youtube = $key['source'];
            }
        }

        if( isset( $grabber['runtime'] ) ) {
            $hours = ltrim(gmdate("i", $grabber['runtime']), 0);
            $minutes = ltrim(gmdate("s", $grabber['runtime']), 0);

            $hours = empty($hours) ? 0 : $hours;
            $minutes = empty($minutes) ? 0 : $minutes;
        }

        if( isset( $grabber['genres'] ) ) {
            $geners_array = array();
            foreach ($grabber['genres'] as $geners) {
                $geners_array[] = $geners['name'];
            }

            wp_set_object_terms($post_id, $geners_array, 'category');
        }

        if( isset( $grabber['production_countries'] ) ) {
            $countries_array = array();
            foreach ($grabber['production_countries'] as $country) {
                $countries_array[] = $country['name'];
            }

            wp_set_object_terms($post_id, $countries_array, 'country');
        }

        if( isset( $grabber_credits['crew'] ) ) {
            $crew_array = array();
            foreach ($grabber_credits['crew'] as $crew) {
                if($crew['department']=='Directing'){ $crew_array[] = $crew['name']; }
            }

            wp_set_object_terms($post_id, $crew_array, 'directors');
        }

        if( isset( $grabber_credits['cast'] ) ) {

            $cast_array = array(); $cast_array_image = array();

            foreach ($grabber_credits['cast'] as $cast) {
                $cast_array[] = $cast['name'];
                $cast_array_image[] = $cast['profile_path'];
            }

            $term_taxonomy_ids = wp_set_object_terms($post_id, $cast_array, 'cast');

            for ($casti = 0; $casti <= count($term_taxonomy_ids)-1; $casti++) {

                $term_ex = term_exists($cast_array[$casti], 'cast');

                if (!empty($term_ex)) {

                    if($cast_array_image[$casti]!=''){
                        update_term_meta($term_taxonomy_ids[$casti], 'image_hotlink', $cast_array_image[$casti]);
                    }

                }

            }
        }

        if( TR_GRABBER_UPLOAD_IMAGES == 1 ) {

            $upload_dir = wp_upload_dir();

            if( isset( $grabber['poster_path'] ) ) {

                if ( is_rtl() ) { $title_img = md5(sanitize_title($grabber['title'])); }else{ $title_img = sanitize_title($grabber['title']); }

                copy( esc_url_raw( 'https://image.tmdb.org/t/p/original/'.$grabber['poster_path'] ), $upload_dir['path'].'/'.$title_img.'-'.$post_id.'-poster.jpg' );

                $attachment = array(
                    'guid' => $upload_dir['path'].'/'.sanitize_title($grabber['title']).'-'.$post_id.'-poster.jpg', 
                    'post_mime_type' => 'image/jpeg',
                    'post_title' => $title_img.'-'.$post_id.'-poster.jpg',
                    'post_content' => '',
                    'post_status' => 'inherit'
                );

                $attach_id = wp_insert_attachment( $attachment, $upload_dir['path'].'/'.$title_img.'-'.$post_id.'-poster.jpg',$post_id);

                require_once(ABSPATH . 'wp-admin/includes/image.php');

                $attach_data = wp_generate_attachment_metadata( $attach_id, $upload_dir['path'].'/'.$title_img.'-'.$post_id.'-poster.jpg' );
                wp_update_attachment_metadata( $attach_id, $attach_data );

                set_post_thumbnail($post_id, $attach_id);

            }

            if( isset( $grabber['backdrop_path'] ) ) {

                if ( is_rtl() ) { $title_backdrop = md5(sanitize_title($grabber['title'])); }else{ $title_backdrop = sanitize_title($grabber['title']); }

                copy( esc_url_raw( 'https://image.tmdb.org/t/p/original/'.$grabber['backdrop_path'] ), $upload_dir['path'].'/'.$title_backdrop.'-'.$post_id.'-backdrop.jpg' );

                $image = wp_get_image_editor( $upload_dir['path'].'/'.$title_backdrop.'-'.$post_id.'-backdrop.jpg' );

                if ( ! is_wp_error( $image ) ) {
                    $image->resize( TR_GRABBER_BACKDROP_WIDTH, TR_GRABBER_BACKDROP_HEIGHT, false );
                    $image->save( $upload_dir['path'].'/'.$title_backdrop.'-'.$post_id.'-backdrop.jpg' );
                }

                $attachment_backdrop = array(
                    'guid' => $upload_dir['path'].'/'.$title_backdrop.'-'.$post_id.'-backdrop.jpg', 
                    'post_mime_type' => 'image/jpeg',
                    'post_title' => $title_backdrop.'-'.$post_id.'-backdrop.jpg',
                    'post_content' => '',
                    'post_status' => 'inherit'
                );

                $attach_id_backdrop = wp_insert_attachment( $attachment_backdrop, $upload_dir['path'].'/'.$title_backdrop.'-'.$post_id.'-backdrop.jpg',$post_id);

                $attach_data_backdrop = wp_generate_attachment_metadata( $attach_id_backdrop, $upload_dir['path'].'/'.$title_backdrop.'-'.$post_id.'-backdrop.jpg' );
                wp_update_attachment_metadata( $attach_id_backdrop, $attach_data_backdrop );

                $backdrop_id = $attach_id_backdrop;

            }

        }

    }

 }

    add_action( 'save_post', 'save_post_movies' );
}
add_action( 'save_post', 'save_post_movies' );

Everything does not work with custom post typeenter image description here

I can not give up :(



Related Questions


Custom post type meta box field won't save

Updated September 06, 2017 18:08 PM

Displaying Meta Box Image

Updated March 29, 2015 10:02 AM

Metabox not show in categories custom post type cmb2

Updated December 13, 2017 01:08 AM

Custom meta box data not saving

Updated April 01, 2017 18:08 PM

Remove custom metaboxes from custom post type

Updated March 28, 2015 06:02 AM