-1

嗨,我有这段代码可以在 wordpress 后编辑屏幕的元框中创建自定义可重复字段。

完美运行,但是当我使用 jquery 函数中的“删除”按钮删除所有字段并保存帖子时,它给了我以下错误:

    Notice: Undefined index: medias in /home1/mimo/public_html/demos/newtheme/wp-content/plugins/mimo-media/mimo-media.php on line 104

    Warning: Cannot modify header information - headers already sent by (output started at /home1/mimo/public_html/demos/newtheme/wp-content/plugins/mimo-media/mimo-media.php:104) in /home1/mimo/public_html/demos/newtheme/wp-admin/post.php on line 222

    Warning: Cannot modify header information - headers already sent by (output started at /home1/mimo/public_html/demos/newtheme/wp-content/plugins/mimo-media/mimo-media.php:104) in /home1/mimo/public_html/demos/newtheme/wp-includes/pluggable.php on line 875

我的代码在 mimo-media.php 文件中是这样的,第 104 行的错误就在文件的末尾,它在哪里结束 ?¿?¿:

似乎当该字段为空时它会给出错误,但不是第一次打开帖子(并且该字段为空),只有在删除所有字段后保存它时。

非常感谢您的关注。

add_action( 'add_meta_boxes', 'dynamic_add_custom_box' );

/* Do something with the data entered */
add_action( 'save_post', 'dynamic_save_postdata' );

/* Adds a box to the main column on the Post and Page edit screens */
function dynamic_add_custom_box() {
add_meta_box(
    'dynamic_sectionid',
    __( 'Add Media(Images and Vimeo or Youtube Videos urls', 'one' ),
    'mimo_custom_media',
    'post');
}

/* Prints the box content */
function mimo_custom_media() {
global $post;
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), 'dynamicMeta_noncename' );
?>
<div id="meta_inner">
<?php

//get the saved meta as an arry
$medias = get_post_meta($post->ID,'medias',true);

$c = 0;
if ( count( $medias ) > 0 ) {
    foreach( $medias as $track ) {
        if ( isset( $track['title'] ) ) {
            printf( '<p>Media url <input type="text" name="medias[%1$s][title]"   value="%2$s" /><span class="remove">%4$s</span></p>', $c, $track['title'], '', __( 'Remove Media' ) );
            $c = $c +1;
        }
    }
}
?>
<span id="here"></span>
<span class="add"><?php _e('Add Tracks'); ?></span>
<script>
var $ =jQuery.noConflict();
$(document).ready(function() {
    var count = <?php echo $c; ?>;
    $(".add").click(function() {
        count = count + 1;

        $('#here').append('<p> Media url <input type="text" name="medias['+count+'][title]" value="" /><span class="remove">Remove Media</span></p>' );
        return false;
    });
    $(".remove").live('click', function() {
        $(this).parent().remove();
    });
});
</script>
</div><?php

}

/* When the post is saved, saves our custom data */
function dynamic_save_postdata( $post_id ) {
// verify if this is an auto save routine. 
// If it is our form has not been submitted, so we dont want to do anything
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
    return;

// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !isset( $_POST['dynamicMeta_noncename'] ) )
    return;

if ( !wp_verify_nonce( $_POST['dynamicMeta_noncename'], plugin_basename( __FILE__ ) ) )
    return;

// OK, we're authenticated: we need to find and save the data
if($_POST['medias']){$medias = $_POST['medias'];}


update_post_meta($post_id,'medias',$medias);
}
4

2 回答 2

0

当它未设置或为空时,您希望发生什么?

if(isset($_POST['medias']) && !empty($_POST['medias'])){
    $medias = $_POST['medias'];
    update_post_meta($post_id,'medias',$medias);
} else {
    //no $medis so do what?
}
于 2013-10-11T20:15:46.057 回答
0

很可能 $medias 在到达第 104 行时没有定义。要么在函数 dynamic_save_postdata() 的开头创建 $medias 作为空变量,要么将 update_post_meta 放在括号中:

if($_POST['medias']){
    $medias = $_POST['medias'];
    update_post_meta($post_id,'medias',$medias);
}
于 2013-10-11T20:12:39.713 回答