Ver Mensaje Individual
  #6 (permalink)  
Antiguo 19/09/2011, 08:02
Avatar de TMeister
TMeister
Crazy Coder
 
Fecha de Ingreso: enero-2002
Ubicación: En la Oficina
Mensajes: 2.880
Antigüedad: 22 años, 3 meses
Puntos: 193
Respuesta: Custom fields se pierden con los autosaves

Esa validación no la veo bien, ¿que pasa si quiero dejar en blanco el campo? no lo hara.

Para evitar guardar el valor de los custom fields en el autosave hago esta comprobación:

Código PHP:
Ver original
  1. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
  2.     return;

Cuando hace el autosave no hago nada..

Y para seguridad uso wp_verify_nonce en tu metabox agregas un campo

Código PHP:
Ver original
  1. wp_nonce_field( plugin_basename( __FILE__ ), 'check_field' );

Y lo compruebas:

Código PHP:
Ver original
  1. if ( !wp_verify_nonce( $_POST['check_nonce'], plugin_basename( __FILE__ ) ) )
  2.     return;

Y así la funcion para salvar los custom fields queda mas o menos así:

Código PHP:
Ver original
  1. function save_postdata($post_id)
  2.     {
  3.         if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
  4.             return;
  5.  
  6.         if ( !wp_verify_nonce( $_POST['check_field'], plugin_basename( __FILE__ ) ) )
  7.             return;
  8.        
  9.         if ( $_POST['post_type'] != "Tu_custom_post" )         
  10.             return;
  11.  
  12.         #Haces el update.....
  13. }


Saludos!!