lundi 2 novembre 2020

Hi I'm created a custom post type in wordpress, and i'm traing to add a checkbox type metabox, but I can't save it

Hi I'm created a custom post type in wordpress, and i'm traing to add a checkbox type metabox, but I can't save it, when I save or update the page, the check mark disappear. I need create a custom post were I can mark if it is "featured" or not and save this information

this is the code

<?php

add_action( 'add_meta_boxes', 'featured' );

function featured()
{
    add_meta_box( 'featuredb', 'Featured or Regular( Default Regular)', 'featured_metabox', 'building', 'normal', 'default' );
}

function featured_metabox( $post )
  {
    $values = get_post_custom( $post->ID );

    $check = isset( $values['my_meta_box_check'] ) ? esc_attr( $values['my_meta_box_check'] ) : '';
    
?>

   
    <p>
            <input type="checkbox" id="my_meta_box_check" name="my_meta_box_check" <?php checked( $check, 'on' ); ?> />
            <label for="my_meta_box_check">Featured</label>

        
</p>
<?php
}

add_action('save_post','gima_metabox_save');
add_action('publish_post','gima_metabox_save');

function gima_metabox_save( $post_id )
{
   // Bail if we're doing an auto save  
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

// if our current user can't edit this post, bail  
if( !current_user_can( 'edit_post' ) ) return;

// now we can actually save the data  
$allowed = array(
    'a' => array( // on allow a tags  
        'href' => array() // and those anchors can only have href attribute  
    )
);


if( isset( $_POST['my_meta_box_text'] ) )
$my_meta_box_text = sanitize_text_field($_POST['my_meta_box_text']);
update_post_meta( $post_id, 'my_meta_box_text', wp_kses( $_POST['my_meta_box_text'], $allowed ) );

 
$check = isset( $_POST['my_meta_box_check'] )  ? 'on' : 'off';
    update_post_meta( $post_id, 'my_meta_box_check', $check );
}

please help




Aucun commentaire:

Enregistrer un commentaire