Create Dynamic Meta Boxes in WordPress

You are here : Home / Create Dynamic Meta Boxes in WordPress

Meta boxes are small piece of html to add on administrative interface. These are useful to extend posts/pages or custom post type edit screens to ask more related information.

Using meta boxes, you can organized custom fields in a better way.

How to Add Meta Boxes

We’ve discussed about hooks in details. We’ll use add_meta_boxes action here to add a meta box on add or edit posts screens. Below is example output of our tutorial.

meta box Create Dynamic Meta Boxes in WordPress

Let’s try to understand this in steps.

  • Step 1: We need to register a hook “add_meta_boxes” first.
  • Step 2: In the definition of “func_extra_field”, we’ll use “add_meta_box” function to pass required parameters to display a form on the ‘post’ screens.
  • Step 3: “func_custom_forms” is a callback function for add_meta_box which is responsible for the output of the form.

    These are extra fields added by add_meta_box function.

    Note that we didn’t use any submit button here because default ‘publish’ button will be used to save this data.
  • Step 4: Now we need to save this data when click on publish button. For this, we’ll need to handle ‘save_post’ action which is triggered when a new blog post created or saved by clicking on publish button.

    We need to write our form data handling code in func_save_data_now function.

Conclusion

So idea is that every fields we add using add_meta_box save as post meta data using update_post_meta. ‘update_post_meta’ will call ‘add_post_meta’ if meta key doesn’t exists. Before using update_post_meta, we added few essential checklist validations which is necessary whenever you save anything in database.

 Create Dynamic Meta Boxes in WordPress

Flipper Code

We have been building WordPress Plugins at flipper code since 2008. We follow wordpress coding standard that ensures we deliver the excellent wordpress plugins and services.

More Posts - Website

Follow Me:
twitter Create Dynamic Meta Boxes in WordPressfacebook Create Dynamic Meta Boxes in WordPressgoogleplus Create Dynamic Meta Boxes in WordPress

Customize Product according to your requirements?

We will be glad to discuss any of your needs related to our wordpress plugins . if any of functionality you'd like to see in products, you can reach us by via email hello@flippercode.com.