How To Add a Custom Field via WordPress plugin

How To Add a Custom Field via WordPress plugin?

Sometimes we need to add custom data for our posts and pages other than title and content especially when ones become comfortable with WordPress, there is an urge to do something experimenting for exciting features using WordPress custom field.  for  Suppose you are using Custom Post Types for your book store and want to save the Book price as well. You have added title and description but for price you will add a custom field. Today we are going to gain an understanding of  how you can add custom fields via WordPress plugins. In modern WordPress development, these kinds of functional stuff should be done via plugins instead of coding in WordPress themes.

Before moving forward please download the plugin from here. Please open the class file “class_custom_field.php”.

I will explain every code snippet step by step.

function init_customField() {
new customField();
}

The above function is simply getting an instance of the class and returning it.

if ( is_admin() ) {
add_action( 'load-post.php', 'init_customField' );
add_action( 'load-post-new.php', 'init_customField' );
}

This code snippet is saying that when a post is to be edited or a new post is to be made get an instance of “customField” class and getting an instance is of course means to initialize and render the text field in the posting page.

“load-post” and “load-post-new” are two new hooks.

 

public function __construct() {
add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
add_action( 'save_post', array( $this, 'save' ) );
}

Class constructor is adding functions to the hooks for rendering and saving the data.

 

public function add_meta_box( $post_type ) {

$post_types = array('post', 'page'); //limit meta box to certain post types
if ( in_array( $post_type, $post_types )) {
add_meta_box(
'my_meta_box_name'
,__( 'My Meta Box Headline', 'myplugin_textdomain' )
,array( $this, 'render_meta_box_content' )
,$post_type
);
}
}

The “add_meta_box” is in fact the responsibility to render the text field. The first argument is setting “id“, the second argument is setting title, the third argument is giving the definition that how will it be and the fourth argument is setting the screen is it a “page” or a “post” where this custom field will be rendered on.

 

public function save( $post_id ) {
//code goes here
}

Inside the “save” function first, we are checking whether “nonce” is set. “nonce” is put in the form for security reasons. You can read about it from here. Then we are verifying whether it is a valid “nonce”. Then simply checking whether the current user is authorized to perform this action. Then we are sanitizing the data and saving or updating the data using “update_post_meta” giving a key. Because with this key we will later retrieve our data.

 

public function render_meta_box_content( $post ) {
//code goes here
}

Inside “render_meta_box_content” we are generating “nonce”. Then getting the data if it was set using “get_post_meta” function and putting this value in the text field and rendering the text field.

And all done! Adding custom fields to WordPress is like doing some extra coding and in result you get the specific features for a particular post that may need some extra informational data.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.