我正在嘗試向頁面屬性側邊欄添加一個新的選擇欄位,但我無法讓它保存值。
這就是我目前所擁有的:
<?php
function addMyMeta() {
add_meta_box( 'my_custom_metabox', 'My Meta', 'setMyMeta', 'page', 'side', 'default' );
}
add_action( 'add_meta_boxes', 'addMyMeta' );
function setMyMeta() {
global $page;
$value = get_post_meta( $page->ID, 'my_custom_metabox', true );
?>
<fieldset>
<select name="my_custom_metabox" id="my_custom_metabox" autocomplete="off">
<option value="">Default</option>
<option value="my-value" <?php if($value == 'my-value') {echo ' selected ';}; ?>>My Value</option>
</select>
</fieldset>
<?php
}
function saveMyMeta( $page_id, $page ) {
if ( !isset( $_POST['my_custom_metabox'] ) ) {
update_post_meta( $page->ID, 'my_custom_metabox', $_POST['my_custom_metabox'] );
}
}
add_action( 'save_post', 'saveMyMeta', 1, 2 );
?>
uj5u.com熱心網友回復:
更改global $page;為global $post;和 $value = get_post_meta( $post->ID, 'my_custom_metabox', true );
function setMyMeta() {
global $post;
$value = get_post_meta( $post->ID, 'my_custom_metabox', true );
?>
<fieldset>
<select name="my_custom_metabox" id="my_custom_metabox" autocomplete="off">
<option value="">Default</option>
<option value="my-value" <?php if($value == 'my-value') {echo ' selected ';}; ?>>My Value</option>
</select>
</fieldset>
<?php
}
uj5u.com熱心網友回復:
您應該在函式中使用isset而不是。如果甚至沒有設定值,為什么要運行?!issetsaveMyMetaupdate_post_meta
我更喜歡使用 OOP 添加元框,因為它很簡單,并且讓您不必擔心全域命名空間中的命名沖突。
添加元框時要考慮使用的事項:
- wp_nonce_field()驗證表單的內容來自當前站點上的位置,而不是其他地方。
- selected()用于選擇欄位。
- array_key_exists()檢查密鑰是否存在。
- 使用以下方法在保存或更新之前清理值:sanitize_text_field()、sanitize_textarea_field()、intval()、floatval()等。
- 為save_post操作掛鉤添加
post_type后綴。在你的情況下,它將是。save_post_page
這是完整的 OOP 代碼:
abstract class My_Custom_MetaBox {
/**
* Set up and add the meta box.
*/
public static function add() {
add_meta_box(
'my_custom_metabox', // Unique ID
'My Meta', // Box title
[ self::class, 'html' ], // Content callback, must be of type callable
'page', // Post type
'side', // The context within the screen where the box should display
'default' // Priority
);
}
/**
* Display the meta box HTML to the user.
*/
public static function html( $post ) {
// Add an nonce field so we can check for it later.
wp_nonce_field('my_custom_meta_box', 'my_custom_meta_box_nonce');
$value = get_post_meta($post->ID, '_my_custom_metabox', true);
?>
<fieldset>
<select name="my_custom_metabox" id="my_custom_metabox" autocomplete="off">
<option value="" <?php selected($value, ''); ?>>Default</option>
<option value="my-value" <?php selected($value, 'my-value'); ?>>My Value</option>
</select>
</fieldset>
<?php
}
/**
* Save the meta box selections.
*/
public static function save( int $post_id ) {
// Check if our nonce is set.
if ( !isset($_POST['my_custom_meta_box_nonce']) ) {
return $post_id;
}
$nonce = $_POST['my_custom_meta_box_nonce'];
// Verify that the nonce is valid.
if ( !wp_verify_nonce($nonce, 'my_custom_meta_box') ) {
return $post_id;
}
// If this is an autosave, our form has not been submitted,
// so we don't want to do anything.
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return $post_id;
}
// Check the user's permissions.
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;
}
}
// Saving or Updating the data
if ( array_key_exists('my_custom_metabox', $_POST) ) {
$selected_value = sanitize_text_field($_POST['my_custom_metabox']);
update_post_meta( $post_id, 'my_custom_metabox', $selected_value);
}
}
}
add_action( 'add_meta_boxes', ['My_Custom_MetaBox', 'add'] );
add_action( 'save_post_page', ['My_Custom_MetaBox', 'save'] );
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/525724.html
標籤:phpWordPress
