Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Why this metaboxes dissapear if the post change to draft? WordPress

I create new custom post types on a childtheme, for this custom post types I create some metaboxes for each one. To do this just copy/paste/change the main code of the father theme. but in a new file, so I keep the father custom post types and they metaboxes.

The bug I get is when the custom post type change to draft, and is there when the info of the metaboxes, created and saved like a published post, dissapear.

Any idea?

<?php

function mercapalma_empresas_meta_boxes() {

$meta_box = array(
"empresas_puestos" => array(
"name" => "_empresas_puestos",
"title" => __("Puestos",'mercapalma'),
"description" => __("Añadir puestos separados por una linea media. Ej: 201-202-301",'mercapalma'),
"type" => "text"
),
"empresas_telefono" => array(
"name" => "_empresas_telefono",
"title" => __("Teléfono",'mercapalma'),
"description" => __("Añadir teléfono de contacto o información.",'mercapalma'),
"type" => "text"
),
"empresas_fax" => array(
"name" => "_empresas_fax",
"title" => __("Fax",'mercapalma'),
"description" => __("Añadir teléfono de fax.",'mercapalma'),
"type" => "text"
),
"empresas_productos" => array(
"name" => "_empresas_productos",
"title" => __("Productos que comercializa",'mercapalma'),
"description" => __("Introducir los productos separados por comas. Ej: Tomates, Naranjas, Patatas.",'mercapalma'),
"type" => "textarea"
),
"empresas_web" => array(
"name" => "_empresas_web",
"title" => __("Página web",'mercapalma'),
"description" => __("Por favor introducir la dirección web (URL) de la empresa. No olvidar http://",'mercapalma'),
"type" => "text"
),
"empresas_email" => array(
"name" => "_empresas_email",
"title" => __("Dirección de correo electrónico para contacto.",'mercapalma'),
"description" => __("Verificar que sea una dirección de correo válida.",'mercapalma'),
"type" => "text"
)

);

return apply_filters( 'mercapalma_empresas_meta_boxes', $meta_box );
}

function mercapalma_trabajo_meta_boxes() {

$meta_box = array(
"trabajo_empresa" => array(
"name" => "_trabajo_empresa",
"title" => __("Empresa Contratante",'mercapalma'),
"description" => __("Nombre de la empresa que oferta el puesto.",'mercapalma'),
"type" => "text"
),
"trabajo_contacto" => array(
"name" => "_trabajo_contacto",
"title" => __("Método de contacto",'mercapalma'),
"description" => __("Añadir teléfono, fax o email para contacto.",'mercapalma'),
"type" => "text"
),
"trabajo_plazas" => array(
"name" => "_trabajo_plazas",
"title" => __("Nº de Plazas",'mercapalma'),
"description" => __("Cantidad de plazas disponibles.",'mercapalma'),
"type" => "text"
),
"trabajo_web" => array(
"name" => "_trabajo_web",
"title" => __("Ficha de empresa",'mercapalma'),
"description" => __("Por favor introducir la dirección web (URL) de la ficha de la emrpesa. Ej: http://mercapalma.es/empresa/nombre-de-empresa-s-aNo olvidar http://",'mercapalma'),
"type" => "text"
)
);

return apply_filters( 'mercapalma_trabajo_meta_boxes', $meta_box );
}
function mercapalma_espacios_meta_boxes() {

$meta_box = array(
"espacios_dimensiones" => array(
"name" => "_espacios_dimensiones",
"title" => __("Medidas y formato del espacios",'mercapalma'),
"description" => __("Dimensiones en metros cuadrados, volumen o área.",'mercapalma'),
"type" => "text"
),
"espacios_contrato" => array(
"name" => "_espacios_contrato",
"title" => __("Tipo de Contrato",'mercapalma'),
"description" => __("Especificar si es alquiler, concesión, lising, venta, etc.",'mercapalma'),
"type" => "text"
)
);

return apply_filters( 'mercapalma_espacios_meta_boxes', $meta_box );
}

function empresas_meta_boxes() {
global $post;
$meta_box = mercapalma_empresas_meta_boxes(); ?>

<table class="form-table">
<?php foreach ( $meta_box as $meta ) :

$value = stripslashes( get_post_meta( $post->ID, $meta['name'], true ) );

if ( $meta['type'] == 'text' )
get_meta_text_input( $meta, $value );
elseif ( $meta['type'] == 'textarea' )
get_meta_textarea( $meta, $value );

endforeach; ?>
</table>
<?php
}
function trabajo_meta_boxes() {
global $post;
$meta_box = mercapalma_trabajo_meta_boxes(); ?>

<table class="form-table">
<?php foreach ( $meta_box as $meta ) :

$value = stripslashes( get_post_meta( $post->ID, $meta['name'], true ) );

if ( $meta['type'] == 'text' )
get_meta_text_input( $meta, $value );
elseif ( $meta['type'] == 'textarea' )
get_meta_textarea( $meta, $value );

endforeach; ?>
</table>
<?php
}
function espacios_meta_boxes() {
global $post;
$meta_box = mercapalma_espacios_meta_boxes(); ?>

<table class="form-table">
<?php foreach ( $meta_box as $meta ) :

$value = stripslashes( get_post_meta( $post->ID, $meta['name'], true ) );

if ( $meta['type'] == 'text' )
get_meta_text_input( $meta, $value );
elseif ( $meta['type'] == 'textarea' )
get_meta_textarea( $meta, $value );

endforeach; ?>
</table>
<?php
}

function mercapalma_create_meta_box() {
global $theme_name;

add_meta_box( 'empresas-meta-boxes', __('Datos de la Empresa','mercapalma'), 'empresas_meta_boxes', 'empresas', 'normal', 'high' );
add_meta_box( 'trabajo-meta-boxes', __('Datos de la oferta de trabajo','mercapalma'), 'trabajo_meta_boxes', 'trabajo', 'normal', 'high' );
add_meta_box( 'espacios-meta-boxes', __('Datos del espacio','mercapalma'), 'espacios_meta_boxes', 'espacios', 'normal', 'high' );

}

function mercapalma_save_meta_data( $post_id ) {
global $post;


if ( 'trabajo' == $_POST['post_type'] )
$meta_box = array_merge( mercapalma_trabajo_meta_boxes() );
else if ( 'espacios' == $_POST['post_type'] )
$meta_box = array_merge( mercapalma_espacios_meta_boxes() );
else
$meta_box = array_merge( mercapalma_empresas_meta_boxes() );


foreach ( $meta_box as $merca_meta_box ) :

/*
if ( !wp_verify_nonce( $_POST[$merca_meta_box['name'] . '_noncename'], plugin_basename( __FILE__ ) ) )
return $post_id;

elseif ( 'trabajo' == $_POST['post_type'] && !current_user_can( 'edit_post', $post_id ) )
return $post_id;

elseif ( 'empresas' == $_POST['post_type'] && !current_user_can( 'edit_post', $post_id ) )
return $post_id;

elseif ( 'espacios' == $_POST['post_type'] && !current_user_can( 'edit_post', $post_id ) )
return $post_id;
*/


$data = stripslashes( $_POST[$merca_meta_box['name']] );

if ( get_post_meta( $post_id, $merca_meta_box['name'] ) == '' )
add_post_meta( $post_id, $merca_meta_box['name'], $data, true );

elseif ( $data != get_post_meta( $post_id, $merca_meta_box['name'], true ) )
update_post_meta( $post_id, $merca_meta_box['name'], $data );

elseif ( $data == '' )
delete_post_meta( $post_id, $merca_meta_box['name'], get_post_meta( $post_id, $merca_meta_box['name'], true ) );

endforeach;
}


/* Add a new meta box to the admin menu. */
add_action( 'admin_menu', 'mercapalma_create_meta_box' );

/* Saves the meta box data. */
add_action( 'save_post', 'mercapalma_save_meta_data' );

?>

Answers (4)

2011-08-17

Kannan C answers:

i hope you need to use do_meta_box function

add_action( 'do_meta_boxes', 'mercapalma_create_meta_box' );


Kannan C comments:

Hey, i forgot to ask you. Are your meta boxes disappeared or the data in your metabox disappeared?
if data in your metabox disappeared, place this code inside your save function

function mercapalma_save_meta_data( $post_id ) {
global $post;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
...// other codes here
}


Pancho Pérez comments:

Just data dissapear.


Kannan C comments:

if your data disappeared after successful save then that may be of two reasons(two i know).
1. if you have auto save fn enabled, the meta data will get deleted. The below code will prevent it.

function mercapalma_save_meta_data( $post_id ) {

global $post;

if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )

return;

...// other codes here

}


2. Upon quick edit, your metabox data cannot pass along and so empty data will be saved for your meta keys. You need to use wp_nonce verification like @Christianto said. If you cannot, you can use a hidden field some where in the form to check whether it is post.php

<input type="hidden" name="custom_verification" value="123xyz" />

and place this at the beginning of your save function

if(isset($_POST['custom_verification']) == "123xyz" {
//save stuff
}
else return;

2011-08-17

Romel Apuya answers:

replace :

add_action( 'save_post', 'mercapalma_save_meta_data' );

with :

add_action( 'init', 'mercapalma_save_meta_data' );

2011-08-17

Christianto answers:

Did it happen on main theme too..?
Did you use quick edit to change the post status?

In your code you didn't use wp_verify_nonce to check if the save_post is trigger from post editor (you comment the nonce verification).
So if you make a modification on quick edit the metabox function will be run too.
And since on quick edit, your metabox fields/data isn't exists/display, it delete the value of metabox.
elseif ( $data == '' )
delete_post_meta( $post_id, $merca_meta_box['name'], get_post_meta( $post_id, $merca_meta_box['name'], true ) );

You should verify nonce first at begining of your metabox save function
function mercapalma_save_meta_data( $post_id ) {
global $post;

if ( !wp_verify_nonce( $_POST[$merca_meta_box['name'] . '_noncename'], plugin_basename( __FILE__ ) ) )
return $post_id;

// function continue..

}


And display the nonce in function to display your metabox
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), $_POST[$merca_meta_box['name'] . '_noncename' )


Pancho Pérez comments:

I´m not using the main theme, so I never tried.
I use quick edit and change some taxonomies or not comment allowed and it works.

The verify nonce is commented because return an error, I will try to make the changes and uncomment the nonce to see if works. Thanx.

2011-08-17

Jurre Hanema answers:

Modifying your function like this will probably fix this issue and other potential issues you may experience with the existing code:

function mercapalma_save_meta_data($post_id)
{
global $post;

if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;

if(empty($_POST))
return $post_id;

$post_id_old = $post_id;

$post_id = wp_is_post_revision($post_id);

if(!$post_id)
$post_id = $post_id_old;

// ... your code goes here ...
}