Metabox with multiple custom fields for WooCommerce admin order pages - php

I found the below code the other day, and now my client wants two more fields. If I add it again to my snippet plugin it says “Cannot redeclare function set_custom_edit_shop_order_columns.”. Could someone kindly help me to change the code with two more custom fields?
I have changed custom_column to courier_reference and want another text field tracking_number and one more is shipping_date with a date picker rather than a text box.
Any help is appreciated.
//from::https://codex.wordpress.org/Plugin_API/Action_Reference/manage_posts_custom_column
// For displaying in columns.
add_filter( 'manage_edit-shop_order_columns', 'set_custom_edit_shop_order_columns' );
function set_custom_edit_shop_order_columns($columns) {
$columns['custom_column'] = __( 'Custom Column', 'your_text_domain' );
return $columns;
}
// Add the data to the custom columns for the order post type:
add_action( 'manage_shop_order_posts_custom_column' , 'custom_shop_order_column', 10, 2 );
function custom_shop_order_column( $column, $post_id ) {
switch ( $column ) {
case 'custom_column' :
echo esc_html( get_post_meta( $post_id, 'custom_column', true ) );
break;
}
}
// For display and saving in order details page.
add_action( 'add_meta_boxes', 'add_shop_order_meta_box' );
function add_shop_order_meta_box() {
add_meta_box(
'custom_column',
__( 'Custom Column', 'your_text_domain' ),
'shop_order_display_callback',
'shop_order'
);
}
// For displaying.
function shop_order_display_callback( $post ) {
$value = get_post_meta( $post->ID, 'custom_column', true );
echo '<textarea style="width:100%" id="custom_column" name="custom_column">' . esc_attr( $value ) . '</textarea>';
}
// For saving.
function save_shop_order_meta_box_data( $post_id ) {
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'shop_order' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_shop_order', $post_id ) ) {
return;
}
}
// Make sure that it is set.
if ( ! isset( $_POST['custom_column'] ) ) {
return;
}
// Sanitize user input.
$my_data = sanitize_text_field( $_POST['custom_column'] );
// Update the meta field in the database.
update_post_meta( $post_id, 'custom_column', $my_data );
}
add_action( 'save_post', 'save_shop_order_meta_box_data' );

Updated... You just need to have multiple different slugs in the same functions. For your desired 3 custom fields use the following:
// Add columns to admin orders table.
add_filter( 'manage_edit-shop_order_columns', 'set_custom_edit_shop_order_columns' );
function set_custom_edit_shop_order_columns($columns) {
$columns['courier_reference'] = __( 'Courier reference', 'woocommerce' );
$columns['tracking_number'] = __( 'Tracking number', 'woocommerce' );
$columns['shipping_date'] = __( 'Shipping date', 'woocommerce' );
return $columns;
}
// Add the data to the custom columns for the order post type:
add_action( 'manage_shop_order_posts_custom_column' , 'custom_shop_order_column', 10, 2 );
function custom_shop_order_column( $column, $post_id ) {
switch ( $column ) {
case 'courier_reference' :
echo esc_html( get_post_meta( $post_id, 'courier_reference', true ) );
break;
case 'tracking_number' :
echo esc_html( get_post_meta( $post_id, 'tracking_number', true ) );
break;
case 'shipping_date' :
echo esc_html( get_post_meta( $post_id, 'shipping_date', true ) );
break;
}
}
// Add a metabox.
add_action( 'add_meta_boxes', 'add_shop_order_meta_box' );
function add_shop_order_meta_box() {
add_meta_box(
'custom_meta_box',
__( 'Tracking information', 'woocommerce' ),
'shop_order_content_callback',
'shop_order'
);
}
// For displaying metabox content
function shop_order_content_callback( $post ) {
// Textarea Field
$courier_reference = get_post_meta( $post->ID, 'courier_reference', true );
echo '<p>' . __( 'Courier reference', 'woocommerce' ) . '<br>
<textarea style="width:100%" id="courier_reference" name="courier_reference">' . esc_attr( $courier_reference ) . '</textarea></p>';
// Text field
$tracking_number = get_post_meta( $post->ID, 'tracking_number', true );
echo '<p>' . __( 'Tracking number', 'woocommerce' ) . '<br>
<input type="text" style="width:100%" id="tracking_number" name="tracking_number" value="' . esc_attr( $tracking_number ) . '"></p>';
// Date picker field
$shipping_date = get_post_meta( $post->ID, 'shipping_date', true );
echo '<p>' . __( 'shipping_date', 'woocommerce' ) . '<br>
<input type="date" style="width:100%" id="shipping_date" name="shipping_date" value="' . esc_attr( $shipping_date ) . '"></p>';
}
// For saving the metabox data.
add_action( 'save_post_shop_order', 'save_shop_order_meta_box_data' );
function save_shop_order_meta_box_data( $post_id ) {
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( ! current_user_can( 'edit_shop_order', $post_id ) ) {
return;
}
// Make sure that 'shipping_date' is set.
if ( isset( $_POST['courier_reference'] ) ) {
// Update the meta field in the database.
update_post_meta( $post_id, 'courier_reference', sanitize_textarea_field( $_POST['courier_reference'] ) );
}
// Make sure that 'tracking_number' it is set.
if ( isset( $_POST['tracking_number'] ) ) {
// Update the meta field in the database.
update_post_meta( $post_id, 'tracking_number', sanitize_text_field( $_POST['tracking_number'] ) );
}
// Make sure that 'shipping_date' is set.
if ( isset( $_POST['shipping_date'] ) ) {
// Update the meta field in the database.
update_post_meta( $post_id, 'shipping_date', sanitize_text_field( $_POST['shipping_date'] ) );
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Related

Save WooCommerce custom Session Variables as order meta data

I'm trying to store custom session variables in database. Then, show them inside new order email and order details in WooCommerce Admin.
I have custom variables inside session:
add_action( 'init', 'oturum_degiskeni_olustur' );
function oturum_degiskeni_olustur () {
// Early initialize customer session
if ( isset(WC()->session) && ! WC()->session->has_session() ) {
WC()->session->set_customer_session_cookie( true );
}
if ( isset( $_GET['konumu'] ) || isset( $_GET['masa_no'] ) ) {
$konum = isset( $_GET['konumu'] ) ? esc_attr( $_GET['konumu'] ) : '';
$masa = isset( $_GET['masa_no'] ) ? esc_attr( $_GET['masa_no'] ) : '';
// Set the session data
WC()->session->set( 'custom_data', array( 'konum' => $konum, 'masa' => $masa ) );
}
}
Firstly, I added custom variables to database with this code;
// Storing session variables for using them in order notifications
add_action( 'woocommerce_checkout_create_order', 'oturum_degiskeni_kaydet' );
function oturum_degiskeni_kaydet( $order, $data ) {
if ( $_POST['konumu'] ) update_meta_data( $order_id, '_konum', esc_attr( $_POST['konumu'] ) );
if ( $_POST['masa_no'] ) update_meta_data( $order_id, '_masa', esc_attr( $_POST['masa_no'] ) );
}
Second, I added this variable's data to a new order email for admin.
// Show this session variables in new order email for admin
add_action( 'woocommerce_email_after_order_table', 'konumu_emaile_ekle', 20, 4 );
function konumu_emaile_ekle( $order, $sent_to_admin, $plain_text, $email ) {
if ( get_post_meta( $order->get_id(), '_konum', true ) ) echo '<p><strong>Konum :</strong> ' . get_post_meta( $order->get_id(), '_konum', true ) . '</p>';
if ( get_post_meta( $order->get_id(), '_masa', true ) ) echo '<p><strong>Masa Numarası :</strong> ' . get_post_meta( $order->get_id(), '_masa', true ) . '</p>';
}
Last part of the code is shown session variables data in WooCommerce order page;
// Show session variable in woocommerce order page
add_action( 'woocommerce_admin_order_data_after_billing_address', 'konumu_admine_ekle', 10, 1 );
function konumu_admine_ekle( $order ) {
$order_id = $order->get_id();
if ( get_post_meta( $order_id, '_konum', true ) ) echo '<p><strong>Konum :</strong> ' . get_post_meta( $order_id, '_konum', true ) . '</p>';
if ( get_post_meta( $order_id, '_masa', true ) ) echo '<p><strong>Masa Numarası :</strong> ' . get_post_meta( $order_id, '_masa', true ) . '</p>';
}
But, it does not work. When customer made an order, it gave an error "We were unable to process your order, please try again."
Updated: There are some mistakes in your code when you are trying to save your custom data from session as order meta data and display it on emails and admin order pages…
Your first function is correct (oturum_degiskeni_olustur)…
Assuming that data is passed through URL like: website.com/?konumu=newyork&masa_no=12
Here is the revisited code:
// Unchanged
add_action( 'init', 'oturum_degiskeni_olustur' );
function oturum_degiskeni_olustur () {
// Early initialize customer session
if ( isset(WC()->session) && ! WC()->session->has_session() ) {
WC()->session->set_customer_session_cookie( true );
}
if ( isset( $_GET['konumu'] ) || isset( $_GET['masa_no'] ) ) {
$konum = isset( $_GET['konumu'] ) ? esc_attr( $_GET['konumu'] ) : '';
$masa = isset( $_GET['masa_no'] ) ? esc_attr( $_GET['masa_no'] ) : '';
// Set the session data
WC()->session->set( 'custom_data', array( 'konum' => $konum, 'masa' => $masa ) );
}
}
// Save custom session data as order meta data
add_action( 'woocommerce_checkout_create_order', 'oturum_degiskeni_kaydet' );
function oturum_degiskeni_kaydet( $order ) {
$data = WC()->session->get( 'custom_data' ); // Get custom data from session
if ( isset($data['konum']) ) {
$order->update_meta_data( '_konum', $data['konum'] );
}
if ( isset($data['masa']) ) {
$order->update_meta_data( '_masa', $data['masa'] );
}
WC()->session->__unset( 'custom_data' ); // Remove session variable
}
// Show this session variables in new order email for admin and in woocommerce order page
add_action( 'woocommerce_email_after_order_table', 'konumu_emaile_admine_ekle', 20 );
add_action( 'woocommerce_admin_order_data_after_billing_address', 'konumu_emaile_admine_ekle' );
function konumu_emaile_admine_ekle( $order ) {
if ( $konum = $order->get_meta( '_konum' ) )
echo '<p><strong>Masa Numarası :</strong> ' . $konum . '</p>';
if ( $masa = $order->get_meta( '_masa' ) )
echo '<p><strong>Konum :</strong> ' . $masa . '</p>';
}
Code goes in functions.php file of your active child theme (or active theme). Tested and Works.
On the email notification before customer details:
On admin order edit page (under billing phone):
Tested on WooCommerce 4.2+ under storefront theme

Add personalized content after the "Add to Cart" button? Woocommerce

Woocommerce: How to add some html codes below add to cart in single product page? and i want add different codes for each single product!
I used a similar one for metabox, but I can not find a way to adapt it after "add to cart"
---- 1. Backend ----
// Adding a custom Meta container to admin products pages
add_action( 'add_meta_boxes', 'create_custom_meta_box' );
if ( ! function_exists( 'create_custom_meta_box' ) )
{
function create_custom_meta_box()
{
add_meta_box(
'custom_product_meta_box',
__( 'Additional Product text <em>(optional)</em>', 'woocommerce' ),
'add_custom_product_content_meta_box',
'product',
'normal',
'default'
);
}
}
// Custom metabox content in admin product pages
if ( ! function_exists( 'add_custom_product_content_meta_box' ) ){
function add_custom_product_content_meta_box( $post ){
$text_area = get_post_meta($post->ID, '_custom_text', true) ? get_post_meta($post->ID, '_custom_text', true) : '';
$args['textarea_rows'] = 6;
echo '<p>'.__( 'Custom text label', 'woocommerce' ).'</p>';
wp_editor( $text_area, 'custom_text', $args );
echo '<input type="hidden" name="custom_text_field_nonce" value="' . wp_create_nonce() . '">';
}
}
//Save the data of the Meta field
add_action( 'save_post', 'save_custom_product_content_meta_box', 20, 3 );
if ( ! function_exists( 'save_custom_product_content_meta_box' ) ){
function save_custom_product_content_meta_box( $post_id, $post, $update ) {
if ( $post->post_type != 'product') return; // Only products
// Check if our nonce is set.
if ( ! isset( $_POST[ 'custom_text_field_nonce' ] ) )
return $post_id;
//Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST[ 'custom_text_field_nonce' ] ) )
return $post_id;
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// Check the user's permissions.
if ( ! current_user_can( 'edit_product', $post_id ) )
return $post_id;
// Sanitize user input and update the meta field in the database.
if ( isset( $_POST[ 'custom_text' ] ) )
update_post_meta( $post_id, $prefix.'_custom_text', wp_kses_post($_POST[ 'custom_text' ]) );
}
}
---- 2. Frontend ----
// Add custom text under single product meta
add_action( 'woocommerce_single_product_summary', 'add_custom_product_text', 70 );
function add_custom_product_text() {
global $product;
$custom_text = get_post_meta( $product->get_id(), '_custom_text', true );
if( empty($custom_text) ) return;
echo '<div class="product-extra-text" style="margin-top:30px;">';
echo '<h3>' . __( 'Product extras', 'woocommerce' ) . '</h3>';
// Updated to apply the_content filter to WYSIWYG content
echo apply_filters( 'the_content', $custom_text );
echo '</div>';
}
I put the code in parts, but it is united in functions of the theme.
Backend IMAGE:
enter image description here
In your theme's function.php add the code,
add_action('woocommerce_after_add_to_cart_button','show_custom_text');
function show_custom_text() {
call the custom text inside this function
}

WordPress - how to add custom meta box to a specific admin page?

How can I add my custom meta box to a specific page only on the admin page?
Here is my custom meta box code which I got it from here:
/**
* Adds a meta box to the post editing screen
*/
function prfx_custom_meta() {
add_meta_box( 'prfx_meta', __( 'Meta Box Title', 'prfx-textdomain' ), 'prfx_meta_callback', array( 'post', 'page') );
}
add_action( 'add_meta_boxes', 'prfx_custom_meta' );
/**
* Outputs the content of the meta box
*/
function prfx_meta_callback( $post ) {
// echo 'This is a meta box';
wp_nonce_field( basename( __FILE__ ), 'prfx_nonce' );
$prfx_stored_meta = get_post_meta( $post->ID );
if ($post_slug == 'home') {
?>
<p>
<label for="meta-text" class="prfx-row-title"><?php _e( 'Example Text Input', 'prfx-textdomain' )?></label>
<input type="text" name="meta-text" id="meta-text" value="<?php if ( isset ( $prfx_stored_meta['meta-text'] ) ) echo $prfx_stored_meta['meta-text'][0]; ?>" />
</p>
<?php
}
}
/**
* Saves the custom meta input
*/
function prfx_meta_save( $post_id ) {
// Checks save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'prfx_nonce' ] ) && wp_verify_nonce( $_POST[ 'prfx_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}
// Checks for input and sanitizes/saves if needed
if( isset( $_POST[ 'meta-text' ] ) ) {
update_post_meta( $post_id, 'meta-text', sanitize_text_field( $_POST[ 'meta-text' ] ) );
}
}
add_action( 'save_post', 'prfx_meta_save' );
I just want to add the meta box to my home page. but now on other pages and posts I still see the meta title:
Any idea how I stop it from showing on other pages and posts?
EDIT:
/**
* Add custom meta box to a specific page in the WP admin.
*
* # http://themefoundation.com/wordpress-meta-boxes-guide/
* # http://www.farinspace.com/page-specific-wordpress-meta-box/
*/
function my_meta_init() {
// Get post/page ID.
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
// Get post/page slug.
$post = get_post($post_id);
$slug = $post->post_name;
// checks for post/page slug.
if ($slug == 'home') {
add_meta_box( 'prfx_meta', __( 'Meta Box Title', 'prfx-textdomain' ), 'prfx_meta_callback', array( 'post', 'page') );
}
add_action( 'add_meta_boxes', 'prfx_meta_save' );
}
add_action('admin_init','my_meta_init');
/**
* Outputs the content of the meta box
*/
function prfx_meta_callback( $post ) {
// echo 'This is a meta box';
wp_nonce_field( basename( __FILE__ ), 'prfx_nonce' );
$prfx_stored_meta = get_post_meta( $post->ID );
?>
<p>
<label for="meta-text" class="prfx-row-title"><?php _e( 'Example Text Input', 'prfx-textdomain' )?></label>
<input type="text" name="meta-text" id="meta-text" value="<?php if ( isset ( $prfx_stored_meta['meta-text'] ) ) echo $prfx_stored_meta['meta-text'][0]; ?>" />
</p>
<?php
}
/**
* Saves the custom meta input
*/
function prfx_meta_save( $post_id ) {
// Checks save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'prfx_nonce' ] ) && wp_verify_nonce( $_POST[ 'prfx_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}
// Checks for input and sanitizes/saves if needed
if( isset( $_POST[ 'meta-text' ] ) ) {
update_post_meta( $post_id, 'meta-text', sanitize_text_field( $_POST[ 'meta-text' ] ) );
}
}
add_action( 'save_post', 'prfx_meta_save' );
You can try this condition
add_action('admin_init','my_meta_init');
function my_meta_init()
{
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
// checks for post/page ID
if ($post_id == '84')
{
add_meta_box('my_all_meta_1', 'My Custom Meta Box 1', 'my_meta_setup_1', 'page', 'normal', 'high');
}
add_action('save_post','my_meta_save');
}
/**
* Adds a meta box to the post editing screen
*/
function prfx_custom_meta() {
$current_user = wp_get_current_user();
if($current_user->roles[0] === 'administrator') {
add_meta_box( 'prfx_meta', __( 'Meta Box Title', 'prfx-textdomain' ), 'prfx_meta_callback', array( 'post', 'page') );
}
}
add_action( 'add_meta_boxes', 'prfx_custom_meta' );
/**
* Outputs the content of the meta box
*/
function prfx_meta_callback( $post ) {
// echo 'This is a meta box';
wp_nonce_field( basename( __FILE__ ), 'prfx_nonce' );
$prfx_stored_meta = get_post_meta( $post->ID );
if ($post_slug == 'home') {
?>
<p>
<label for="meta-text" class="prfx-row-title"><?php _e( 'Example Text Input', 'prfx-textdomain' )?></label>
<input type="text" name="meta-text" id="meta-text" value="<?php if ( isset ( $prfx_stored_meta['meta-text'] ) ) echo $prfx_stored_meta['meta-text'][0]; ?>" />
</p>
<?php
}
}
/**
* Saves the custom meta input
*/
function prfx_meta_save( $post_id ) {
// Checks save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'prfx_nonce' ] ) && wp_verify_nonce( $_POST[ 'prfx_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}
// Checks for input and sanitizes/saves if needed
if( isset( $_POST[ 'meta-text' ] ) ) {
update_post_meta( $post_id, 'meta-text', sanitize_text_field( $_POST[ 'meta-text' ] ) );
}
}
add_action( 'save_post', 'prfx_meta_save' );
Use this js it will resolve your problem. this will show the metabox on perticular template or page..
(function($){
$(document).ready(function() {
var $page_template = $('#pageid')
,$metabox1 = $('#metaboxid');
$page_template.change(function() {
if ($(this).val() == 'templatename') {
$metabox1.show();
} else {
$metabox1.hide();
}
}).change();
});
})(jQuery);
if you are using this in admin then use admin hook and add this code in functions.php

How to make the custom meta boxes support for the visual composer in WordPress?

I am using the visual composer for the WordPress posts and pages actually for over all. But I want to make some custom meta boxes under the screen of the post editor. Actually already I have made the fields. But now I want to make those fields available in the visual composer. Actually I want to add those fields in the visual editor. How can I do that? Please help me with your valuable knowledge.
Here is my code of the meta boxes
<?php
function myplugin_add_meta_box() {
$screens = array( 'post', 'page' );
foreach ( $screens as $screen ) {
add_meta_box(
'myplugin_sectionid',
__( 'My Post Section Title', 'myplugin_textdomain' ),
'myplugin_meta_box_callback',
$screen
);
}
}
add_action( 'add_meta_boxes', 'myplugin_add_meta_box' );
function myplugin_meta_box_callback( $post ) {
wp_nonce_field( 'myplugin_save_meta_box_data', 'myplugin_meta_box_nonce' );
$value = get_post_meta( $post->ID, '_my_meta_value_key', true );
echo '<label for="myplugin_new_field">';
_e( 'Description for this field', 'myplugin_textdomain' );
echo '</label> ';
echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="' . esc_attr( $value ) . '" size="25" />';
}
function myplugin_save_meta_box_data( $post_id ) {
if ( ! isset( $_POST['myplugin_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['myplugin_meta_box_nonce'], 'myplugin_save_meta_box_data' ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
/* OK, it's safe for us to save the data now. */
// Make sure that it is set.
if ( ! isset( $_POST['myplugin_new_field'] ) ) {
return;
}
// Sanitize user input.
$my_data = sanitize_text_field( $_POST['myplugin_new_field'] );
// Update the meta field in the database.
update_post_meta( $post_id, '_my_meta_value_key', $my_data );
}
add_action( 'save_post', 'myplugin_save_meta_box_data' );
I see that you are echoing your fields as inputs. You need to use the wp_editor() function instead. It will take care of the wysiwyg (visual editor) field creation for you.

Creating Meta-boxes

Found a function which add to my admin panel one meta-box. It's looks nice, but not enough.
I need at least ten custom fields. How to create them?
The questoins looks stupid, because my knowledge of php are minimal, so do not judge strictly ;). I will be grateful for the solution of this problem or an explanation of how it should work.
function myplugin_add_meta_box() {
$screens = array( 'post', 'page' );
foreach ( $screens as $screen ) {
add_meta_box(
'myplugin_sectionid',
__( 'My Post Section Title', 'myplugin_textdomain' ),
'myplugin_meta_box_callback',
$screen
);
}
}
add_action( 'add_meta_boxes', 'myplugin_add_meta_box' );
/**
* Prints the box content.
*
* #param WP_Post $post The object for the current post/page.
*/
function myplugin_meta_box_callback( $post ) {
wp_nonce_field( 'myplugin_meta_box', 'myplugin_meta_box_nonce' );
$value = get_post_meta( $post->ID, '_my_meta_value_key', true );
echo '<label for="myplugin_new_field">';
_e( 'Description for this field', 'myplugin_textdomain' );
echo '</label> ';
echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="' . esc_attr( $value ) . '" size="25" />';
}
/**
* When the post is saved, saves our custom data.
*
* #param int $post_id The ID of the post being saved.
*/
function myplugin_save_meta_box_data( $post_id ) {
if ( ! isset( $_POST['myplugin_meta_box_nonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( $_POST['myplugin_meta_box_nonce'], 'myplugin_meta_box' ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
if ( ! isset( $_POST['myplugin_new_field'] ) ) {
return;
}
$my_data = sanitize_text_field( $_POST['myplugin_new_field'] );
update_post_meta( $post_id, '_my_meta_value_key', $my_data );
}
add_action( 'save_post', 'myplugin_save_meta_box_data' );
If your knowledge of PHP is quite low, there are some great plugins that allow you to do this easily.
If you have a lot of fields to add, I usually find it easier to use the following plugin as ordering fields and pulling them out in themes is a lot easier than doing it manually.
http://wordpress.org/plugins/advanced-custom-fields/

Categories