Posted inProgramming / Mysql / Mysqli / php / wordpress

WordPress AJAX URL GET POST Call PHP Function

WordPress AJAX URL GET POST Call PHP Function

In this Post We Will Explain About is WordPress AJAX URL GET POST Call PHP Function With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to How to use AJAX to call a function in WordPressExample

In this post we will show you Best way to implement WordPress Ajax URL for function in functions.php, hear for How to use AJAX on WordPress step by stepwith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Example: WordPress AJAX

jQuery( document ).ready( function($) { $('#wpdiv-form-data' ).html( 'Simple Encode and Decode Custom Strings with Base64 in JavaScript Devloped by Pakainfo.com' ); });

How?

    jQuery(document).ready(function($) {
     
        $.ajax({
            type: "POST", 
            url: 'https://www.pakainfo.com/data_process_get.php',
            data: {
                user_fname : 'admin', // PHP: $_POST['user_fname']
                user_lname  : 'Greg Pierce', // PHP: $_POST['user_lname']
            },
            success:function(data) {
                console.log(data); 
            },
            error: function(errorThrown){
                console.log(errorThrown);
            }
        });  
                  
    });
    

data_process_get.php


Hello. wer user_firstname name is .

And your user_lastname name is .

And it will simple display this text as final results of the request:

    

Hello. wer user_firstname name is admin.

And your user_lastname name is Greg Pierce.

AJAX “success” method using WordPress

     $.ajax({
        type: "POST", 
        url: 'https://www.pakainfo.com/data_process_get.php', 
        data: {
            user_fname : 'admin', // PHP: $_POST['user_fname']
            user_lname  : 'Greg Pierce', // PHP: $_POST['user_lname']
        },
        success:function(data) {
            $( '#wpdiv-form-data' ).html( data );
        },
        error: function(errorThrown){
            $( '#wpdiv-form-data' ).html( '

Error all the module retrieving data. Please simple try again.

' ); } });

WordPress and AJAX

https://www.pakainfo.com/wp-admin/liveuserdata-ajax.php
$ajax_url = admin_url( 'liveuserdata-ajax.php' );

liveuserdata-ajax.php

$.ajax({
	url: https://www.pakainfo.com/api/v1/index.php/userlist", 
});

we can use wp_localize_script()

 
wp_enqueue_script( 'my_script_handle', MY_JS_URL, array( 'jquery' ) );
wp_localize_script( 'my_script_handle', '"https://www.pakainfo.com/api/v1/index.php/userlist"', admin_url( 'liveuserdata-ajax.php' ) );

$.ajax({
	url: "https://www.pakainfo.com/api/v1/index.php/userlist", 
});

AJAX Action

.ajax({
    url: "https://www.pakainfo.com/api/v1/index.php/userlist",
    data: {
        action     : 'my_ajax_action',
        user_fname : 'admin',           // PHP: $_POST['user_fname']
        user_lname  : 'Greg Pierce',           // PHP: $_POST['user_lname']
    },
});

wp_ajax_* action hook like this:

    add_action( 'wp_ajax_my_ajax_action', 'my_ajax_action_callback' );
    function my_ajax_action_callback(){
        $user_fname = isset( $_POST['user_fname'] ) ? $_POST['user_fname'] : 'N/A';
        $user_lname = isset( $_POST['user_lname'] ) ? $_POST['user_lname'] : 'N/A';
        ?>
        

Hello. wer First Name is .

And your user_lastname name is .

wp_ajax_* (the same as above): for logged-in user.

    add_action( 'wp_ajax_my_ajax_action', 'my_ajax_action_callback' );
    add_action( 'wp_ajax_nopriv_my_ajax_action', 'my_ajax_action_callback' );

wp_ajax_nopriv_* : for logged-out simple user.

add_action( 'wp_ajax_nopriv_my_ajax_action', 'my_ajax_action_logged_out_user_callback' );
function my_ajax_action_logged_out_user_callback(){
	?>
	

here click to Please log in.

we can do anything in this callback function.

we can simple load post data using “wp_query” wordpress function
we can simple retrieve options data using “get_option()” wordpress function
we can simple get post simple meta data, or any save it, etc.

Example

I hope you have Got What is How to use AJAX on WordPress step by step And how it works.I would Like to have FeadBack From My Blog(Pakainfo.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

Your email address will not be published. Required fields are marked *

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype