I campi personalizzati non vengono salvati nella versione più recente di WordPress 3.0.1
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;
}

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 eupdate_post_meta()
non saprà cosa salvare. Aggiungiglobal $post;
all'inizio della funzione. register_taxonomy()
non è nel tuo gestoreinit
e probabilmente viene chiamato troppo presto (se è un plugin, forse sei stato fortunato quando lo hai fatto infunctions.php
). Spostalo nella funzionetestimonals_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.
