【发布时间】:2017-07-13 16:44:37
【问题描述】:
我目前正在学习如何创建 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
请帮我看看我哪里出了问题或者过去两年发生了什么变化。
【问题讨论】:
-
任何实时链接??
-
你能不能写一段代码,用 print_r 打印 $fd_stored_meta,并检查值是否到来?在此之后 $fd_stored_meta = get_post_meta( $post->ID );
-
@Umair 如何实时分享此代码?我只知道 Codepen 和 JSfiddle,但它们不支持 PHP,是吗?请告诉我
-
@Trisup 我把代码放在 $fd_stored_meta = get_post_meta( $post->ID );它给我的只是以下内容: Array ( [_vc_post_settings] => Array ( [0] => a:1:{s:10:"vc_grid_id";a:0:{}} ) [_edit_last] => Array ( [0] => 1 ) [_edit_lock] => 数组 ([0] => 1487853889:1 ) )
-
@Tristup 数据库表中也没有条目。然而,该代码适用于教程中的那个人
标签: php database wordpress youtube meta-boxes