Last active
January 15, 2018 16:28
-
-
Save moghanbari/01b7d0d523661efe8d86 to your computer and use it in GitHub Desktop.
second title for a woocommerce product
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Calls the class on the post edit screen. | |
*/ | |
function call_mq_product_second_title_metabox() { | |
new mq_product_second_title_metabox(); | |
} | |
if ( is_admin() ) { | |
add_action( 'load-post.php', 'call_mq_product_second_title_metabox' ); | |
add_action( 'load-post-new.php', 'call_mq_product_second_title_metabox' ); | |
} | |
/** | |
* The Class. | |
*/ | |
class mq_product_second_title_metabox{ | |
/** | |
* Hook into the appropriate actions when the class is constructed. | |
*/ | |
public function __construct() { | |
add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) ); | |
add_action( 'save_post', array( $this, 'save' ) ); | |
} | |
/** | |
* Adds the meta box container. | |
*/ | |
public function add_meta_box( $post_type ) { | |
$post_types = array('product'); //limit meta box to certain post types | |
if ( in_array( $post_type, $post_types )){ | |
add_meta_box( | |
'Woocommerce_Product_Second_Title' | |
,__( 'Woocommerce Product Second Title', 'mq_woocommerce_product_second_title_metabox' ) | |
,array( $this, 'render_meta_box_content' ) | |
,$post_type | |
,'side' | |
,'high' | |
); | |
} | |
} | |
/** | |
* Save the meta when the post is saved. | |
* | |
* @param int $post_id The ID of the post being saved. | |
*/ | |
public function save( $post_id ) { | |
/* | |
* We need to verify this came from the our screen and with proper authorization, | |
* because save_post can be triggered at other times. | |
*/ | |
// Check if our nonce is set. | |
if ( ! isset( $_POST['mq_product_second_title_nonce_field'] ) ) | |
return $post_id; | |
$nonce = $_POST['mq_product_second_title_nonce_field']; | |
// Verify that the nonce is valid. | |
if ( ! wp_verify_nonce( $nonce, 'mq_product_second_title_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 ( 'product' == $_POST['post_type'] ) { | |
if ( ! current_user_can( 'edit_post', $post_id ) ) | |
return $post_id; | |
} | |
/* OK, its safe for us to save the data now. */ | |
// Sanitize the user input. | |
$mydata = sanitize_text_field( $_POST['mq_woocommerce_product_second_title_field'] ); | |
// Update the meta field. | |
update_post_meta( $post_id, '_mq_second_title_meta_value_key', $mydata ); | |
} | |
/** | |
* Render Meta Box content. | |
* | |
* @param WP_Post $post The post object. | |
*/ | |
public function render_meta_box_content( $post ) { | |
// Add an nonce field so we can check for it later. | |
wp_nonce_field( 'mq_product_second_title_nonce', 'mq_product_second_title_nonce_field' ); | |
// Use get_post_meta to retrieve an existing value from the database. | |
$value = get_post_meta( $post->ID, '_mq_second_title_meta_value_key', true ); | |
// Display the form, using the current value. | |
echo '<label for="mq_woocommerce_product_second_title_field">'; | |
_e( 'Write The second title For your woocommerce Product.', 'mq_woocommerce_product_second_title_metabox' ); | |
echo '</label> '; | |
echo '<input type="text" id="mq_woocommerce_product_second_title_field" name="mq_woocommerce_product_second_title_field"'; | |
echo ' value="' . esc_attr( $value ) . '" size="25" />'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
add_meta_box( $id, $title, $callback, $post_type, $context, $priority, $callback_args );
to see how it works see:
http://codex.wordpress.org/Function_Reference/add_meta_box