Mittul At TechnoBrave
Mittul At TechnoBrave

Reputation: 1242

WordPress AJAX update custom field in custom page

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 page

<?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;
}

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

$ajaxPageUrl =  admin_url( 'admin-ajax.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.

But when I change $ajaxPageUrl to something like below

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

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

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.

Thanks

Upvotes: 0

Views: 1018

Answers (1)

Bhautik
Bhautik

Reputation: 11282

Include updatesubscription.php in your function.php file

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

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

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

Upvotes: 1

Related Questions