WordPress has metabox in post / page. Meta boxes is used to adding information in your post / page. To create meta box you need to know function add_meta_box, add_meta_boxes , wp_nonce_field, get_post_meta and save_post
<?php
/*
Plugin Name: ZZ Metabox
Plugin URI: http://www.wpamanuke.com
Description: Sample Metabox
Version: 1.0.1
Author: WPAmaNuke
Author URI: https://wpamanuke.com
*/
/**
* Register meta box(es).
*/
function zz_register_meta_boxes() {
add_meta_box( 'zz-meta-box-id', __( 'ZZ Meta Box', 'zz-metabox' ), 'zz_metaboxes_display_callback', 'post' );
}
add_action( 'add_meta_boxes', 'zz_register_meta_boxes' );
/**
* Meta box display callback.
*
* @param WP_Post $post Current post object.
*/
function zz_metaboxes_display_callback( $post ) {
// Display code/markup goes here. Don't forget to include nonces!
// retrieve the custom meta box values
$zz_price = get_post_meta( $post->ID, '_zz_price', true );
$zz_plan = get_post_meta( $post->ID, '_zz_plan', true );
//nonce for security
wp_nonce_field( plugin_basename( __FILE__ ), 'zz_save_meta_box' );
// Custom Meta Box Form
echo '<p>Price: <input type="text" name="zz_price" value="'.esc_attr( $zz_price ).'" size="10" /></p>';
echo '<p>Plan:
<select name="zz_plan" id="zz_plan">
<option value="daily" '. selected( $zz_plan, 'daily', false ). '>Daily</option>
<option value="weekly" '. selected( $zz_plan, 'weekly', false ). '>Weekly</option>
<option value="monthly" ' . selected( $zz_plan, 'monthly', false ). '>Monthly </option>
<option value="yearly" ' . selected( $zz_plan, 'yearly', false ). '>Yearly </option>
</select>
</p>';
}
/**
* Save meta box content.
*
* @param int $post_id Post ID
*/
function zz_save_meta_box( $post_id ) {
// Save logic goes here. Don't forget to include nonce checks!
// process form data if $_POST is set
if( isset( $_POST['zz_plan'] ) ) {
// if auto saving skip saving our meta box data
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
//check nonce for security
check_admin_referer( plugin_basename( __FILE__ ), 'zz_save_meta_box' );
// save the meta box data as post meta using the post ID as a unique prefix
update_post_meta( $post_id, '_zz_plan', sanitize_text_field( $_POST['zz_plan'] ) );
update_post_meta( $post_id, '_zz_price', sanitize_text_field ( $_POST['zz_price'] ) );
}
}
add_action( 'save_post', 'zz_save_meta_box' );