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

标签 php database wordpress youtube meta-boxes

我目前正在研究有关如何创建WordPress插件的教程,但是由于某些原因,关于将Post Meta数据保存到数据库的部分,我的代码无法正常工作。当我单击“发布”或“更新”时,它将刷新页面,并且字段中的内容消失了。本教程创建于2015年,我不确定代码中是否丢失了某些内容,或者是否对WordPress保存数据的方式进行了更改。这是本教程特定视频的链接-https://www.youtube.com/watch?v=waS0gCkuLeM&t=64s&index=10&list=PLIjMj0-5C8TI7Jwell1rTvv5XXyrbKDcy

这是下面该教程中的代码:

<?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');

请注意,我在前两个输入中仅具有PHP来捕获数据,而wp-editor则是最后一个

请帮我看看我在哪里出错了,或者过去两年里发生了什么变化。

最佳答案

有关此问题的更新。帖子发布后,我似乎在结尾处有一个逗号-因此它看起来应该像 add_action('save_post','fd_meta_save'); 代替此 add_action('save_post,','fd_meta_save');

这解决了我的问题。因为它是作为字符串读取的,所以不能将其作为语法错误提取。

经验教训-即使程序未检测到语法错误,也不要停止寻找它们

关于php - 将WordPress Post Meta保存到数据库不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42411739/

相关文章:

php - 需要使用PHP在页面上显示MySQL表

html - 将 gzip 压缩的 css 文件导入 html 标题?

php - 在 Wordpress 中编辑 CSS 媒体查询

php PDO 和 mysql : how to insert a geo point type?

android - 在 Sqlite 中插入行很慢

php - Zend Framework - 日期问题

database - playframework: -- 数据库/测试框架/缓存错误

wordpress - WooCommerce - 增加运费并为 Paypal 网关增加额外费用

php - 根据 WooCommerce 购物车总数和月份范围添加或删除免费产品

php - 我的 JSON 没有被 PHP 解码