Wordpress-无法保存来自多个metabox的值

时间:2018-12-05 21:00:18

标签: wordpress

有人可以帮助我吗?我无法保存第二个元框(rip_editor_gallery)的值。第一个框的值已正确保存, 但该图像不起作用。上载按钮正在工作,出现了图像,但是当我更新时,图像丢失了。我附上了代码,我真的不知道该如何解决。

这是我的两个Metabox:

add_action( 'add_meta_boxes', 'rip_add_custom_meta_box' );
function rip_add_custom_meta_box() {
    add_meta_box(
    'rip_editor',            
    'Portfolio Meta Box',         
    'rip_editor_content',        
    'rip_portfolio'            
    );

    add_meta_box(
        'rip_gallery_metabox',
        'Galerie',
        'rip_editor_gallery',
        'rip_portfolio'
    );
}

第一个Metabox正在运行,值被保存:

function rip_editor_content( $post ) {

    wp_nonce_field( 'rip_save_meta_box_data', 'rip_meta_box_nonce' );

    $output = '';

    $output = '<p>
                <label for="rip_subline">Subline</label>
                <input type="text" name="rip_subline" value="' . esc_html( get_post_meta( $post->ID, 'rip_subline', true ) ) . '" />
            </p>
            <p>
                <label for="rip_infos">Weitere Infos</label>
                <textarea name="rip_infos" rows="5">' . esc_html( get_post_meta( $post->ID, 'rip_infos', true ) ) . '</textarea>
            </p>
            <p>
                <label for="rip_button_text">Button Text</label>
                <input type="text" name="rip_button_text" value="' . esc_html( get_post_meta( $post->ID, 'rip_button_text', true ) ) . '" />
            </p>
            <p>
                <label for="rip_button_link">Button Link</label>
                <input type="text" name="rip_button_link" value="' . esc_html( get_post_meta( $post->ID, 'rip_button_link', true ) ) . '" />
            </p>';
    echo $output;
}

这使问题产生。单击更新按钮后,图像丢失。

function rip_editor_gallery ($post) {

    $rip_featured_image = ( isset( $meta['rip_featured_image'][0] ) ) ? $meta['rip_featured_image'][0] : '';
    $rip_gallery = ( isset( $meta['rip_gallery'][0] ) ) ? $meta['rip_gallery'][0] : '';
    ?>
    <p>
        <label for="rip_featured_image">Bild</label>
        <span class="uploaded_image">
        <?php if ( '' !== $rip_featured_image ) : ?>
            <img src="<?php echo esc_url( $rip_featured_image ); ?>" />
        <?php endif; ?>
        </span>
        <input type="text" name="rip_featured_image" value="<?php echo esc_url( $rip_featured_image ); ?>" class="featured_image_upload">
        <input type="button" name="image_upload" value="Hochladen" class="button upload_image_button">
        <input type="button" name="remove_image_upload" value="Entfernen" class="button remove_image_button">
    </p>
    <?php
}

这就是我保存值的方式:我试图回显测试,即使它没有出现,所以变量是否为空?我不知道...

    add_action( 'save_post', 'rip_save' );
function rip_save( $post_id ) {

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

    if ( ! isset( $_POST['rip_meta_box_nonce'] ) ) {
        return;
    }

    if ( ! wp_verify_nonce( $_POST['rip_meta_box_nonce'], 'rip_save_meta_box_data' ) ) {
        return;
    }

    if ( 'ritec_portfolio' == $_POST['post_type'] ) {
        if ( !current_user_can( 'edit_page', $post_id ) || !current_user_can( 'edit_post', $post_id ) ) {
            return;
            }
    }

    if( isset( $_POST['rip_subline']) ) {
        update_post_meta( $post_id, 'rip_subline', $_POST['rip_subline'] );
        }

    if( isset( $_POST['rip_infos']) ) {
        update_post_meta( $post_id, 'rip_infos', $_POST['rip_infos'] );
        }
    if( isset( $_POST['rip_button_text']) ) {
        update_post_meta( $post_id, 'rip_button_text', $_POST['rip_button_text'] );
        }

    if( isset( $_POST['rip_button_link']) ) {
        update_post_meta( $post_id, 'rip_button_link', $_POST['rip_button_link'] );
        }

        if ( isset( $_POST['rip_featured_image'] ) ) { // Input var okay.
            echo 'test';
            update_post_meta( $post_id, 'rip_featured_image', sanitize_text_field( wp_unslash( $_POST['rip_featured_image'] ) ) ); 
        }
}

非常感谢:)

0 个答案:

没有答案