php - 在帖子编辑页面上显示自定义元框值的问题

标签 php wordpress meta-boxes

我正在尝试将一些自定义元框添加到我的 wordpress 帖子编辑页面。我添加了框(视频 URL),但是当我尝试将帖子中的信息调用到字段中作为设置值时,什么也没有显示。我相信我保存正确,但保存后无法显示,请帮忙。下面是添加和保存元框信息的代码:

<!-- adding the video url meta box prototype data -->
<?php
   add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
        add_meta_box( 'post-video', 'Video URL', 'video_url_meta_box', 'post', 'side', 'high' );
}
?>

<!-- rendering the video url meta box on the post edit page -->
<?php
function video_url_meta_box( $post )
{
$value = get_post_meta( $post->ID, 'video_url_text', true );
wp_nonce_field( 'video_url_nonce', 'meta_box_nonce' );
        ?>
        <p>
        <label for="video_url_text">Youtube or Vimeo URL</label>
        <input type="text" name="video_url_text" id="video_url_text" value="<?php echo $value; ?>" />
        </p>
        <?php
}
?>

<!-- Saving the video url meta box data -->
<?php
add_action( 'save_post', 'video_url_save' );
function video_url_save( $post_id )
{
        //return if we're doing an auto save
        if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
        //return if we can't verify nonce or it isn't there
        if( !isset( $_POST['video_url_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'video_url_nonce' ) ) return;
        //return if the current user can't edit the post
        if( !current_user_can( 'edit_page' ) ) return;
        // save data once all checks are passed
        // make sure the url is set
        if( isset( $_POST['video_url_text'] ) )
                update_post_meta( $post_id, 'video_url_text' );

}
?>

最佳答案

我最终将所有代码移动到一个自定义插件中并使其工作如下所示:

/**
* Adds the meta boxes to the post editing screen
*/
function weshine_meta_boxes() {
add_meta_box( 'video_url_meta', __( 'Video URL', 'video-url-text' ), 'video_url_meta_callback', 'post', 'side', 'high' );
add_meta_box( 'caption_text_meta', __( 'Caption Text', 'thumb-caption-text' ), 'caption_text_meta_callback', 'post', 'side', 'high');
}
add_action( 'add_meta_boxes', 'weshine_meta_boxes' );

/**
 * Outputs the content of the meta box
*/

function video_url_meta_callback( $post ) {
wp_nonce_field( basename( __FILE__ ), 'prfx_nonce' );
$video_url_stored_meta = get_post_meta( $post->ID );
?>

<p>
    <label for="video-url" class="prfx-row-title"><?php _e( 'Enter Youtube or Vimeo URL', 'video-url-text' )?></label>
    <input type="text" name="video-url" id="video-url" value="<?php if ( isset ( $video_url_stored_meta['video-url'] ) ) echo $video_url_stored_meta['video-url'][0]; ?>"$
</p>

<?php
}
/**
 * Saves the custom meta input
 */
function video_url_meta_save( $post_id ) {

// Checks save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'prfx_nonce' ] ) && wp_verify_nonce( $_POST[ 'prfx_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';

// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
    return;
}

// Checks for input and sanitizes/saves if needed
if( isset( $_POST[ 'video-url' ] ) ) {
    update_post_meta( $post_id, 'video-url', sanitize_text_field( $_POST[ 'video-url' ] ) );
}

}
add_action( 'save_post', 'video_url_meta_save' );

关于php - 在帖子编辑页面上显示自定义元框值的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24851765/

相关文章:

wordpress - 无法从 edit.php 中删除插件的元框

php - 自定义主题中的 wordpress 页面选项卡?

PHP + MySQL 3 条件

php - 数据未存储在 PHP 站点的 mysql 数据库表中

jquery - 如何在WordPress中使用媒体上传来上传视频文件?

Wordpress - 定制器设置覆盖以前的设置

wordpress - 如何在背景图片上正确地重新定位报名表和按钮?

php - 在 Woocommerce 单一产品页面的简短描述下显示自定义字段

php - wordpress 创建包含多个条目的元框

javascript - jQuery 向 AJAX 调用或 CSS 解决方案添加另一个功能?