Aviso sobre PHP de Metabox nonce

2

He recibido un aviso cuando intento agregar una nueva publicación en dos taxonomías diferentes, en una uso un meta box personalizado y en la otra no, sin embargo, la notificación aparece para ambos al intentar agregar un nuevo elemento. , veo esto: Aviso: Índice no definido: nombre_producto de producto en /Aplicaciones/MAMP/htdocs/site/wordpress/wp-content/themes/theme/functions.php en la línea 259 oculto un poco debajo de la franja negra superior en WP Admin

La línea 259 es:

if ( !wp_verify_nonce( $_POST['product_noncename'], plugin_basename(__FILE__) )) {

que es parte de esto:

// Save the Metabox Data
function wpt_save_product_meta($post_id, $post) {
    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times
    if ( !wp_verify_nonce( $_POST['product_noncename'], plugin_basename(__FILE__) )) {
    return $post->ID;
    }
    // Is the user allowed to edit the post or page?
    if ( !current_user_can( 'edit_post', $post->ID ))
        return $post->ID;
    // OK, we're authenticated: we need to find and save the data
    // We'll put it into an array to make it easier to loop though.
    $product_meta['_location'] = $_POST['_location'];
    $product_meta['_asiaLoc'] = $_POST['_asiaLoc'];
    $product_meta['_europeLoc'] = $_POST['_europeLoc'];
    $product_meta['_isbn'] = $_POST['_isbn'];
    $product_meta['_orderform'] = $_POST['_orderform'];
     // Add values of $events_meta as custom fields
    foreach ($product_meta as $key => $value) { // Cycle through the $events_meta array!
        if( $post->post_type == 'revision' ) return; // Don't store custom data twice
        $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
        if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
            update_post_meta($post->ID, $key, $value);
        } else { // If the custom field doesn't have a value
            add_post_meta($post->ID, $key, $value);
        }
        if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
    }
}
add_action('save_post', 'wpt_save_product_meta', 1, 2); // save the custom fields
    
pregunta Beto 03.04.2012 - 09:14

3 respuestas

2
// Save the Metabox Data
function wpt_save_product_meta($post_id, $post) {

    if($post->post_type!='post type with metabox') {
       return $post->ID;
    }

    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times
    if ( !wp_verify_nonce( $_POST['product_noncename'], plugin_basename(__FILE__) )) {
    return $post->ID;
    }
    // Is the user allowed to edit the post or page?
    if ( !current_user_can( 'edit_post', $post->ID ))
        return $post->ID;
    // OK, we're authenticated: we need to find and save the data
    // We'll put it into an array to make it easier to loop though.
    $product_meta['_location'] = $_POST['_location'];
    $product_meta['_asiaLoc'] = $_POST['_asiaLoc'];
    $product_meta['_europeLoc'] = $_POST['_europeLoc'];
    $product_meta['_isbn'] = $_POST['_isbn'];
    $product_meta['_orderform'] = $_POST['_orderform'];
     // Add values of $events_meta as custom fields
    foreach ($product_meta as $key => $value) { // Cycle through the $events_meta array!
        if( $post->post_type == 'revision' ) return; // Don't store custom data twice
        $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
        if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
            update_post_meta($post->ID, $key, $value);
        } else { // If the custom field doesn't have a value
            add_post_meta($post->ID, $key, $value);
        }
        if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
    }
}
add_action('save_post', 'wpt_save_product_meta', 1, 2); // save the custom fields
    
respondido por el Rajeev Vyas 03.04.2012 - 09:25
6

Compruebe simplemente si $_POST['product_noncename'] está configurado primero, así que cambie esta línea:

if ( !wp_verify_nonce( $_POST['product_noncename'], plugin_basename(__FILE__) )) {

a esto:

if ( !isset($_POST['product_noncename']) || !wp_verify_nonce( $_POST['product_noncename'], plugin_basename(__FILE__) )) {
    
respondido por el Bainternet 03.04.2012 - 11:04
0

A partir de la respuesta de Bainternet, esto me funcionó:

if ( !isset($_POST['product_noncename']) || !wp_verify_nonce( $_POST['product_noncename'], basename(__FILE__) )) {

Observe la diferencia en plugin_basename vs basename.

    
respondido por el Luke Keller 19.07.2013 - 05:43

Lea otras preguntas en las etiquetas