Wordpress自定义字段钩子不工作


Wordpress custom fields hook not working

我试图在Wordpress (Woocommerce)上为产品添加更多字段。我添加了如下html字段:

<tr>
    <td><input class="backend_price_accommodatie" name="g/w/l_prijs" type="text" placeholder="g/w/l prijs" width="10"></td>
    <td><input class="backend_price_accommodatie" name="t/t/i_prijs" type="text" placeholder="Telefoon/tv/internet prijs" width="10"></td>
    <td><input class="backend_price_accommodatie" name="heffingen_prijs" type="text" placeholder="Heffingen prijs" width="10"></td>
    <td><input class="backend_price_accommodatie" name="verzekering_prijs" type="text" placeholder="Woonverzekering prijs" width="10"></td>
</tr>

我正在使用一个钩子来保存这个信息到数据库,但是我不能让它工作。

add_action( 'save_post', 'wc_prices_save_product' );
function wc_prices_save_product( $pID ) {
global $globals;
// If this is a auto save do nothing, we only save when update button is clicked
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; }
    add_post_meta( $pID, 'g/w/l_prijs', $_POST['g/w/l_prijs'], true ) || update_post_meta( $pID, 'g/w/l_prijs', $_POST['g/w/l_prijs'] ); 
    add_post_meta( $pID, 't/t/i_prijs', $_POST['t/t/i_prijs'], true ) || update_post_meta( $pID, 't/t/i_prijs', $_POST['t/t/i_prijs'] ); 
    add_post_meta( $pID, 'heffingen_prijs', $_POST['heffingen_prijs'], true ) || update_post_meta( $pID, 'heffingen_prijs', $_POST['heffingen_prijs'] ); 
    add_post_meta( $pID, 'verzekering_prijs', $_POST['verzekering_prijs'], true ) || update_post_meta( $pID, 'verzekering_prijs', $_POST['verzekering_prijs'] );
}
?>

我错过了什么吗?这看起来很简单,但似乎并没有节省任何东西。或者由于某些原因没有在字段中显示保存的信息

在保存文章时,不需要add_post_meta(),因为update_post_meta()将在不存在的情况下创建元字段,或者在存在的情况下更新现有的元字段。

要在仪表板中显示,您需要提取这些元字段值并显示它们。

例如

在元框中,回调函数,其中添加了额外的字段

<tr>
    <td><input class="backend_price_accommodatie" name="g/w/l_prijs" type="text" placeholder="g/w/l prijs" width="10"></td>
    <td><input class="backend_price_accommodatie" name="t/t/i_prijs" type="text" placeholder="Telefoon/tv/internet prijs" width="10"></td>
    <td><input class="backend_price_accommodatie" name="heffingen_prijs" type="text" placeholder="Heffingen prijs" width="10"></td>
    <td><input class="backend_price_accommodatie" name="verzekering_prijs" type="text" placeholder="Woonverzekering prijs" width="10"></td>
</tr>

使用下面的代码

global $post;
<tr>
    <td><input class="backend_price_accommodatie" name="g/w/l_prijs" type="text" placeholder="g/w/l prijs" width="10" value="<?php echo get_post_meta( $post->ID, 'g/w/l_prijs', true); ?>"></td>
...
</tr>