The following tutorial column will introduce how to add Meta Box to WordPress , I hope it will be helpful to friends in need! The way to add Meta Box in WordPress requires the use of add meta boxes Action. This Action allows us to register Meta Box for any article type. In this Action, we need to use the add_meta_box() method to add Meta Box. Related Information. The code is as follows
function add_rating_meta_box($post_type, $post) { // 需要哪些post type添加推荐指数 Meta Box $types = array( 'post', 'page' ); foreach ( $types as $type ) { add_meta_box( 'rating_meta_box_id', // Meta Box在前台页面中的id,可通过JS获取到该Meta Box '推荐指数', // 显示的标题 'render_rating_meta_box', // 回调方法,用于输出Meta Box的HTML代码 $type, // 在哪个post type页面添加 'side', // 在哪显示该Meta Box 'default' // 优先级 ); } } add_action( 'add_meta_boxes', 'add_rating_meta_box' );
Here we define the custom field that both Post and Page need to recommend the index in the $types array, and then tell WordPress to use the "render_rating_meta_box" method to render the Meta Box, position In the sidebar. Because there is not much content, the sidebar is sufficient. If there is more content, you can change "side" to "advanced" so that the Meta Box will be rendered in the main content area.
Next let’s see how it is rendered
function render_rating_meta_box( $post ) { // 添加 nonce 项用于后续的安全检查 wp_nonce_field( 'rating_nonce_action', 'rating_nonce_name' ); // 获取推荐指数的值 $rating_key = 'rating'; $rating_value = get_post_meta( $post->ID, $rating_key, true ); $rating_value = (int)$rating_value; $html = '<select name="rating_field">'; for ($i = 0; $i <= 10; $i++) { $selected = ''; if ($i == $rating_value) { $selected = 'selected="selected"'; } $html .= sprintf('<option value="%s" %s>%s星</option>', $i, $selected, $i/2); } $html .= '</select>'; echo $html; }
Here we first use wp_nonce_field() to add a nonce field for security checking, then read the value of the recommended index and loop 1 ~10 to output optional values. If it is the same as the recommended index, it will be selected by default. Through the drop-down box, the problem of inconvenient input and inability to verify can be solved. Remember the value of the name attribute of the drop-down box here (rating_field), which will be used to obtain the selected value in the following code.
Finally, when the article is saved, the recommendation index needs to be saved as well
function save_rating_post_data( $post_id ) { // 检查nonce是否设置 if (!isset($_POST['rating_nonce_name'])) { return $post_id; } $nonce = $_POST['rating_nonce_name']; // 验证nonce是否正确 if (!wp_verify_nonce( $nonce, 'rating_nonce_action')) { return $post_id; } // 如果是系统自动保存,则不操作 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return $post_id; } // 检查用户权限 if ($_POST['post_type'] == 'post') { if (!current_user_can('edit_post', $post_id )) { return $post_id; } } $rating_key = 'rating'; // 获取数据 $rating_value = $_POST['rating_field']; // 更新数据 update_post_meta( $post_id, $rating_key, $rating_value ); } add_action( 'save_post', 'save_rating_post_data' );
A series of checks are done here, including the nonce check just set, user permissions check, and automatic exclusion Saved condition. Then use the update_post_meta() method to store the data in the database.
At this point, we have completed the modification of the custom field for the recommendation index, and we can easily select the recommendation index of the article.
etc. . .
Careful friends may have discovered that after applying the above three pieces of code, the function can indeed be realized. However, under the default custom column area, you can see that there is a column called "rating", which is the recommendation index we just selected. If you don’t want it to be displayed under the custom column, you can change $rating_key in the above code to start with an underscore, so that WordPress will not display it. Note that there are two places to change.
// 原来的代码 $rating_key = 'rating'; // 改后的代码 $rating_key = '_rating';
The above is the detailed content of How to add Meta Box to WordPress. For more information, please follow other related articles on the PHP Chinese website!