gpt4 book ai didi

javascript - WordPress 自定义元框无法保存

转载 作者:行者123 更新时间:2023-12-03 03:40:09 25 4
gpt4 key购买 nike

我有以下代码,它将自定义元框添加到 WordPress 页面,但由于某种原因,它不会保存所选选项。任何想法有什么问题吗?

谢谢。

 // Add Header Select Option MetaBox
function alfie_header_select_meta() {
add_meta_box( 'alfie_header_select_meta', __( 'Header Style', 'alfie' ), 'alfie_header_select_meta_callback', 'page', 'side' );
}
add_action( 'add_meta_boxes', 'alfie_header_select_meta' );

function alfie_header_select_meta_callback( $post ) {
$values = get_post_custom( $post->ID );
$selected = isset( $values['alfie_selected_header'] ) ? esc_attr( $values['alfie_selected_header'][0] ) : ”;
wp_nonce_field( 'alfie_header_select_meta_nonce', 'alfie_header_select_meta_nonce' );
?>
<p>
<select name="alfie_selected_header" id="alfie_selected_header">
<option value="alfie-header-default" <?php selected( $selected, 'default' ); ?>>Default</option>
<option value="alfie-header-style-minimal" <?php selected( $selected, 'minimal' ); ?>>Minimal</option>
<option value="alfie-header-test" <?php selected( $selected, 'test' ); ?>>Just a test option</option>
</select>
</p>
<?php
}

function alfie_meta_save( $post_id ) {
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'alfie_header_select_meta_nonce' ] ) && wp_verify_nonce( $_POST[ 'alfie_header_select_meta_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';

if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}

if( isset( $_POST[ 'alfie_selected_header' ] ) ) {
update_post_meta( $post_id, 'alfie_selected_header', sanitize_text_field( $_POST[ 'alfie_selected_header' ] ) );
}
}
add_action( 'save_post', 'alfie_meta_save' );

最佳答案

这是工作的调试代码。正在保存帖子元。但由于字符串值和获取元数据的方式不同,它没有显示。只需对单个字段使用 get_post_meta 即可。

 // Add Header Select Option MetaBox
function alfie_header_select_meta() {
add_meta_box( 'alfie_header_select_meta', __( 'Header Style', 'alfie' ), 'alfie_header_select_meta_callback', 'page', 'side' );
}
add_action( 'add_meta_boxes', 'alfie_header_select_meta' );

function alfie_header_select_meta_callback( $post ) {
$selected = get_post_meta( $post->ID,'alfie_selected_header',true);
wp_nonce_field( 'alfie_header_select_meta_nonce', 'alfie_header_select_meta_nonce' );
?>
<p>
<select name="alfie_selected_header" id="alfie_selected_header">
<option value="alfie-header-default" <?php selected( $selected, 'alfie-header-default' ); ?>>Default</option>
<option value="alfie-header-style-minimal" <?php selected( $selected, 'alfie-header-style-minimal' ); ?>>Minimal</option>
<option value="alfie-header-test" <?php selected( $selected, 'alfie-header-test' ); ?>>Just a test option</option>
</select>
</p>
<?php
}

function alfie_meta_save( $post_id ) {
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'alfie_header_select_meta_nonce' ] ) && wp_verify_nonce( $_POST[ 'alfie_header_select_meta_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';

if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}

if( isset( $_POST[ 'alfie_selected_header' ] ) ) {
update_post_meta( $post_id, 'alfie_selected_header', sanitize_text_field( $_POST[ 'alfie_selected_header' ] ) );
}
}
add_action( 'save_post', 'alfie_meta_save' );

关于javascript - WordPress 自定义元框无法保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45663817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com