Last active
June 21, 2018 14:39
-
-
Save imranrbx/574ce31f56faeca85dce0b9eb3f8c442 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
add_action( 'add_meta_boxes', 'my_meta_box_add' ); | |
add_action( 'save_post', 'save' ); | |
function my_meta_box_add() { | |
add_meta_box( 'my-meta-box-id', 'Select The Chef for Food', 'my_meta_box', 'product', 'normal', 'high' ); | |
} | |
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['myplugin_inner_custom_box_nonce'] ) ) { | |
return $post_id; | |
} | |
$nonce = $_POST['myplugin_inner_custom_box_nonce']; | |
// Verify that the nonce is valid. | |
if ( ! wp_verify_nonce( $nonce, 'myplugin_inner_custom_box' ) ) { | |
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 ( 'page' == $_POST['post_type'] ) { | |
if ( ! current_user_can( 'edit_page', $post_id ) ) { | |
return $post_id; | |
} | |
} else { | |
if ( ! current_user_can( 'edit_post', $post_id ) ) { | |
return $post_id; | |
} | |
} | |
/* OK, it's safe for us to save the data now. */ | |
// Sanitize the user input. | |
$mydata = sanitize_text_field( $_POST['my_meta_box_post_type'] ); | |
// Update the meta field. | |
update_post_meta( $post_id, '_my_meta_value_key', $mydata ); | |
} | |
function my_meta_box( $post ) { | |
?> | |
<p> | |
<label for="my_meta_box_post_type">Post type: </label> | |
<?php | |
wp_nonce_field( 'myplugin_inner_custom_box', 'myplugin_inner_custom_box_nonce' ); | |
$value = get_post_meta( $post->ID, '_my_meta_value_key', true ); | |
?> | |
<select name='my_meta_box_post_type' id='my_meta_box_post_type'> | |
<?php | |
// query for your post type here. | |
$post_type_query = new WP_Query( | |
array ( | |
'post_type' => 'post', | |
'post_status' => 'publish', | |
'posts_per_page' => -1 | |
) | |
); | |
// we need the array of posts | |
$posts_array = $post_type_query->posts; | |
// create a list with needed information | |
// the key equals the ID, the value is the post_title | |
$post_title_array = wp_list_pluck( $posts_array, 'post_title', 'ID' ); | |
foreach ($post_title_array as $id => $title): ?> | |
<option value="<?php echo esc_attr($id); ?>" <?php if($id == $value) echo "selected"; ?>><?php echo esc_html($title); ?></option> | |
<?php endforeach; ?> | |
</select> | |
</p> | |
<?php | |
} | |
add_action( 'woocommerce_single_product_summary','pwspk_add_chef', 45 ); | |
function pwspk_add_chef(){ | |
$id = get_post_meta($post->ID, '_my_meta_value_key', true); | |
$chef = get_post($id); | |
echo "<h2> <a href='".get_the_permalink($id)."'>$chef->post_title</a> </h2>"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment