3

I need to add a custom field in admin section (below the title field) which insert the values of that custom field in the db table.

but all this i want to do through coding like

<input type="text" name="post_title" size="30" tabindex="1" value="<?php echo esc_attr( htmlspecialchars( $post->post_title ) ); ?>" id="title" autocomplete="off" />

This is for the text box title I need the same one for age, one for country, and one for date of birth below the title field without using any plugin

I'm really very confused

thanx for help

3 Answers 3

2

To insert your own custom field after the title field you will have to invoke the hook edit_form_after_title. You may at the following to your plugin or theme:

add_action('edit_form_after_title', function() {
  global $post;
  print '<input type="text" name="post_title" size="30" tabindex="1" value="'.esc_attr( htmlspecialchars( $post->post_title ) ).'" id="title" autocomplete="off" />';
});

Note: the default title field will stop working if you add this field (it won't save anymore).

All default post attributes don't necessarily require a form submit handler. Your field will use the default handlers what come with wordpress. However if you want to add custom fields which are not available yet or if you want to alter default behavior of the handlers then you will have to invoke the save_post hook as well.

I recommend specifying to the post type you want to hook on by appending the name to the hook (e.g. save_post_book). This hook enables you to apply logic to the form data submitted by the user.

1

Custom fields in WordPress admin reside in "Custom Meta Boxes".

To create a meta box use the add_meta_box() function plugged to the "add_meta_boxes" action hook.

To save the fields create a function plugged to the "save_post" action hook - in this function you can validate the fields and save them as post meta.

Read a more detail explanation at https://developer.wordpress.org/plugins/metadata/custom-meta-boxes/

0

Why not just use custom field post meta data and save yourself a lot of trouble?

3
  • 2
    yup you are right but i want to put my self in trouble infact i want to learn it from core that why want to do it can u help me according to my wish as i mentioned above
    – kashif
    Dec 9, 2011 at 9:39
  • If you want to learn it "from the core" use the functions that WordPress has put in place. You're actually trying to go beyond WordPress which is really quite useless
    – Jebble
    Jun 11, 2018 at 13:32
  • There are times when the stock WP options do not meet the requirements. ie. I am adding a new field manually so as to position the field in the proper place on the screen for our non admin (editor) users vs having to ask them to use a manually created custom field via WP custom fields option.
    – DropHit
    Feb 14, 2021 at 6:57

Your Answer

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

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