0

I'm creating plugin and can't understand how to add custom fields like in WooCommerce - selects, inputs, textareas, etc.

I'm talking not about WP Custom fields. I've registered custom post type with register_post_type function with arguments:

'capability_type' => 'post',
'supports' => array(
    'title',
    'custom-fields'
)

And now I have only Title and Custom fields. How I can add some custom inputs/select/radiobuttons to Edit %custom_post_type% page?

  • WP doesn't include the interface to build these, you have to build them yourself using a combination of add_meta_box() and the save_post hook. Best way to get up and running quickly however is to use Advanced Custom Fields, which will do all the work for you. – brianjohnhanna Nov 4 '16 at 15:07
3

You need to add your own metabox for the custom post type created. You can use the action add_meta_boxes_{cpt_slug}

add_action( 'add_meta_boxes_' . $cpt_public_slug, 'adding_custom_meta_boxes' );
add_action( 'save_post', 'save_metabox' , 10, 2 );

function adding_custom_meta_boxes(){
    global $cpt_public_slug;
    add_meta_box(
        'plugin-site',
        __( 'Website', 'text_domain' ),
        'cpt_form_site_Render',
        $cpt_public_slug,
        'normal',
        'high'
    );
}

function cpt_form_site_Render(){
    global $post;

    $post_meta = get_post_meta($post->ID); 

    // render the input field
    ?>
     <input type="text" name="meta_key" value="<?php echo $post_meta['meta_value'][0]; ?>"/>
    <?php
    // do it for all your metas
}

function save_metabox($post_id, $post){

    foreach ($_POST as $the_posted_key=>$the_posted_value) {
        if (strpos($the_posted_key, brozzme_passport_config::$custom_fields_prefix)!==false) {
            update_post_meta($post_id, $the_posted_key, $the_posted_value);
        }
    }
}
1

Use the Advanced Custom Fields plugin to add some additional fields to your Custom Post Type. It's free in the plugin directory and has plenty of examples on the website.

Plugin Page: https://wordpress.org/plugins/advanced-custom-fields/

Website: https://www.advancedcustomfields.com/

  • It's a good idea if you developing plugin for certain website, but not for any website. – Demyd Ganenko Nov 4 '16 at 16:15
  • I think ACF is a pretty good solution for a lot of things. And it would indeed add those fields to the Edit "Custom Post Type Name" page as the poster requested. Maybe this was a thought initially, but I wouldn't know that since it was not mentioned. Obviously the other answer that was provided is fine. You could also utilize CMB2 if that's what you prefer. I am just surprised I got downvoted. – lavekyl Nov 4 '16 at 16:20
  • I'm surprised you were downvoted too. That would have been my suggestion. I upvoted. HA. – rudtek Nov 4 '16 at 17:54
1

there is working example. I used it for bbpress custom metabox creation

$cpt_public_slug = 'topic';

add_action( 'add_meta_boxes_' . $cpt_public_slug, 'adding_custom_meta_boxes' );
add_action( 'save_post', 'save_metabox' , 10, 2 );
add_action( 'publish_'.$cpt_public_slug, 'save_metabox' , 10, 2 );

function adding_custom_meta_boxes(){
    global $cpt_public_slug;
    add_meta_box('topictimeout', 'Time', 'cpt_form_site_Render', $cpt_public_slug, 'side', 'high');
}

function cpt_form_site_Render(){
    global $post;
    $post_meta = get_post_meta($post->ID); 
   // put here input or textarea 
 }

function save_metabox($post_id, $post){
    global $post;
// when first publish and then save
    if( isset( $_POST['save'] ) || isset( $_POST['publish'] ) ) {

        // you will need to use a $_POST param and validate before saving
        $meta_val = isset( $_POST['topictimeout'] ) ? sanitize_text_field( $_POST['topictimeout'] ) : '';
        // the $meta_val would be a $_POST param from inner meta box form
        update_post_meta($post_id, 'topictimeout', $meta_val);
    }

}

U can put there this input for example:

<input type="text" name="topictimeout" value="<?=$post_meta['topictimeout'][0]; ?>"/> in minutes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.