代码之家  ›  专栏  ›  技术社区  ›  Abilash Erikson

使用ajax的wordpress update postemta不起作用

  •  0
  • Abilash Erikson  · 技术社区  · 6 年前

    在我的页面模板中,我在 获取页脚();

    <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>
    

    我在中编写了以下函数函数.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 />
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Jasmin Mistry    6 年前

    您应该在下面使用的URL错误代码调整指向点管理的ajaxurl-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>