Php 标题已发送
嗨,我有这段代码在wordpress后期编辑屏幕的metabox中创建一个自定义可重复字段 工作正常,但当我使用jquery函数中的“删除”按钮删除所有字段并保存帖子时,会出现以下错误:Php 标题已发送,php,wordpress,field,Php,Wordpress,Field,嗨,我有这段代码在wordpress后期编辑屏幕的metabox中创建一个自定义可重复字段 工作正常,但当我使用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
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);
}
add_操作('add_meta_box','dynamic_add_custom_box');
/*对输入的数据进行处理*/
添加操作('save_post','dynamic_save_postdata');
/*在Post和页面编辑屏幕的主列中添加一个框*/
函数动态添加自定义框(){
添加元框(
“动态_节ID”,
__(“添加媒体(图像和Vimeo或Youtube视频URL”,“一个”),
“mimo_自定义_媒体”,
"职位";;
}
/*打印框中的内容*/
函数mimo_自定义_媒体(){
全球$员额;
//使用nonce进行验证
wp_nonce_字段(plugin_basename(uu FILE_uuu),'dynamicMeta_noncename');
?>
很可能$medias在第104行没有定义。在函数dynamic_save_postdata()的开头创建$medias作为空变量,或者将update_post_meta放在括号中:
if($_POST['medias']){
$medias = $_POST['medias'];
update_post_meta($post_id,'medias',$medias);
}
当它未设置或为空时,您希望发生什么
if(isset($_POST['medias']) && !empty($_POST['medias'])){
$medias = $_POST['medias'];
update_post_meta($post_id,'medias',$medias);
} else {
//no $medis so do what?
}
嗨,Abracadver,我尝试过这个,但是得到了相同的错误。当没有媒体时,我是否必须在else中包含一些内容?谢谢你,你没有发布104行代码,这就是错误所在,所以我假设这是唯一的$u POST['medias']。你添加了isset(),并且没有其他对$u POST['medias'的引用在代码中,Hi最终解决了它,包括else中的var声明:'if(isset($POST['medias')和&!empty($POST['medias')){$medias=$POST['medias'];update_POST_meta($POST_id,'medias',$medias);}else{$medias='';update POST__meta if(isset($POST id,'medias',medias')和}empty($POST['medias']{
与if(!empty($\u POST['medias']]){
相同,因此不需要isset
部分。嗨,艾恩伯,这个解决方案给了我同样的错误?非常感谢