Skip to content

Instantly share code, notes, and snippets.

@itskawsar
Created June 24, 2014 11:35
Show Gist options
  • Save itskawsar/ea542c8dd92cd5f2f73a to your computer and use it in GitHub Desktop.
Save itskawsar/ea542c8dd92cd5f2f73a to your computer and use it in GitHub Desktop.
WordPress Custom Metabox Snippet
// Product meta box
/**
* Adds a box to the main column on the Post and Page edit screens.
*/
function tormuz_add_meta_box() {
$screens = array( 'product' );
foreach ( $screens as $screen ) {
add_meta_box(
'tormuz_sectionid',
__( 'Product Additional Info', 'st-framework' ),
'tormuz_meta_box_callback',
$screen,
'side'
);
}
}
add_action( 'add_meta_boxes', 'tormuz_add_meta_box' );
/**
* Prints the box content.
*
* @param WP_Post $post The object for the current post/page.
*/
function tormuz_meta_box_callback( $post ) {
// Add an nonce field so we can check for it later.
wp_nonce_field( 'tormuz_meta_box', 'tormuz_meta_box_nonce' );
/*
* Use get_post_meta() to retrieve an existing value
* from the database and use the value for the form.
*/
$product_amazon_link = get_post_meta( $post->ID, 'product_amazon_link', true );
$product_new_price = get_post_meta( $post->ID, 'product_new_price', true );
$product_old_price = get_post_meta( $post->ID, 'product_old_price', true );
echo '<label for="product_amazon_link">';
_e( 'Amazon Product Link:', 'st-framework' );
echo '</label> ';
echo "<br>";
echo '<input class="widefat" type="text" id="product_amazon_link" name="product_amazon_link" value="' . esc_attr( $product_amazon_link ) . '" size="25" />';
echo "<br>";
echo "<br>";
echo '<label for="product_amazon_link">';
_e( 'New Price:', 'st-framework' );
echo '</label> ';
echo "<br>";
echo '<input class="widefat" type="text" id="product_new_price" name="product_new_price" value="' . esc_attr( $product_new_price ) . '" size="25" />';
echo "<br>";
echo "<br>";
echo '<label for="product_amazon_link">';
_e( 'Old Price:', 'st-framework' );
echo '</label> ';
echo "<br>";
echo '<input class="widefat" type="text" id="product_old_price" name="product_old_price" value="' . esc_attr( $product_old_price ) . '" size="25" />';
}
/**
* When the post is saved, saves our custom data.
*
* @param int $post_id The ID of the post being saved.
*/
function tormuz_save_meta_box_data( $post_id ) {
/*
* We need to verify this came from our screen and with proper authorization,
* because the save_post action can be triggered at other times.
*/
// Check if our nonce is set.
if ( ! isset( $_POST['tormuz_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['tormuz_meta_box_nonce'], 'tormuz_meta_box' ) ) {
return;
}
// 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_post', $post_id ) ) {
return;
}
// Update the meta field in the database.
update_post_meta( $post_id, 'product_amazon_link', sanitize_text_field( $_POST['product_amazon_link'] ) );
update_post_meta( $post_id, 'product_new_price', sanitize_text_field( $_POST['product_new_price'] ) );
update_post_meta( $post_id, 'product_old_price', sanitize_text_field( $_POST['product_old_price'] ) );
}
add_action( 'save_post', 'tormuz_save_meta_box_data' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment