I have a meta-box for a cpt and can't get the values from the form to update_post_meta
cpt declaration:
function hgod_fichas()
{
$labels = array( … );
$args = array(
'label' => __('Fichas', 'hgodinho'),
'description' => __('Fichamento', 'hgodinho'),
'labels' => $labels,
'supports' => array('title', 'editor', 'thumbnail', 'revisions'),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => 'fichas',
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'show_in_rest' => true,
);
register_post_type('hgod_fichas', $args);
}
meta-box declaration:
function hgod_add_metabox($post_type)
{
$post_type = get_post_type();
add_meta_box(
$post_type . '_metabox',
__('Ficha', 'hgod'),
$post_type . '_render',
$post_type,
);
}
meta-box callback:
function hgod_fichas_render( $post )
{
$mb = get_post_meta($post->ID);
wp_nonce_field( basename( __FILE__ ), '_hgod_fichas_mb_nonce');
?>
<p>
<label for="ficha_tipo">Tipo:
</label>
<select name="ficha_tipo" id="ficha_tipo">
<option value="none" selected disable hidden>Selecione o…</option>
<option value="livro">livro</option>
<option value="texto">texto</option>
<option value="webpage">webpage</option>
<option value="imagem">imagem</option>
<option value="video">video</option>
<option value="audio">audio</option>
<option value="codigo">codigo</option>
</select>
</p>
…
}
Save function
the problem relies here, I cant get the values from nounce neither the select field, or any other input value on my hgod_fichas_render()
function, so I get returned on the very first conditional of this function:
function hgod_save_ficha($post_id){
if ( ! isset( $_POST['_hgod_fichas_mb_nonce'] ) ) {
return; // RETURN HERE
}
if ( ! wp_verify_nonce( $_POST['_hgod_fichas_mb_nonce'], '_hgod_fichas_mb_nonce' ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( ! current_user_can( 'edit_hgod_fichas', $post_id ) ) {
return;
}
$tipo_ficha = $_POST['ficha_tipo'];
$update_ficha = update_post_meta($post_id, '_hgod_ficha_livro_autor', $tipo_ficha);
if ( !$update_ficha ){
wp_die('nao salvou' );
}
}
Can anyone throw a light on this?
I have a meta-box for a cpt and can't get the values from the form to update_post_meta
cpt declaration:
function hgod_fichas()
{
$labels = array( … );
$args = array(
'label' => __('Fichas', 'hgodinho'),
'description' => __('Fichamento', 'hgodinho'),
'labels' => $labels,
'supports' => array('title', 'editor', 'thumbnail', 'revisions'),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => 'fichas',
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'show_in_rest' => true,
);
register_post_type('hgod_fichas', $args);
}
meta-box declaration:
function hgod_add_metabox($post_type)
{
$post_type = get_post_type();
add_meta_box(
$post_type . '_metabox',
__('Ficha', 'hgod'),
$post_type . '_render',
$post_type,
);
}
meta-box callback:
function hgod_fichas_render( $post )
{
$mb = get_post_meta($post->ID);
wp_nonce_field( basename( __FILE__ ), '_hgod_fichas_mb_nonce');
?>
<p>
<label for="ficha_tipo">Tipo:
</label>
<select name="ficha_tipo" id="ficha_tipo">
<option value="none" selected disable hidden>Selecione o…</option>
<option value="livro">livro</option>
<option value="texto">texto</option>
<option value="webpage">webpage</option>
<option value="imagem">imagem</option>
<option value="video">video</option>
<option value="audio">audio</option>
<option value="codigo">codigo</option>
</select>
</p>
…
}
Save function
the problem relies here, I cant get the values from nounce neither the select field, or any other input value on my hgod_fichas_render()
function, so I get returned on the very first conditional of this function:
function hgod_save_ficha($post_id){
if ( ! isset( $_POST['_hgod_fichas_mb_nonce'] ) ) {
return; // RETURN HERE
}
if ( ! wp_verify_nonce( $_POST['_hgod_fichas_mb_nonce'], '_hgod_fichas_mb_nonce' ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( ! current_user_can( 'edit_hgod_fichas', $post_id ) ) {
return;
}
$tipo_ficha = $_POST['ficha_tipo'];
$update_ficha = update_post_meta($post_id, '_hgod_ficha_livro_autor', $tipo_ficha);
if ( !$update_ficha ){
wp_die('nao salvou' );
}
}
Can anyone throw a light on this?
Share Improve this question edited Jun 23, 2020 at 22:39 hgodinho asked Jun 23, 2020 at 21:57 hgodinhohgodinho 296 bronze badges 1- Are you checking on the navigator dev tools network section if the fields are actually being sent? – Himad Commented Jun 23, 2020 at 23:44
1 Answer
Reset to default 1I get returned on the very first conditional
Are you sure? Because it seems to me that it's likely the second one which verifies the nonce.
if ( ! wp_verify_nonce( $_POST['_hgod_fichas_mb_nonce'], '_hgod_fichas_mb_nonce' ) ) {
return;
}
But even if the first conditional is also exiting the function, you should know that the second parameter for wp_verify_nonce()
should match the first parameter that you passed to wp_nonce_field()
.
Therefore,
Because you used
wp_nonce_field( basename( __FILE__ ), '_hgod_fichas_mb_nonce');
Then you should use
wp_verify_nonce( $_POST['_hgod_fichas_mb_nonce'], basename( __FILE__ ) )
.