Last active
October 24, 2018 01:59
-
-
Save leavlzi/a12e4de9e06a0a567a6d0ac516700ce3 to your computer and use it in GitHub Desktop.
Wordpress function add_meta_box() Sample https://9iphp.com/opensystem/wordpress/769.html
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 | |
$new_meta_boxes = | |
array( | |
"description" => array( | |
"name" => "description", | |
"std" => "", | |
"title" => "description:"), | |
"keywords" => array( | |
"name" => "keywords", | |
"std" => "", | |
"title" => "keywords:") | |
); | |
function new_meta_boxes() { | |
global $post, $new_meta_boxes; | |
foreach($new_meta_boxes as $meta_box) { | |
$meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true); | |
if($meta_box_value == "") | |
$meta_box_value = $meta_box['std']; | |
echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />'; | |
echo'<h4>'.$meta_box['title'].'</h4>'; | |
echo '<textarea cols="60" rows="3" name="'.$meta_box['name'].'_value">'.$meta_box_value.'</textarea><br />'; | |
} | |
} | |
function create_meta_box() { | |
global $theme_name; | |
if ( function_exists('add_meta_box') ) { | |
add_meta_box( 'new-meta-boxes', 'new meta box', 'new_meta_boxes', 'post', 'normal', 'high' ); | |
} | |
} | |
function save_postdata( $post_id ) { | |
global $post, $new_meta_boxes; | |
foreach($new_meta_boxes as $meta_box) { | |
if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) { | |
return $post_id; | |
} | |
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; | |
} | |
$data = $_POST[$meta_box['name'].'_value']; | |
if(get_post_meta($post_id, $meta_box['name'].'_value') == "") | |
add_post_meta($post_id, $meta_box['name'].'_value', $data, true); | |
elseif($data != get_post_meta($post_id, $meta_box['name'].'_value', true)) | |
update_post_meta($post_id, $meta_box['name'].'_value', $data); | |
elseif($data == "") | |
delete_post_meta($post_id, $meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true)); | |
} | |
} | |
add_action('admin_menu', 'create_meta_box'); | |
add_action('save_post', 'save_postdata'); | |
?> |
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 echo $description = get_post_meta($post->ID, "description_value", true); ?> | |
<?php echo $keywords = get_post_meta($post->ID, "keywords_value", true); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment