/ / Nagłówek już wysłany - php, wordpress, pole

Nagłówek już wysłany - php, wordpress, pole

Cześć, mam ten kod, aby utworzyć niestandardowe, powtarzalne pole ina metabox na ekranie edycji tekstu Wordpress.

Działa idealnie, ale kiedy usuwam wszystkie pola za pomocą przycisku „usuń”, który znajduje się w funkcji jquery i zapisuję post, pojawia się następujący błąd:

    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

Mój kod wygląda tak w pliku mimo-media.php, błąd w linii 104 znajduje się na końcu pliku, gdzie kończy się? ¿? ¿:

Wydaje się, że gdy pole jest puste, wyświetla błąd, ale nie po pierwszym otwarciu posta (a pole jest puste) tylko podczas zapisywania go po usunięciu wszystkich pól.

Dziękuję bardzo za uwagę.

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);
}

Odpowiedzi:

0 dla odpowiedzi № 1

Co chcesz zrobić, gdy nie jest ustawiony lub jest pusty?

if(isset($_POST["medias"]) && !empty($_POST["medias"])){
$medias = $_POST["medias"];
update_post_meta($post_id,"medias",$medias);
} else {
//no $medis so do what?
}

0 dla odpowiedzi nr 2

Najprawdopodobniej $ medias nie jest zdefiniowany, gdy uderza w linię 104. Albo utwórz $ medias jako pustą zmienną na początku funkcji dynamic_save_postdata (), albo umieść update_post_meta w nawiasach tutaj:

if($_POST["medias"]){
$medias = $_POST["medias"];
update_post_meta($post_id,"medias",$medias);
}