webentwicklung-frage-antwort-db.com.de

Warum werden meine Metaboxdaten nicht gespeichert?

Ich habe einen benutzerdefinierten Post mit vielen Meta-Boxen. Ich habe kürzlich versucht, einige dynamische Metaboxen hinzuzufügen, indem ich diese Frage verwendete: Weitere Meta-Boxen nach Bedarf erstellen

Ich habe es geschafft, die Metaboxen gut hinzuzufügen, die Abfrage funktioniert, alles funktioniert außer der Aktion save_post - die "Reviews" werden gut gespeichert, aber die "Screenings" nicht. Was habe ich in meinem Code falsch gemacht? Ich bin sicher, es muss etwas sehr Einfaches sein, das ich übersehen habe, vielleicht mit den Nonces?

Bearbeiten : Ich weiß, dass ich all dies mit einem save_post machen kann, und das funktioniert, wenn ich nur einen dieser dynamischen Metaboxen habe, aber wenn ich einen zweiten (oder mehrere) dynamischen Metaboxenabschnitt hinzufüge, tun diese Daten dies nicht nicht speichern.

Bearbeiten : Bei Bedarf kann ich meine gesamte custom-posts.php-Funktionsdatei in Pastebin oder so etwas posten, aber ich habe nur das aufgenommen, was meiner Meinung nach der Kürze halber das relevante Snippet ist

Bearbeiten : Code aktualisiert ...

<?php
add_action('save_post', 'save_postdata'); // saves post data from another function earlier on

add_action('save_post', 'save_postdata_dynamic_reviews_metabox' );  
add_meta_box("film-reviews", "Reviews", "print_dynamic_reviews_metabox", "film", "normal", "low");

add_action('save_post', 'save_postdata_dynamic_screenings_metabox' );
add_meta_box("film-screenings", "Screenings", "print_dynamic_screenings_metabox", "film", "normal", "low");

/* Prints the box content */
function print_dynamic_reviews_metabox() {
    global $post;
    // Use nonce for verification

        echo '<input type="hidden" name="reviews_noncename" id="reviews_noncename" value="' . wp_create_nonce( 'reviews-nonce' ) . '" />';
        echo '<div id="meta_inner-reviews">';
        echo '<ol id="reviews-meta">';
    $reviews = get_post_meta($post->ID,'reviews',true); //get any previously saved meta as an array so we can display it
    // print_r($reviews);
    $c = 0;
    if( is_array($reviews) ) {
      foreach($reviews as $review ) {
                if (isset($review['review-name']) || isset($review['review-link']) ) {
          echo '
                        <li><span class="remove-review" title="Delete">Remove</span>
                       <label><strong>Review Name/Title:</strong> <input type="text" class="meta-review-name saveddata" name="reviews['.$c.'][review-name]" value="'.$review['review-name'].'" /></label>
                         <label><strong>Review URL:</strong> (don\'t forget the http://) <input type="text" class="meta-review-url saveddata" name="reviews['.$c.'][review-url]" value="'.$review['review-url'].'" /></label>';
                    echo '<span class="remove-review" title="Delete">Remove</span></li>';
          $c = $c +1;
          } // ends if isset $award[album]
        } // ends foreach
            } // ends if (is_array)
        echo '</ol>';
    echo '<span class="add-review">Add New Review</span>';
        ?>
            <script>
                var $ =jQuery.noConflict();
                $(document).ready(function() {
                    var count = <?php echo $c; ?>;
                    $(".add-review").click(function() {
                        count = count + 1;
                        $('#reviews-meta').append('<li><span class="remove-review" title="Delete">Remove</span> <label><strong>Review Name/Title:</strong> <input type="text" class="meta-review-name" name="reviews['+count+'][review-name]" value="" /></label> <label><strong>Review URL:</strong> (don\'t forget the http://) <input type="text" class="meta-review-url" name="reviews['+count+'][review-url]" value="" /></label></li> <span class="remove-review" title="Delete">Remove</span>');
                        return false;
                    });
                    $(".remove-review").live('click', function() {
                        $(this).parent().remove();
                    });
                });
              </script>

     <?php
     echo '</div>'; // ends div#meta_inner
} // ends function print_dynamic_reviews_metabox()


function save_postdata_dynamic_reviews_metabox( $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;
        }
    // Check permissions
    if ( 'page' == $_POST['post_type'] ) { if ( !current_user_can( 'edit_page', $post_id )) { return $post_id; }}
    elseif ( !current_user_can( 'edit_post', $post_id )) { return $post_id;}

  // verify this came from the our screen and with proper authorization,
  // because save_post can be triggered at other times
    if (isset($_POST['reviews_noncename'])){
        if ( !wp_verify_nonce( $_POST['reviews_noncename'], 'reviews-nonce' ) )
            return;
    }else{return;}

    // OK, we're authenticated: we need to find and save the data
    $reviews = $_POST['reviews'];
    update_post_meta($post_id,'reviews',$reviews);
    } // ends function save_postdata_dynamic_reviews_metabox



    /* Prints the box content */
function print_dynamic_screenings_metabox() {
global $post;
// Use nonce for verification

    echo '<input type="hidden" name="screenings_noncename" id="screenings_noncename" value="' . wp_create_nonce( 'screenings-nonce' ) . '" />';
    echo '<div id="meta_inner-screenings">';
    echo '<ol id="screenings-meta">';
$screenings= get_post_meta($post->ID,'screenings',true); //get any previously saved meta as an array so we can display it
// print_r($screenings);
$c = 0;
if( is_array($screenings) ) {
  foreach($screenings as $screening ) {
            if (isset($screening['screening-festival-name']) || isset($screening['screening-festival-date']) ) {
      echo '
                    <li><span class="remove-screening" title="Delete">Remove</span>
                   <label><strong>Festival Name:</strong> <input type="text" class="meta-screening-festival-name saveddata" name="screenings['.$c.'][screening-festival-name]" value="'.$screening['screening-festival-name'].'" /></label>
                     <label><strong>Festival Date:</strong> <input type="text" class="meta-screening-festival-date saveddata" name="screenings['.$c.'][screening-festival-date]" value="'.$screening['screening-festival-date'].'" /></label>';
                echo '<span class="remove-screening" title="Delete">Remove</span></li>';
      $c = $c +1;
      } // ends if isset $award[album]
    } // ends foreach
        } // ends if (is_array)
    echo '</ol>';
echo '<span class="add-screening">Add New Screening</span>';
    ?>
        <script>
            var $ =jQuery.noConflict();
            $(document).ready(function() {
                var count = <?php echo $c; ?>;
                $(".add-screening").click(function() {
                    count = count + 1;
                    $('#screenings-meta').append('<li><span class="remove-screening" title="Delete">Remove</span> <label><strong>Festival Name:</strong> <input type="text" class="meta-screening-festival-name" name="screenings['+count+'][screening-festival-name]" value="" /></label> <label><strong>Festival Date:</strong> <input type="text" class="meta-screening-festival-date" name="screenings['+count+'][screening-festival-date]" value="" /></label> <span class="remove-screening" title="Delete">Remove</span>');
                    return false;
                });
                $(".remove-screening").live('click', function() {
                    $(this).parent().remove();
                });
            });
          </script>

 <?php
 echo '</div>'; // ends div#meta_inner
} // ends function print_dynamic_screenings_metabox()



function save_postdata_dynamic_screenings_metabox( $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;
        }
    // Check permissions
    if ( 'page' == $_POST['post_type'] ) { if ( !current_user_can( 'edit_page', $post_id )) { return $post_id; }}
    elseif ( !current_user_can( 'edit_post', $post_id )) { return $post_id;}

  // verify this came from the our screen and with proper authorization,
  // because save_post can be triggered at other times
    if (isset($_POST['screenings_noncename'])){
        if ( !wp_verify_nonce( $_POST['screenings_noncename'], 'screenings-nonce' ) )
            return;
    }else{return;}

    // OK, we're authenticated: we need to find and save the data
    $screenings= $_POST['screenings'];
    update_post_meta($post_id,'screenings',$screenings);
    } // ends function save_postdata_dynamic_screenings_metabox


?>
5
Amanda

Im Folgenden sind die Probleme aufgeführt, die verhindert haben, dass die Metawerte in beiden Speicherfunktionen gespeichert werden.

1. Falsche post_type Überprüfung. Sie überprüfen, ob der Post-Typ in Ihrer Speicherfunktion "Seite" ist, während Ihre Meta-Boxen auf Ihrem benutzerdefinierten Post-Typ "Film" angezeigt werden.

Dein Code:

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

Es sollte sein:

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

2. Falscher Nonce-Feldname. @tollmanz hat darauf hingewiesen, dass Sie beim Überprüfen von Nonces falsche Feldnamen überprüfen. Der Feldname in den Speicherfunktionen sollte mit dem Namen des Eingabefelds in den Druck-Meta-Box-Funktionen übereinstimmen.

Dein Code:

if (isset($_POST['reviews-nonce'])){
    if ( !wp_verify_nonce( $_POST['reviews-nonce'], 'reviews-nonce' ) )
        return;
}else{return;}

Es sollte sein:

if (isset($_POST['reviews_noncename'])){
    if ( !wp_verify_nonce( $_POST['reviews_noncename'], 'reviews-nonce' ) )
        return;
}else{return;}

Zur Überprüfung Ihres Codes:

if (isset($_POST['screenings-nonce'])){
    if ( !wp_verify_nonce( $_POST['screenings-nonce'], 'screenings-nonce' ) )
        return;
}else{return;}

Es sollte sein:

if (isset($_POST['screenings_noncename'])){
    if ( !wp_verify_nonce( $_POST['screenings_noncename'], 'screenings-nonce' ) )
        return;
}else{return;}

3. Speichern Sie die Meta-Werte falsch. Sie haben update_post_meta zum Speichern der Meta-Werte verwendet.

4. Falsch bekommen Meta-Werte. Sie haben in Ihren get_post_meta-Aufrufen den dritten Parameter verwendet, der angeben soll, dass nur ein einziger Meta-Wert abgerufen werden soll. In Ihrem Fall müssen Sie alle Meta-Werte abrufen.

Dein Code:

$reviews = get_post_meta($post->ID,'reviews',true); //get any previously saved meta as an array so we can display it

Es sollte sein:

$reviews = get_post_meta($post->ID,'reviews'); //get any previously saved meta as an array so we can display it

Ihr Code für Screenings:

$reviews = get_post_meta($post->ID,'screenings',true); //get any previously saved meta as an array so we can display it

Es sollte sein:

$reviews = get_post_meta($post->ID,'screenings',true); //get any previously saved meta as an array so we can display it

Unten ist der richtige Code. Der folgende Code sollte für Sie funktionieren, da ich ihn getestet habe und er gut funktioniert hat. Wenn es aus irgendeinem Grund nicht funktioniert, muss ich mir Ihren vollständigen Code ansehen.

add_action('save_post', 'save_postdata_dynamic_reviews_metabox' );  
add_meta_box("film-reviews", "Reviews", "print_dynamic_reviews_metabox", "film", "normal", "low");

add_action('save_post', 'save_postdata_dynamic_screenings_metabox' );
add_meta_box("film-screenings", "Reviews", "print_dynamic_screenings_metabox", "film", "normal", "low");
/* Prints the box content */
function print_dynamic_reviews_metabox() {
    global $post;
    // Use nonce for verification

        echo '<input type="hidden" name="reviews_noncename" id="reviews_noncename" value="' . wp_create_nonce( 'reviews-nonce' ) . '" />';
        echo '<div id="meta_inner-reviews">';
        echo '<ol id="reviews-meta">';
    $reviews = get_post_meta($post->ID,'reviews'); //get any previously saved meta as an array so we can display it
    // print_r($reviews);
    $c = 0;
    if( is_array($reviews) ) {
      foreach($reviews as $review ) {
                if (isset($review['review-name']) || isset($review['review-link']) ) {
          echo '
                        <li><span class="remove-review" title="Delete">Remove</span>
                       <label><strong>Review Name/Title:</strong> <input type="text" class="meta-review-name saveddata" name="reviews['.$c.'][review-name]" value="'.$review['review-name'].'" /></label>
                         <label><strong>Review URL:</strong> (don\'t forget the http://) <input type="text" class="meta-review-url saveddata" name="reviews['.$c.'][review-url]" value="'.$review['review-url'].'" /></label>';
                    echo '<span class="remove-review" title="Delete">Remove</span></li>';
          $c = $c +1;
          } // ends if isset $award[album]
        } // ends foreach
            } // ends if (is_array)
        echo '</ol>';
    echo '<span class="add-review">Add New Review</span>';
        ?>
            <script>
                var $ =jQuery.noConflict();
                $(document).ready(function() {
                    var count = <?php echo $c; ?>;
                    $(".add-review").click(function() {
                        count = count + 1;
                        $('#reviews-meta').append('<li><span class="remove-review" title="Delete">Remove</span> <label><strong>Review Name/Title:</strong> <input type="text" class="meta-review-name" name="reviews['+count+'][review-name]" value="" /></label> <label><strong>Review URL:</strong> (don\'t forget the http://) <input type="text" class="meta-review-url" name="reviews['+count+'][review-url]" value="" /></label></li> <span class="remove-review" title="Delete">Remove</span>');
                        return false;
                    });
                    $(".remove-review").live('click', function() {
                        $(this).parent().remove();
                    });
                });
              </script>

     <?php
     echo '</div>'; // ends div#meta_inner
} // ends function print_dynamic_reviews_metabox()


function save_postdata_dynamic_reviews_metabox( $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;
        }

    // Check permissions
    if ( 'film' == $_POST['post_type'] ) { if ( !current_user_can( 'edit_page', $post_id )) { return $post_id; }}
    elseif ( !current_user_can( 'edit_post', $post_id )) { return $post_id;}

  // verify this came from the our screen and with proper authorization,
  // because save_post can be triggered at other times
    if (isset($_POST['reviews_noncename'])){
        if ( !wp_verify_nonce( $_POST['reviews_noncename'], 'reviews-nonce' ) )
            return;
    }else{return;}

    delete_post_meta( $post_id, 'reviews' );
    // OK, we're authenticated: we need to find and save the data
    $reviews = $_POST['reviews'];
    foreach ( $reviews as $review ) {
        add_post_meta($post_id,'reviews',$review);
    }
} // ends function save_postdata_dynamic_reviews_metabox


/* Prints the box content */
function print_dynamic_screenings_metabox() {
global $post;
// Use nonce for verification

    echo '<input type="hidden" name="screenings_noncename" id="screenings_noncename" value="' . wp_create_nonce( 'screenings-nonce' ) . '" />';
    echo '<div id="meta_inner-screenings">';
    echo '<ol id="screenings-meta">';
$reviews = get_post_meta($post->ID,'screenings'); //get any previously saved meta as an array so we can display it
// print_r($reviews);
$c = 0;
if( is_array($screenings) ) {
  foreach($screenings as $screening ) {
            if (isset($screening['screening-festival-name']) || isset($screening['screening-festival-date']) ) {
      echo '
                    <li><span class="remove-screening" title="Delete">Remove</span>
                   <label><strong>Festival Name:</strong> <input type="text" class="meta-screening-festival-name saveddata" name="screenings['.$c.'][screening-festival-name]" value="'.$screening['screening-festival-name'].'" /></label>
                     <label><strong>Festival Date:</strong> <input type="text" class="meta-screening-festival-date saveddata" name="screenings['.$c.'][screening-festival-date]" value="'.$screening['screening-festival-date'].'" /></label>';
                echo '<span class="remove-screening" title="Delete">Remove</span></li>';
      $c = $c +1;
      } // ends if isset $award[album]
    } // ends foreach
        } // ends if (is_array)
    echo '</ol>';
echo '<span class="add-screening">Add New Screening</span>';
    ?>
        <script>
            var $ =jQuery.noConflict();
            $(document).ready(function() {
                var count = <?php echo $c; ?>;
                $(".add-screening").click(function() {
                    count = count + 1;
                    $('#screenings-meta').append('<li><span class="remove-screening" title="Delete">Remove</span> <label><strong>Festival Name:</strong> <input type="text" class="meta-screening-festival-name" name="screenings['+count+'][screening-festival-name]" value="" /></label> <label><strong>Festival Date:</strong> <input type="text" class="meta-screening-festival-date" name="screenings['+count+'][screening-festival-date]" value="" /></label> <span class="remove-screening" title="Delete">Remove</span>');
                    return false;
                });
                $(".remove-screening").live('click', function() {
                    $(this).parent().remove();
                });
            });
          </script>

 <?php
 echo '</div>'; // ends div#meta_inner
} // ends function print_dynamic_screenings_metabox()


function save_postdata_dynamic_screenings_metabox( $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;
        }
    // Check permissions
    if ( 'film' == $_POST['post_type'] ) { if ( !current_user_can( 'edit_page', $post_id )) { return $post_id; }}
    elseif ( !current_user_can( 'edit_post', $post_id )) { return $post_id;}

  // verify this came from the our screen and with proper authorization,
  // because save_post can be triggered at other times
    if (isset($_POST['screenings_noncename'])){
        if ( !wp_verify_nonce( $_POST['screenings_noncename'], 'screenings-nonce' ) )
            return;
    }else{return;}

    delete_post_meta( $post_id, 'screenings' );
    // OK, we're authenticated: we need to find and save the data
    $screenings = $_POST['screenings'];
    foreach ( $screenings as $screening ) {
        add_post_meta($post_id,'screenings',$screening);
    }
} // ends function save_postdata_dynamic_reviews_metabox
5

Ich glaube, das Problem liegt im folgenden Code:

if (isset($_POST['reviews-nonce'])){
    if ( !wp_verify_nonce( $_POST['reviews-nonce'], 'reviews-nonce' ) )
        return;
}else{return;}

Gute Arbeit, die nach dem Nonce sucht; Der Nonce-Name ist jedoch falsch. Im HTML-Formular geben Sie Folgendes an:

echo '<input type="hidden" name="reviews_noncename" id="reviews_noncename" value="' . wp_create_nonce( 'reviews-nonce' ) . '" />';

Der Name des Nonce-Felds lautet reviews_noncename not reviews-nonce. Ich glaube, Sie können den Code in der Speicherfunktion folgendermaßen korrigieren:

if (isset($_POST['reviews_noncename'])){
    if ( !wp_verify_nonce( $_POST['reviews_noncename'], 'reviews-nonce' ) )
        return;
}else{return;}

Ich denke, das wird Ihr Problem beheben.

3
tollmanz