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!
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:1. Create the required field information
Here we will add two custom fields, named _description_value and _keywords_value respectively. You can give The following array adds multiple elements to achieve the purpose of adding multiple custom fields. The first element of the array, name, is the name of the custom field. In this code, the name of the custom field is the name value plus _value to prevent conflicts with other codes, such as _description_value; std is self Define the default value of the field. When you publish an article and no value is filled in the custom field, the default value will be used; title is the title of the custom field module, such as "Abstract", "Category" and "Tag" on the article editing page ", these are module names.$new_meta_boxes =array( "description" => array( "name" => "_description", "std" => "这里填默认的网页描述", "title" => "网页描述:"), "keywords" => array( "name" => "_keywords", "std" => "这里填默认的网页关键字", "title" => "关键字:"));
2. Create a custom field input box
The following code will be used to create a custom field and input box, just copy itfunction 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 id="meta-box-title">'.$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__) ).'" />';}
3. Create a custom field module
The following code will add a custom field module to the article editing page, which uses WordPress’s add module functionadd_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' ); }}
4. Save article data
All preparations have been made before, the most important thing is to save the data in our custom fields information.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); }}
5. Connect the function to the specified action
This is the last step and the most important step, we have to do The purpose is to connect the function to the specified action (action) to let the WordPress program execute the function we wrote before:add_action('admin_menu', 'create_meta_box'); add_action('save_post', 'save_postdata');
<?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.'" />'; } ?>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

To build a website using WordPress hosting, you need to: select a reliable hosting provider. Buy a domain name. Set up a WordPress hosting account. Select a topic. Add pages and articles. Install the plug-in. Customize your website. Publish your website.

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

WordPress editing dates can be canceled in three ways: 1. Install the Enable Post Date Disable plug-in; 2. Add code in the functions.php file; 3. Manually edit the post_modified column in the wp_posts table.

A step-by-step guide to replacing a header image of WordPress: Log in to the WordPress dashboard and navigate to Appearance >Theme. Select the topic you want to edit and click Customize. Open the Theme Options panel and look for the Site Header or Header Image options. Click the Select Image button and upload a new head image. Crop the image and click Save and Crop. Click the Save and Publish button to update the changes.

The steps to create a custom header in WordPress are as follows: Edit the theme file "header.php". Add your website name and description. Create a navigation menu. Add a search bar. Save changes and view your custom header.

WordPress Error Resolution Guide: 500 Internal Server Error: Disable the plug-in or check the server error log. 404 Page not found: Check permalink and make sure the page link is correct. White Screen of Death: Increase the server PHP memory limit. Database connection error: Check the database server status and WordPress configuration. Other tips: enable debug mode, check error logs, and seek support. Prevent errors: regularly update WordPress, install only necessary plugins, regularly back up your website, and optimize website performance.

Enable comments in WordPress website: 1. Log in to the admin panel, go to "Settings" - "Discussions", and check "Allow comments"; 2. Select a location to display comments; 3. Customize comments; 4. Manage comments, approve, reject or delete; 5. Use <?php comments_template(); ?> tags to display comments; 6. Enable nested comments; 7. Adjust comment shape; 8. Use plugins and verification codes to prevent spam comments; 9. Encourage users to use Gravatar avatar; 10. Create comments to refer to
