您好我有这个代码在wordpress后期编辑屏幕中的metabox中创建自定义可重复字段。
完美无缺,但是当我使用jquery函数中的'remove'按钮删除所有字段并保存帖子时,它会给我以下错误:
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);
}
答案 0 :(得分:0)
很可能$ medias在到达第104行时没有定义。在函数dynamic_save_postdata()的开头创建$ medias作为空变量,或者将update_post_meta放在括号中:
if($_POST['medias']){
$medias = $_POST['medias'];
update_post_meta($post_id,'medias',$medias);
}
答案 1 :(得分:0)
如果没有设置或者它是空的,你想要发生什么?
if(isset($_POST['medias']) && !empty($_POST['medias'])){
$medias = $_POST['medias'];
update_post_meta($post_id,'medias',$medias);
} else {
//no $medis so do what?
}