2017-02-23 95 views
0

我目前正在通過如何創建WordPress插件的教程,但由於某些原因,當涉及到發佈元數據保存到數據庫的部分時,我的代碼無法工作。當我點擊「發佈」或「更新」時,只刷新頁面,字段中的內容不見了。本教程是在2015年創建的,我不確定是否在我的代碼中丟失了某些內容,或者如果對WordPress如何保存數據進行了更改。下面是本教程特定視頻的鏈接 - https://www.youtube.com/watch?v=waS0gCkuLeM&t=64s&index=10&list=PLIjMj0-5C8TI7Jwell1rTvv5XXyrbKDcy將WordPress發佈元數據保存到數據庫不工作

,這裏是從下教程我的代碼:

<?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編輯

請幫助告訴我我錯了什麼或過去兩年發生了什麼變化。

+0

任何活動鏈接? –

+0

您可以寫一段代碼,使用print_r打印$ fd_stored_meta,並檢查值是否到來?這個$ fd_stored_meta = get_post_meta($ post-> ID)後的 ; – Tristup

+0

@Umair我該如何分享這段代碼?我只知道Codepen和JSfiddle,但他們不支持PHP,是嗎?請讓我知道 – Feyt

回答

0

此問題的更新。看起來我在帖子後面的最後有一個逗號 - 所以它應該看起來像這樣:add_action('save_post','fd_meta_save');而不是此add_action('save_post,','fd_meta_save');

這解決了我的問題。因爲它是以字符串的形式讀取的,所以不能將其作爲語法錯誤。

獲得的教訓 - 即使沒有被程序檢測到也不會停止查找語法錯誤