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 | |
/** | |
* Add meta box | |
* | |
* @param post $post The post object | |
* @link https://codex.wordpress.org/Plugin_API/Action_Reference/add_meta_boxes | |
*/ | |
function food_add_meta_boxes( $post ){ | |
add_meta_box( 'food_meta_box', __( 'Nutrition facts', 'food_example_plugin' ), 'food_build_meta_box', 'food', 'side', 'low' ); | |
} |
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 | |
/** | |
* Build custom field meta box | |
* | |
* @param post $post The post object | |
*/ | |
function food_build_meta_box( $post ){ | |
// our code here | |
} |
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 | |
wp_nonce_field( basename( __FILE__ ), 'food_meta_box_nonce' ); |
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 | |
// retrieve the _food_cholesterol current value | |
$current_cholesterol = get_post_meta( $post->ID, '_food_cholesterol', true ); | |
// retrieve the _food_carbohydrates current value | |
$current_carbohydrates = get_post_meta( $post->ID, '_food_carbohydrates', true ); | |
// stores _food_vitamins array | |
$current_vitamins = ( get_post_meta( $post->ID, '_food_vitamins', true ) ) ? get_post_meta( $post->ID, '_food_vitamins', true ) : array(); |
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
?> | |
<div class='inside'> | |
<h3><?php _e( 'Carbohydrates', 'food_example_plugin' ); ?></h3> | |
<p> | |
<input type="text" name="carbohydrates" value="<?php echo $current_carbohydrates; ?>" /> | |
</p> | |
</div> | |
<?php |
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
<h3><?php _e( 'Cholesterol', 'food_example_plugin' ); ?></h3> | |
<p> | |
<input type="radio" name="cholesterol" value="0" <?php checked( $current_cholesterol, '0' ); ?> /> Yes<br /> | |
<input type="radio" name="cholesterol" value="1" <?php checked( $current_cholesterol, '1' ); ?> /> No | |
</p> |
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
<h3><?php _e( 'Vitamins', 'food_example_plugin' ); ?></h3> | |
<p> | |
<input type="checkbox" name="vitamins[]" value="Vitamin A" <?php checked( ( in_array( 'Vitamin A', $current_vitamins ) ) ? 'Vitamin A' : '', 'Vitamin A' ); ?> />Vitamin A <br /> | |
<input type="checkbox" name="vitamins[]" value="Thiamin (B1)" <?php checked( ( in_array( 'Thiamin (B1)', $current_vitamins ) ) ? 'Thiamin (B1)' : '', 'Thiamin (B1)' ); ?> />Thiamin (B1) <br /> | |
<!-- more vitamins here --> | |
</p> |
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 | |
( in_array( 'Thiamin (B1)', $current_vitamins ) ) ? 'Thiamin (B1)' : '' |
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 | |
// an array of values | |
$vitamins = array( 'Vitamin A', 'Thiamin (B1)', 'Riboflavin (B2)', 'Niacin (B3)', 'Pantothenic Acid (B5)', 'Vitamin B6', 'Vitamin B12', 'Vitamin C', 'Vitamin D', 'Vitamin E', 'Vitamin K' ); | |
// stores _food_vitamins array | |
$current_vitamins = ( get_post_meta( $post->ID, '_food_vitamins', true ) ) ? get_post_meta( $post->ID, '_food_vitamins', true ) : array(); | |
?> | |
<div class='inside'> | |
<h3><?php _e( 'Vitamins', 'food_example_plugin' ); ?></h3> | |
<p> |
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 | |
/** | |
* Build custom field meta box | |
* | |
* @param post $post The post object | |
*/ | |
function food_build_meta_box( $post ){ | |
// make sure the form request comes from WordPress | |
wp_nonce_field( basename( __FILE__ ), 'food_meta_box_nonce' ); |
OlderNewer