0

I just need the extra fields on my post but without using advance custom fields.

Please what should i do the changes in my function.php

i have simple do the below code for custom post i need on extra field which name should be "company" what changes i required in below code.

please can anyone tell me ?

add_action( 'init', 'client' );
   function client() {
    register_post_type( 'client',
        array(
            'labels' => array(
                'name' => __( 'Our Client' ),
                'singular_name' => __( 'client' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'client'),
            'supports' => array( 'title','thumbnail')
        )
      );
  }

1 Answer 1

1

Try the following code:

add_action( 'init', 'client' );
       function client() {
        register_post_type( 'client',
            array(
                'labels' => array(
                    'name' => __( 'Our Client' ),
                    'singular_name' => __( 'client' )
                ),
                'public' => true,
                'has_archive' => true,
                'rewrite' => array('slug' => 'client'),
                'supports' => array( 'title','thumbnail'),
                'register_meta_box_cb' => 'add_client_metaboxes',
            )
          );
      }

//Code for meta box.
add_action( 'add_meta_boxes', 'add_client_metaboxes' );

    function add_client_metaboxes() {
        add_meta_box('company_description', 'Company', 'company_description', 'client');
    }

    function company_description() {
        global $post;
     // Noncename needed to verify where the data originated
        echo '<input type="hidden" name="itemmeta_noncename" id="itemmeta_noncename" value="' .
        wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

     // Get the location data if its already been entered

        $comapny_detail = get_post_meta($post->ID, 'comapny_detail', true);
     // Echo out the field
     echo '<input type="text" name="comapny_detail" value="'.$comapny_detail.'">';
    }

Edited:

    function wpt_client() {
        register_post_type( 'client',
             array(
                'labels' => array(
                    'name' => __( 'Clients' ),
                    'singular_name' => __( 'Client' ),
                    'add_new' => __( 'Add New Client' ),
                    'add_new_item' => __( 'Add New Client' ),
                    'edit_item' => __( 'Edit Client' ),
                    'new_item' => __( 'Add New Client' ),
                    'view_item' => __( 'View Client' ),
                    'search_items' => __( 'Search Client' ),
                    'not_found' => __( 'No cleint found' ),
                    'not_found_in_trash' => __( 'No client found in trash' )
                ),
                'public' => true,
                'supports' => array( 'title','editor','thumbnail'),
                'capability_type' => 'post',
                'rewrite' => array("slug" => "client"), // Permalinks format
                'menu_position' => 20,
                'register_meta_box_cb' => 'create_meta_boxes',
            )
        );
    }
    add_action( 'init', 'wpt_client' );
    /* Custom meta boxes  */
    add_action( 'add_meta_boxes', 'create_meta_boxes' );

    function create_meta_boxes() {
        add_meta_box( 'my-meta-box-id', __('Company Name'), 'client_info', 'client', 'normal', 'low' );
    }

    // Create meta box: Company Name
    function client_info( $post ) {
        $values = get_post_custom( $post->ID );
        wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
    ?>
    <?php $text = get_post_meta($post->ID, 'client_info', true); ?>
    <input type="text" name="client_info" id="client_info" style="width: 100%; margin: 6px 0;" value="<?php echo $text; ?>" />
    <?php   
    }
    // Save meta box: Company Name
    add_action( 'save_post', 'save_client_info' );
    function save_client_info( $post_id ) {
        if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
        if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
        if( !current_user_can( 'edit_post' ) ) return;
        $allowed = array( 
            'a' => array( // on allow a tags
                'href' => array() // and those anchords can only have href attribute
            )
        );
        if( isset( $_POST['client_info'] ) )
            update_post_meta( $post_id, 'client_info', wp_kses( $_POST['client_info'], $allowed ) );
    }
Sign up to request clarification or add additional context in comments.

12 Comments

i used this but i am getting two problem one is after submitting the value it is not display me when i edit the post and not getting the value on front side as well
With the same code that I given you above ? Which problems ?
i used this but i am getting two problem one is after submitting the value it is not display me when i edit the post and not getting the value on front side as well
This is the only code for displaying custom field as you mention in your question. If you want to solve your two problems then you need to verify your nonce and then save it .It will solve it.
i have already check that even post id and all things i am getting only the value is not showing me
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.