Home >CMS Tutorial >WordPress >A brief analysis of how to add a custom field panel in WordPress
How to add a custom field panel in WordPress? The following article will introduce to you how to add a custom field panel in WordPress. I hope it will be helpful to you!
## When we write articles in WordPress, we often use some custom fields, such as the two meta tags of web page description and keywords. Regarding these two For a tag, you can read an article I wrote before:WordPress sets independent Description and Keywords
Usually when adding custom fields and their values, we do it manually It seems a bit troublesome to select the corresponding field in the drop-down box of the "Custom Field" module, then enter its value, and finally submit and wait for a short period of time. So is it possible to create a separate panel for these commonly used custom fields and just fill in the content directly? Just like article tags, you can add tags directly without submitting them separately. The answer is yes, here is the rendering:$new_meta_boxes =array( "description" => array( "name" => "_description", "std" => "这里填默认的网页描述", "title" => "网页描述:"), "keywords" => array( "name" => "_keywords", "std" => "这里填默认的网页关键字", "title" => "关键字:"));
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'<h3>'.$meta_box['title'].'</h3>'; // 自定义字段输入框 echo '<textarea cols="60" rows="3" name="'.$meta_box['name'].'_value">'.$meta_box_value.'</textarea><br />'; } echo '<input type="hidden" name="ludou_metaboxes_nonce" id="ludou_metaboxes_nonce" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';}
add_meta_box. This is exactly the opposite of what was done in the previous article WordPress article editing page to delete related modules.
function create_meta_box() { if ( function_exists('add_meta_box') ) { add_meta_box( 'new-meta-boxes', '自定义模块', 'new_meta_boxes', 'post', 'normal', 'high' ); }}
function save_postdata( $post_id ) { global $new_meta_boxes; if ( !wp_verify_nonce( $_POST['ludou_metaboxes_nonce'], plugin_basename(__FILE__) )) return; if ( !current_user_can( 'edit_posts', $post_id )) return; foreach($new_meta_boxes as $meta_box) { $data = $_POST[$meta_box['name'].'_value']; if($data == "") delete_post_meta($post_id, $meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true)); else update_post_meta($post_id, $meta_box['name'].'_value', $data); }}
add_action('admin_menu', 'create_meta_box'); add_action('save_post', 'save_postdata');Okay, that’s all we have to do, now you can To call these two custom fields in your theme, use a text editor to open header.php in the theme directory, and copy the following code to to customize the description and keywords for your web page. Tags, please use the search engine for more specific operations:
<?phpif (is_single()) { // 自定义字段名称为 description_value $description = get_post_meta($post->ID, "_description_value", true); // 自定义字段名称为 keywords_value $keywords = get_post_meta($post->ID, "_keywords_value", true); // 去除不必要的空格和HTML标签 $description = trim(strip_tags($description)); $keywords = trim(strip_tags($keywords)); echo '<meta name="description" content="'.$description.'" /> <meta name="keywords" content="'.$keywords.'" />'; } ?>Recommended learning: "
WordPress Tutorial"
The above is the detailed content of A brief analysis of how to add a custom field panel in WordPress. For more information, please follow other related articles on the PHP Chinese website!