更新帖子时未保存Meta Box数据

时间:2018-07-19 01:16:30

标签: php wordpress

我正在尝试按照教程添加元框。我已经对代码进行了三遍检查,使其与本教程的代码相匹配(在这种情况下,保存了meta box数据),但是在我看来,它没有保存。这是我的代码。

<?php
/*
Plugin Name: Custom Post Meta Box
Plugin URI: http://acetronaut.com
Description: Demonstrates how to implement a custom posts meta box into 
wordpress
Version: 1.0.0
Author: Acetronaut
Author URI: http://acetronaut.com
License: GPL2
*/

function acetrnt_add_admin_styles() {
    wp_enqueue_style( 'acetrnt-admin', plugins_url( 'custom-post-meta-box/css/admin.css' ) );
}
add_action( 'admin_enqueue_scripts', 'acetrnt_add_admin_styles' );

function acetrnt_add_meta_box() {   
    add_meta_box(
        'acetrnt_audio',                // The ID for the meta box
        'Add MP3',                      //The title of the meta box
        'acetrnt_display_meta_box',     //The function for rendering the markup
        'post',                         // We'll only be displaying this on post pages
        'side',                         //Where the meta box should appear
        'core'                          // The priority of where the meta box whould be displayed
    );
}
add_action( 'add_meta_boxes', 'acetrnt_add_meta_box' );

function acetrnt_display_meta_box( $post ) {
    wp_nonce_field( plugin_basename( __FILE__ ), 'acetrnt_nonce_field' ); ?>

    <label id="mp3-title" for="mp3-title">Title of MP3</label>
    <input type="text" id="mp3-title" name="mp3-title" value="<?php echo esc_attr( get_post_meta( $post->ID, true ) ); ?>" placeholder="Your Song By Elton John" />

    <label id="mp3-file" for="mp3-file">MP3 File</label>
    <input type="file" id="mp3-file" name="mp3-file" value="" />

    <?php 
}

function acetrnt_save_meta_box_data( $post_id ) {
    if ( acetrnt_user_can_save( $post_id, 'acetrnt-nonce-field' ) ) {
        if ( isset( $_POST['mp3-title'] ) && 0 < count( strlen( trim( $_POST['mp3-title'] ) ) ) ) {
            $mp3_title = $_POST['mp3-title'];
            update_post_meta( $post_id, 'mp3-title', $mp3_title );
        }
    }
}
add_action( 'save_post', 'acetrnt_save_meta_box_data' );

function acetrnt_user_can_save( $post_id, $nonce ) {
    // Is this an autosave?
    $is_autosave = wp_is_post_autosave( $post_id );
    // Is this a revision?
    $is_revision = wp_is_post_revision( $post_id );
    // Is the nonce valid?
    $is_valid_nonce = ( isset( $_POST[ $nonce] ) && wp_verify_nonce( $_POST[ $nonce ], plugin_basename( __FILE__ ) ) );
    // Return true if the user is able to save the file
    return ! ( $is_autosave || $is_revision ) && $is_valid_nonce;
}
?>

我把头撞在墙上是罪魁祸首。我的代码中有什么错误吗?我也尝试过退出php并使用常规的html代码,但这不起作用。

0 个答案:

没有答案