dongxixia6399 2018-07-19 01:16
浏览 42

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

I am trying to add a meta box, following a tutorial. I have triple checked the code that it matches that of the tutorial (in which case, the meta box data IS saving), but on mine, it is not saving. This is my code.

<?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;
}
?>

I am banging my head against the wall as to the culprit. Is there anything in my code that is wrong? I have also tried dropping out of php and using regular html code, but that does not work.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?