我正在尝试在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'] );
}
?>
我错过了什么吗?这似乎很简单,但它似乎没有保存任何东西。或者由于某种原因,它没有在字段中显示已保存的信息。
答案 0 :(得分:1)
保存帖子时,不需要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>