当使用jquery在wordpress中提交表单时,我想更新post meta。为此,我写了以下
在页面模板中,我在 get_footer();
之后编写以下函数<script>
jQuery(function($){
$( "#submit_prce" ).submit(function(e) {
var ajaxurl="<?php echo get_stylesheet_directory_uri().'/functions.php';?>";
e.preventDefault();
var data = {
action: 'update_price',
whatever: $("#price").val()
};
jQuery.post(ajaxurl, data, function(response) {
alert('Got this from the server: ' + response);
e.preventDefault();
});
});
});
</script>
我在functions.php中编写以下函数
add_action('wp_ajax_update_price', 'update_pp_price');
add_action('wp_ajax_nopriv_update_price', 'update_pp_price');
function update_pp_price($given_price){
update_post_meta( 376, '_regular_price', $given_price );
update_post_meta( 376, '_price', $given_price );
}
但是由于某些原因它不起作用。其显示以下错误
从服务器上收到了此邮件:
<b>Fatal error</b>: Call to undefined function add_filter() in <b>/home/myshop/public_html/wp-content/themes/mytheme-child/functions.php</b> on line <b>14</b><br />
答案 0 :(得分:1)
URL错误,应使用以下代码。调整ajaxurl指向admin-ajax.php
<script>
jQuery(function($){
$( "#submit_prce" ).submit(function(e) {
var ajaxurl="/wp-admin/admin-ajax.php";
e.preventDefault();
var data = {
action: 'update_price',
whatever: $("#price").val()
};
jQuery.post(ajaxurl, data, function(response) {
alert('Got this from the server: ' + response);
e.preventDefault();
});
});
});
</script>