Wordpress Php函数通过ajax传递变量来保存用户元数据


Wordpress Php function passing variables through ajax to save user meta

自上次发布以来,所有代码都已更新我在写这段代码时遇到了问题。这是给客户的。它应该做的是当用户以客户身份登录网站时。他们填写了产品的标准:年份、品牌、型号、选项1。这会生成我的客户拥有的产品列表。他想要一个按钮来保存客户的标准,这样他们就可以在用户区点击它来调出产品,而无需再次进行搜索。

我在ajax和通过标准方面遇到了问题。因此,在我的js文件中,这就是我为ajax函数所做的:

function ajaxAddVehicleMeta(){
var userId = ''; // fill this in to somehow acquire the userID client-side...
var year = '<?php echo $year; ?>';
var make= '<?php echo $make; ?>';
var model= '<?php echo $model; ?>';
var option1= '<?php echo $option1; ?>';
var stdoropt= '<?php echo $stdoropt; ?>'; 
jQuery.ajax({
   type: 'POST',
        data: { 
            action: 'saveVeh', 
            userId: userId, 
            year:year, 
            make:make, 
            model:model, 
            option1:option1, 
            stdoropt:stdoropt },
        url: comm_custom.ajaxurl, //this is the url found in functions
        success: function(msg){
     alert( "Data Saved: User ID " + userId + "." );
   },
   error: function(msg){
      alert( "Data not saved: User ID " + userId + ".");
   }
});
}

在我的functions.php文件中,我有:

add_action( 'wp_ajax_saveVeh', 'saveVeh' );
add_action( 'wp_ajax_nopriv_saveVeh', 'saveVeh' ); 
function saveVeh(){
$year = $_POST['year'];
$make = $_POST['make'];
$model = $_POST['model'];
$option1 = $_POST['option1'];
$stdoropt = $_POST['stdoropt'];
    //if ( is_user_logged_in() ) {
        $user_id = get_current_user_id();
        if(!$user_id):
                echo 'failed, not logged in';
                exit;
            endif;  
        if($year){
            $vehicle1 = $year.' '.$make.' '.$model.' '.$option1.' / '.$stdoropt;
        }else if($_POST['width']){
            $vehicle1 = $_POST['width']. ' ' .$_POST['height']. ' ' .$_POST['rim'];
        }
            update_user_meta( $user_id, '_vehicle1', $vehicle1);            
    //}

页面上有以下链接:更新

<a href="#" onclick="ajaxAddVehicleMeta()" class="vehicle-btn" >Save Search Criteria</a>

我发出警报,它告诉我它成功了。但它没有将其保存在用户元中。我做错什么了吗?

comm_custom.ajaxurl在排队脚本中是admin-ajax.php,并对其进行本地化。

上面的代码中有一些问题。。js变量需要在使用之前进行设置,否则会引发错误。

js

        function ajaxAddVehicleMeta(){
        var tireyear= jQuery('#tireyear').val(); // select id of input change to whatever it actually is
        var width= jQuery('#width').val(); // select id of input change to whatever it actually is
        jQuery.ajax({
           type: 'POST',
                data: { 
                    action: 'saveVeh', 
                    userId: userId, 
                    tireyear: tireyear,
                    width: width            
                    },
                url: comm_custom.ajaxurl, //this is the url found in functions
                success: function(msg){
             alert( "Data Saved: User ID " + userId + "." );
           },
           error: function(msg){
              alert( "Data not saved: User ID " + userId + ".");
           }
        });
    }

php

        add_action( 'wp_ajax_saveVeh', 'saveVeh' );
    add_action( 'wp_ajax_nopriv_saveVeh', 'saveVeh' ); 
    function saveVeh(){

            $user_id = get_current_user_id();
            if(!$user_id):
                echo 'failed, not logged in';
                exit;
            endif;  
            if($_POST['tire-year']){
                $vehicle1 = $_POST['tire-year'].' '.$_POST['make'].' '.$_POST['model'].' '.$_POST['option1'].' / '.$_POST['stdoropt'];
            }else if($_POST['width']){
                $vehicle1 = $_POST['width']. ' ' .$_POST['height']. ' ' .$_POST['rim'];
            } else {
                echo 'failed, no data';
                exit;
            }
            update_user_meta( $user_id, '_vehicle1', $vehicle1);            
            echo $user_id; // you need to return something for ajax success to pick up..
            exit;
    }

html:--函数中没有可传递值的参数。所以把它们留空。。

<a href="#" onclick="ajaxAddVehicleMeta()" class="vehicle-btn" >Save Vehicle 1</a>