WordPress AJAX 更新自定义页面中的自定义字段

WordPress AJAX 更新自定义页面中的自定义字段

问题描述:

我正在使用 WordPress 通过下拉更改事件使用 AJAX 和 jQuery 更新我的自定义字段,并且它按照我的要求工作正常.这是我的工作代码.

I am using WordPress to update my custom field with AJAX and jQuery using dropdown change event and it's working fine as per my requirements. Here is my working code.

my-subscriptions.php 页面

<?php 

wp_enqueue_script( 'script', get_template_directory_uri() . '/mindesk-ecommerce/assets/js/custom.js', array ( 'jquery' ), 1.1, true);
$ajaxPageUrl =  admin_url( 'admin-ajax.php'); 

?>

<select id="myid" class="mySubscription" name="mindesk_wc_subscriptions_var_client_user_id" data-action="http://localhost/wordpress/wp-admin/admin-ajax.php" data-subscription-id="635"><option value=""></option><option value="24">mittul</option><option value="10">subscriber</option></select>

custom.js

var prev_val;

jQuery('.mySubscription').focus(function() {
    prev_val = jQuery(this).val();
}).change(function() {
    
     jQuery(this).blur() 
    var success = confirm('Are you sure you want to transfer?');
    if(success)
    {
        var subscriptionId = jQuery(this).data("subscription-id");
        var url = jQuery(this).data("action");        
        var userId = jQuery(this).val();        
        jQuery.ajax({
           type: "POST",
           url: url,           
            data : {
                    action : 'update_subscription_custom_meta',
                    userId : userId,
                    subscriptionId : subscriptionId
            },
           success: function(data)
           {  
              var data = JSON.parse(data);               

               if(data.status == '1')
               {                  
                   alert('Transferred Successfully!!!');
               } 
               else
               {
                   alert('There is some problem transferring the subscription!!!');
               }
           }
         });

    }  
    else
    {
        jQuery(this).val(prev_val);     
        return false; 
    }
});

functions.php

add_action( 'wp_ajax_nopriv_update_subscription_custom_meta', 'update_subscription_custom_meta' );
add_action( 'wp_ajax_update_subscription_custom_meta', 'update_subscription_custom_meta' );

function update_subscription_custom_meta() {
    update_post_meta($_POST['subscriptionId'], 'my_custom_field', $_POST['userId']);
    echo json_encode(array('status'=>1),true);
    exit;
}

正如您在 my-subscriptions.php 文件中看到的,我使用过

As you can see in my-subscriptions.php file I have used

$ajaxPageUrl =  admin_url( 'admin-ajax.php'); 

并且基于 custom.js 文件中的 mySubscription 类,我使用了 update_subscription_custom_meta 作为 action 然后进入 functions.php 文件以更新我的自定义元数据.这一切正常.

And based on mySubscription class in custom.js file, I have used update_subscription_custom_meta as action and then going into functions.php file to update my custom meta data. This all working fine.

但是当我将 $ajaxPageUrl 更改为如下所示的内容时

But when I change $ajaxPageUrl to something like below

$ajaxPageUrl = get_template_directory_uri() . '/mindesk-ecommerce/ajax/updatesubscription.php';

然后创建文件 updatesubscription.php 并将我放在上面的相同代码放在 functions.php 文件然后它不起作用.

And then created the file updatesubscription.php and put same code which I put above in functions.php file then its not working.

它一直给我 500 错误.. 有人可以指导我.. 将functions.php 的代码放到我的一个页面中是否可能或理想的方式..

It keeps giving me 500 error.. Can someone guide me .. is it possible or ideal way to put code of functions.php to one of my page ..

我想以这种方式遵循结构.

I want to follow the structure in that way.

有人可以指导我如何实现这一目标.

Can someone guide me how can I achieve this.

谢谢

Include updatesubscription.php 在你的 function.php 文件中

Include updatesubscription.php in your function.php file

include( get_template_directory() . '/mindesk-ecommerce/ajax/updatesubscription.php' );

ajaxPageUrl 在 my-subscriptions.php 文件中使用

And ajaxPageUrl As you use in the my-subscriptions.php file

$ajaxPageUrl =  admin_url( 'admin-ajax.php');