0

I have created a couple of custom post type. To one of them i also want to add a custom field. It should just be a simple textfield where you can enter some text. Similar to the title field. How would you do that? I dont want to use a plugin.

Current code (functions.php)

    register_post_type( 'cases',
      array(
        'labels' => array(
            'name' => __( 'Cases' ),
            'singular_name' => __( 'Case' )
        ),
        'publicly_queryable' => true,
        'public' => true,
        'has_archive' => true,
        'rewrite' => array('slug' => 'cases'),
        'supports' => array('title','editor','thumbnail')
      )
    );
6
  • Are you editing the core files? Commented Mar 21, 2018 at 11:56
  • No. i dont. im working in functions.php Commented Mar 21, 2018 at 11:56
  • You can add custom field to post under screen options, did you try that? Commented Mar 21, 2018 at 11:58
  • cdn2.wpbeginner.com/wp-content/uploads/2017/06/… check here Commented Mar 21, 2018 at 11:59
  • @Thamaraiselvam I have tried that But the setting does not apply to my custom post types, only to the defaukt post type. Commented Mar 21, 2018 at 12:03

4 Answers 4

5

You need to create custom meta box and add that field within metabox.

Create Metabox

function add_your_fields_meta_box() {
add_meta_box(
    'your_fields_meta_box', // $id
    'Your Fields', // $title
    'show_your_fields_meta_box', // $callback
    'your_post', // $screen
    'normal', // $context
    'high' // $priority
);
}
add_action( 'add_meta_boxes', 'add_your_fields_meta_box' );

Html part

function show_your_fields_meta_box() {
global $post;  
    $meta = get_post_meta( $post->ID, 'your_fields', true ); ?>

<input type="hidden" name="your_meta_box_nonce" value="<?php echo wp_create_nonce( basename(__FILE__) ); ?>">

<!-- All fields will go here -->

<?php }

Save field in database

function save_your_fields_meta( $post_id ) {   
// verify nonce
if ( !wp_verify_nonce( $_POST['your_meta_box_nonce'], basename(__FILE__) ) ) {
    return $post_id; 
}
// check autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
    return $post_id;
}
// check permissions
if ( 'page' === $_POST['post_type'] ) {
    if ( !current_user_can( 'edit_page', $post_id ) ) {
        return $post_id;
    } elseif ( !current_user_can( 'edit_post', $post_id ) ) {
        return $post_id;
    }  
}

$old = get_post_meta( $post_id, 'your_fields', true );
$new = $_POST['your_fields'];

if ( $new && $new !== $old ) {
    update_post_meta( $post_id, 'your_fields', $new );
} elseif ( '' === $new && $old ) {
    delete_post_meta( $post_id, 'your_fields', $old );
}
}
add_action( 'save_post', 'save_your_fields_meta' );

For more detail you can check here https://www.taniarascia.com/wordpress-part-three-custom-fields-and-metaboxes/, This is very nice link which will help you to create custom meta box and field step by step

Sign up to request clarification or add additional context in comments.

2 Comments

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
You are right Philipp this link will be useful for current one user but in future if anyone will have same issue then couldn't be useful. Thank you
1

I know that this is way late, but under your function, you need to add 'custom-fields' to your support:

'supports' => array('title','editor','thumbnail', 'custom-fields')

Comments

0

If you want to create custom fields, you have to add metabox. See my example that will help you.

function show_your_fields_meta_box() {
    global $post;  
        $meta = get_post_meta( $post->ID, 'your_fields', true ); ?>
// Just paste your input here as below.
        <input type="hidden" name="your_meta_box_nonce" value="<?php echo wp_create_nonce( basename(__FILE__) ); ?>">
        <p>
            <label for="your_fields[text]">Input Text</label>
            <br>
            <input type="text" name="your_fields[text]" id="your_fields[text]" class="regular-text" value="<?php echo $meta['text']; ?>">
        </p>


    <?php }


function save_your_fields_meta( $post_id ) {   
    // verify nonce
    if ( !wp_verify_nonce( $_POST['your_meta_box_nonce'], basename(__FILE__) ) ) {
        return $post_id; 
    }
    // check autosave
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }
    // check permissions
    if ( 'page' === $_POST['post_type'] ) {
        if ( !current_user_can( 'edit_page', $post_id ) ) {
            return $post_id;
        } elseif ( !current_user_can( 'edit_post', $post_id ) ) {
            return $post_id;
        }  
    }

    $old = get_post_meta( $post_id, 'your_fields', true );
    $new = $_POST['your_fields'];

    if ( $new && $new !== $old ) {
        update_post_meta( $post_id, 'your_fields', $new );
    } elseif ( '' === $new && $old ) {
        delete_post_meta( $post_id, 'your_fields', $old );
    }
}
add_action( 'save_post', 'save_your_fields_meta' ); 

?>

Comments

0

Adding custom fields for custom post type 'user'

<?php
  function custom_field() {
    add_meta_box(
      'custom_field',
      'custom field',
      'custom_field_type',
      'user',
      'normal',
      'low'
    );
  }

  add_action('add_meta_boxes','custom_field');
     
  function custom_field_type($post) {
    $state = get_post_meta($post->ID,'state',true);
    $country = get_post_meta($post->ID,'country',true);
    $pincode = get_post_meta($post->ID,'pincode',true);
    $college = get_post_meta($post->ID,'college',true);
?>
  <label for = "state"> State : </label>
  <input type ="text" name="state" value="<?php echo $state ?>"><br><br>
  <label for = "country"> Country : </label>
  <input type ="text" name="country" value="<?php echo $country ?>"><br><br>
  <label for = "pincode"> Pincode : </label>
  <input type ="text" name="pincode" value="<?php echo $pincode?>"><br><br>
  <label for = "college"> College Name : </label>
  <input type ="text" name="college" value="<?php echo $college ?>"><br><br>

<?php
  }

  function handle_custom_field($post_id){
    if (isset($_POST['state'])) {
      $state = $_POST['state'];
      update_post_meta($post_id, 'state', $state);
    }
    if (isset($_POST['country'])) {
      $country = $_POST['country'];
      update_post_meta($post_id,'country', $country);
    }
    if (isset($_POST['pincode'])) {
      $pincode = $_POST['pincode'];
      update_post_meta($post_id,'pincode',$pincode);
    }
    if (isset($_POST['college'])) {
      $college = $_POST['college'];
      update_post_meta($post_id,'college',$college);
    }
  }
  add_action('save_post','handle_custom_field');
?>

Comments

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.