Web Dev Tuts

/ Stuffs About Website Development /

Menu
  • Home
  • Wp Plugins
  • How To
  • FAQ
    • GIT
    • JQuery
    • PHP
    • WooCommerce
    • WordPress
  • Contact Us
Menu

How to Create Custom WP Metabox

Posted on September 10, 2017January 29, 2018 by admin

Default and custom types already have default fields that we can fill in, but what it we need custom data for these post types? In this kind of need, we can opt to create custom metaboxes which will contain custom fields for the desired post types.

To make the implementation of a custom metabox easier, I created an abtract class that you can find here.

So, to create a custom metabox, simply follow the steps below:

Step 1: Copy theĀ Metabox Class from WDT-Framework

Step 2: Extend the class and defined the needed functons:

  • _showForm() – this is where you should define the content of your metabox (normally a set of custom fields)
    public function _showForm($post) {
    	$value = get_post_meta($post->ID, 'reference', true);
        ?>
        <div>
        	<label>Reference:</label>
        	<input type="text" name="reference" value="<?php echo $value ?>" />
        </div>
        <?php
    }
  • _saveMetadata() – this is where you should define how the fields are saved.
    protected function _saveMetadata($post_id, $user_inputs) {
        //OK, it's safe for us to save the data now.
        update_post_meta($post_id, 'reference', $user_inputs['reference']);
    }

Step 3: Instantiate your class.

Parameters:
$metabox_id
(string) the id of your metabox; must be unique

$metabox_title
(string) the title if your metabox

$screens
(array) what screens should the metabox appear; normally an array of post types

$context
(string) options: normal, side, advanced

$priotity
(string) options: high, low, default

Examples:

new PostExtraOptions('opt1', 'mymetabox', array('post', 'page'), 'side', 'default');
new PostExtraOptions('opt2', 'mymetabox', array(page'), 'normal', 'high');

 

Category: How To, Wordpress

Leave a Reply Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to add wp_editor Dynamically
  • How to Hide Certain Pages from Certain Users (WP Dashboard)
  • How to Set Up Gmail SMTP
  • How to Set Up Virtual Host in Localhost & Xampp
  • How to Determine the Current WordPress Version Used
  • AngularJs
  • CSS
  • For Me Only
  • geo location api
  • GIT
  • GoDaddy
  • Hosting
  • How To
  • Javascript
  • JQuery
  • Magento
  • My Notes
  • PHP
  • Private
  • Sass
  • WooCommerce
  • Wordpress
  • Xampp
© 2023 Web Dev Tuts | Powered by Minimalist Blog WordPress Theme