I campi personalizzati non vengono salvati nella versione più recente di WordPress 3.0.1

27 ott 2010, 21:49:06
Visualizzazioni: 1.27K
Voti: 0

Questo codice funzionava correttamente qualche settimana fa e l'unica cosa che mi viene in mente è che WordPress si sia aggiornato interrompendo la funzionalità?

Ho seguito il tutorial qui per aggiungere campi personalizzati: http://net.tutsplus.com/tutorials/wordpress/rock-solid-wordpress-3-0-themes-using-custom-post-types/

Tutto funzionava bene (ho inserito circa 50 post personalizzati tutti con campi personalizzati) ma quando oggi vado a modificare, le modifiche che apporto ai campi personalizzati non vengono salvate, anche se altre modifiche funzionano normalmente.

Qualcuno sa se questo è un bug conosciuto?

Modifica: Ecco il codice, aggiunto in functions.php in una nuova installazione (tema: twentyten)

add_action('init', 'testimonials_register');
add_action("admin_init", "admin_init");
add_action('save_post', 'save_testimonial');

function testimonials_register() {
    $args = array(
        'label' => __('Testimonianze'),
        'singular_label' => __('Testimonianza'),
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'menu_position' => 5,
        'hierarchical' => false,
        'rewrite' => true,
        'supports' => array('title', 'editor', 'thumbnail','custom-fields', 'revisions', 'excerpt', 'page-attributes')
    );

    register_post_type( 'testimonials' , $args );
}

register_taxonomy( 'testimonial_project_type', array("testimonials") , array( 'hierarchical' => true, 'label' => 'Tipo Progetto', 'query_var' => true, 'rewrite' => true ) );

function admin_init(){
    add_meta_box("testimonialInfo-meta", "Opzioni Testimonianza", "meta_options_testimonial", "testimonials", "advanced", "high");
}

function meta_options_testimonial(){
    global $post;
    $custom = get_post_custom($post->ID);
    $name = $custom["name"][0];
    $position = $custom["position"][0];
    $project_url = $custom["project_url"][0];
    $website = $custom["website"][0];
    ?>
  <label for="name" style="width:90px;display:inline-block">Nome:</label> <input size="50" name="name" id="name" value="<?php echo $name; ?>" /><br>
  <label for="position" style="width:90px;display:inline-block">Posizione:</label> <input size="50" name="position" id="position" value="<?php echo $position; ?>" /><br>
  <label for="website" style="width:90px;display:inline-block">Nome Sito:</label> <input size="50" name="website" id="website" value="<?php echo $website; ?>" /><br>
  <label for="project_url" style="width:90px;display:inline-block">Slug Progetto:</label> <input size="50" name="project_url" id="project_url" value="<?php echo $project_url; ?>" /> <small>Es. 'parker-harris'</small><br>
    <?php
}

function save_testimonial(){
    global $post;
    $custom_meta_fields = array( 'project_url','name','position','website');
    foreach( $custom_meta_fields as $custom_meta_field ):
        if(isset($_POST[$custom_meta_field]) && $_POST[$custom_meta_field] != ""):
            update_post_meta($post->ID, $custom_meta_field, $_POST[$custom_meta_field]);
        endif;
    endforeach;
}

add_filter("manage_edit-testimonials_columns", "testimonials_edit_columns");  
add_action("manage_posts_custom_column",  "testimonials_custom_columns");  

function testimonials_edit_columns($columns){  
    $columns = array(  
        "cb" => "<input type=\"checkbox\" />",  
        "title" => "Titolo Testimonianza",  
        "name" => "Nome",  
        "description" => "Estratto",  
        "project_url" => "Slug Progetto"
    );  
    return $columns;  
}  

function testimonials_custom_columns($column){  
    global $post;  
    switch ($column) {  
        case "name":  
            $custom = get_post_custom();  
            echo $custom["name"][0].", ".$custom["position"][0]."<br> ".$custom["website"][0];
            break;  
        case "description":  
            the_excerpt(); 
            break;  
        case "project_url":  
            $custom = get_post_custom();  
            echo "<a target='_blank' href='/portfolio/".$custom["project_url"][0]."'>".$custom["project_url"][0]."</a>";
            break;  
    }  
}  

// Mostra le testimonianze nel blog e nel feed
add_filter( 'pre_get_posts', 'my_get_posts' );

function my_get_posts( $query ) {
    if ( is_home() && false == $query->query_vars['suppress_filters'] || is_feed() )
        $query->set( 'post_type', array( 'post', 'testimonials') );
    return $query;
}
2
Commenti

A proposito, questo NON è l'altro problema di salvataggio che è emerso da quei tutorial (il salvataggio automatico che cancella i dati) perché l'ho risolto e questo non salva affatto, non solo su un timer. Ma funzionava perfettamente solo poche settimane fa!

Shaun Shaun
27 ott 2010 23:01:25

I miei tipi di post personalizzati funzionano perfettamente. Invece di collegarci al tutorial, per favore fornisci un link al tuo codice su qualcosa come pastie.org o http://gist.github.com/ così possiamo esaminare il tuo codice.

curtismchale curtismchale
28 ott 2010 01:57:32
Tutte le risposte alla domanda 1
1

Ho inserito il tuo codice in un plugin e ho dovuto modificare due cose per farlo funzionare:

  • In save_testimonial(), usi $post ma non lo dichiari come globale. Quindi $post->ID sarà vuoto e update_post_meta() non saprà cosa salvare. Aggiungi global $post; all'inizio della funzione.
  • register_taxonomy() non è nel tuo gestore init e probabilmente viene chiamato troppo presto (se è un plugin, forse sei stato fortunato quando lo hai fatto in functions.php). Spostalo nella funzione testimonals_register().

Avevi WP_DEBUG impostato su TRUE durante il debug? In tal caso avresti dovuto vedere gli avvisi che mi hanno portato a questa soluzione.

28 ott 2010 11:31:00
Commenti

Grazie, ha funzionato! Dev'essere stato rimosso dal codice in qualche modo... :)

Shaun Shaun
28 ott 2010 11:44:58