dongshou1856 2017-02-23 09:31
浏览 52
已采纳

将WordPress Post Meta保存到数据库不起作用

I am currently working through tutorials of how to create a WordPress plugin but for some reason my code does not work when it comes to the part where the Post Meta data is saved to the database. When I click Publish or Update it just refreshes the page and the content in the fields are gone. This tutorial was created in 2015 and I'm not sure if I am missing something in my code or if changes were made to how WordPress saves the data. Here is a link to the tutorial specific video - https://www.youtube.com/watch?v=waS0gCkuLeM&t=64s&index=10&list=PLIjMj0-5C8TI7Jwell1rTvv5XXyrbKDcy

And here is my code from that tutorial below:

<?php

function fd_add_custom_metabox(){

add_meta_box(

        'fd_meta',
        'Pricing Table',
        'fd_meta_callback',
        'table',
        'normal',
        'core'

        );}

add_action('add_meta_boxes', 'fd_add_custom_metabox');

function fd_meta_callback( $post ){ //needed for the $callback parameter above
    wp_nonce_field(basename( __FILE__ ), 'fd_table_nonce');
    $fd_stored_meta = get_post_meta( $post->ID );


    ?>

<div>
    <div class="meta-row">
        <div class="meta-th">
            <label for="table-title" class="fd-row-title">Title</label>
        </div>
        <div class="meta-td">
            <input type="text" name="table_id" id="table-title" value="<?php if( !empty ($fd_stored_meta['table_id'])) {echo esc_attr($fd_stored_meta['table_id'][0] ); } ?> ">
        </div>
    </div>
</div>
<div>
    <div class="meta-row">
        <div class="meta-th">
            <label for="table-subtitle" class="fd-row-title">Subtitle</label>
        </div>
        <div class="meta-td">
            <input type="text" name="table_subtitle" id="table-subtitle" value="<?php if( !empty ($fd_stored_meta['table_subtitle'])) {echo esc_attr($fd_stored_meta['table_subtitle'][0]);} ?> ">
        </div>
    </div>
</div>
<div>
    <div class="meta-row">
        <div class="meta-th">
            <label for="table-recurrence" class="fd-row-title">Recurrence</label>
        </div>
        <div class="meta-td">
            <input type="text" name="table_recurrence" id="table-recurrence" value="">
        </div>
    </div>
</div>
<div>
    <div class="meta-row">
        <div class="meta-th">
            <label for="table-price" class="fd-row-title">Price</label>
        </div>
        <div class="meta-td">
            <input type="text" name="table_price" id="table-price" value="">
        </div>
    </div>
</div>
<div>
    <div class="meta-row">
        <div class="meta-th">
            <span>Features</span>
        </div>
        <div class="meta-td">

            <textarea name="table_features" rows="8" cols="50"></textarea>
        </div>
    </div>
</div>
<div class="meta">
    <div class="meta-th">
        <span>Some Description</span>
    </div>
</div>
<div class="meta-editor">
<?php

   $content = get_post_meta($post->ID, 'some_description', true);
   $editor = 'some_description';
   $settings = array(
     'textarea_rows' => 8,
     'media_buttons' => true,

   );

   wp_editor($content, $editor, $settings);
?>
    </div>

<?php


}

function fd_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[ 'fd_table_nonce']) && wp_verify_nonce( $_POST['fd_table_nonce'], basename( __FILE__ ) ) )? 'true' : 'false';

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

    if (isset ($_POST['table_id'])){
        update_post_meta( $post_id, 'table_id', sanitize_text_field($_POST[ 'table_id' ]));
    }
    if (isset ($_POST['table_subtitle'])){
        update_post_meta( $post_id, 'table_subtitle', sanitize_text_field($_POST[ 'table_subtitle' ]));
    }
    if (isset ($_POST['some_description'])){
        update_post_meta( $post_id, 'some_description', sanitize_text_field($_POST[ 'some_description' ]));
    }
}
add_action('save_post,', 'fd_meta_save');

Please note that I only have PHP in the first two inputs to capture the data and the wp-editor which is last

Please help to show me where I went wrong or what has changed in the past two years.

  • 写回答

1条回答 默认 最新

  • dongpu1331 2017-03-09 06:05
    关注

    An update on this question. It appears that I had a comma at the very end just after the post - so it should look like this add_action('save_post', 'fd_meta_save'); instead of this add_action('save_post,', 'fd_meta_save');

    This fixed my problem. Because it was read as a string, it could not be picked up as a syntax error.

    Lesson learnt - never stop looking for syntax errors even if they don't get detected by the program

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装