Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have two custom post type defined ('acting' and 'voiceover') and custom met boxes defined for both of them. I'm able to save, update and delete on the acting custom post type. But if I add the voiceover custom post type then I'll be not able to save the data on the acting anymore. The two piece of code are similar but not the same. I'll need to add more field to the voiceover. After many hours spent in looking for solution and trying many things, I'm asking your help.

Acting custom meta boxes

   add_action ('admin_init','acting_meta_boxes');


    function acting_meta_boxes() {
add_meta_box( 'reels_form', 'Acting Details', 'reels_form', 'acting', 'normal', 'high' );
    }

    function reels_form() {
$post_id = get_the_ID();
$reel_data = get_post_meta( $post_id, '_reel', true );
$client_name = ( empty( $reel_data['client_name'] ) ) ? '' : $reel_data['client_name'];
$agency = ( empty( $reel_data['agency'] ) ) ? '' : $reel_data['agency'];
$production_company = ( empty( $reel_data['production_company'] ) ) ? '' : $reel_data['production_company'];
$director = ( empty( $reel_data['director'] ) ) ? '' : $reel_data['director'];
$video_id = ( empty( $reel_data['video_id'] ) ) ? '' : $reel_data['video_id'];  

wp_nonce_field( 'acting', 'acting' );
?>
<p>
    <label>YouTube video ID</label><br />
    <input type="text" value="<?php echo $video_id; ?>" name="reel[video_id]" size="20" />
</p>
<p>
    <label>Client's Name (optional)</label><br />
    <input type="text" value="<?php echo $client_name; ?>" name="reel[client_name]" size="80" />
</p>
<p>
    <label>Agency Name (optional)</label><br />
    <input type="text" value="<?php echo $agency; ?>" name="reel[agency]" size="80" />
</p>
<p>
    <label>Production Company (optional)</label><br />
    <input type="text" value="<?php echo $production_company; ?>" name="reel[production_company]" size="80" />
</p>
<p>
    <label>Director (optional)</label><br />
    <input type="text" value="<?php echo $director; ?>" name="reel[director]" size="80" />
</p>

<?php
     }

    add_action( 'save_post', 'reels_save_post', 10, 2 );

    function reels_save_post( $post_id ) {


if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
    return;

if ( ! empty( $_POST['acting'] ) && ! wp_verify_nonce( $_POST['acting'], 'acting' ) )
    return;

if ( ! empty( $_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 ( ! wp_is_post_revision( $post_id ) && 'acting' == get_post_type( $post_id ) ) {
    remove_action( 'save_post', 'reels_save_post' );

    wp_update_post( array(
        'ID' => $post_id,
    ) );

    add_action( 'wp_inset_post_data', 'reels_save_post' );
}

if ( ! empty( $_POST['acting'] ) ) {
    $reel_data['client_name'] = ( empty( $_POST['reel']['client_name'] ) ) ? '' : sanitize_text_field( $_POST['reel']['client_name'] );
    $reel_data['agency'] = ( empty( $_POST['reel']['agency'] ) ) ? '' : sanitize_text_field( $_POST['reel']['agency'] );
    $reel_data['production_company'] = ( empty( $_POST['reel']['production_company'] ) ) ? '' : sanitize_text_field( $_POST['reel']['production_company'] );
    $reel_data['director'] = ( empty( $_POST['reel']['director'] ) ) ? '' : sanitize_text_field( $_POST['reel']['director'] );
    $reel_data['video_id'] = ( empty( $_POST['reel']['video_id'] ) ) ? '' : sanitize_text_field( $_POST['reel']['video_id'] );

    update_post_meta( $post_id, '_reel', $reel_data );
} else {
    delete_post_meta( $post_id, '_reel' );
}
    }

Voiceover custom meta boxes

    add_action ('admin_init','voiceover_meta_boxes');


    function voiceover_meta_boxes() {
        add_meta_box( 'reels_voiceover_form', 'Voiceover Details', 'reels_voiceover_form', 'voiceover', 'normal', 'high' );
        }


        function reels_voiceover_form() {
        $post_id = get_the_ID();
        $reel_data = get_post_meta( $post_id, '_reel', true );
        $client_name = ( empty( $reel_data['client_name'] ) ) ? '' : $reel_data['client_name'];
        $agency = ( empty( $reel_data['agency'] ) ) ? '' : $reel_data['agency'];
        $production_company = ( empty( $reel_data['production_company'] ) ) ? '' : $reel_data['production_company'];
        $director = ( empty( $reel_data['director'] ) ) ? '' : $reel_data['director'];
        $video_id = ( empty( $reel_data['video_id'] ) ) ? '' : $reel_data['video_id'];  
        $audio_id = ( empty( $reel_data['audio_id'] ) ) ? '' : $reel_data['audio_id'];      

        wp_nonce_field( 'voiceover', 'voiceover' );
        ?>
        <p>
            <label>YouTube video ID</label><br />
            <input type="text" value="<?php echo $video_id; ?>" name="reel[video_id]" size="20" />
        </p>
        <p>
            <label>Soundcloud ID</label><br />
            <input type="text" value="<?php echo $audio_id; ?>" name="reel[audio_id]" size="20" />
        </p>
        <p>
            <label>Client's Name (optional)</label><br />
            <input type="text" value="<?php echo $client_name; ?>" name="reel[client_name]" size="80" />
        </p>
        <p>
            <label>Agency Name (optional)</label><br />
            <input type="text" value="<?php echo $agency; ?>" name="reel[agency]" size="80" />
        </p>
        <p>
            <label>Production Company (optional)</label><br />
            <input type="text" value="<?php echo $production_company; ?>" name="reel[production_company]" size="80" />
        </p>
        <p>
            <label>Director (optional)</label><br />
            <input type="text" value="<?php echo $director; ?>" name="reel[director]" size="80" />
        </p>

        <?php
    }

    add_action( 'save_post', 'reels_save_voiceover_post', 10, 2 );

    function reels_save_voiceover_post( $post_id ) {
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
            return;

        if ( ! empty( $_POST['voiceover'] ) && ! wp_verify_nonce( $_POST['voiceover'], 'voiceover' ) )
            return;

        if ( ! empty( $_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 ( ! wp_is_post_revision( $post_id ) && 'acting' == get_post_type( $post_id ) ) {
            remove_action( 'save_post', 'reels_save_voiceover_post' );

            wp_update_post( array(
                'ID' => $post_id,
            ) );

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

        if ( ! empty( $_POST['voiceover'] ) ) {
            $reel_data['client_name'] = ( empty( $_POST['reel']['client_name'] ) ) ? '' : sanitize_text_field( $_POST['reel']['client_name'] );
            $reel_data['agency'] = ( empty( $_POST['reel']['agency'] ) ) ? '' : sanitize_text_field( $_POST['reel']['agency'] );
            $reel_data['production_company'] = ( empty( $_POST['reel']['production_company'] ) ) ? '' : sanitize_text_field( $_POST['reel']['production_company'] );
            $reel_data['director'] = ( empty( $_POST['reel']['director'] ) ) ? '' : sanitize_text_field( $_POST['reel']['director'] );
            $reel_data['video_id'] = ( empty( $_POST['reel']['video_id'] ) ) ? '' : sanitize_text_field( $_POST['reel']['video_id'] );
            $reel_data['audio_id'] = ( empty( $_POST['reel']['audio_id'] ) ) ? '' : sanitize_text_field( $_POST['reel']['audio_id'] );      

            update_post_meta( $post_id, '_reel', $reel_data );
        } else {
            delete_post_meta( $post_id, '_reel' );
        }
    }

I'm running the whole thing in a multisite WP environment. I hope somebody can help me to figure it out.

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.