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

<div id="wpdiv-form-data"></div>

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);
            }
        });  
                  
    });
    <form action="https://www.pakainfo.com/data_process_get.php" method="post">
        <input type="text" name="user_fname" value="admin">
        <input type="text" name="user_lname" value="Greg Pierce">
        <input type="submit" value="Submit">
    </form>

data_process_get.php

<?php
$user_fname = isset( $_POST['user_fname'] ) ? $_POST['user_fname'] : 'N/A';
$user_lname = isset( $_POST['user_lname'] ) ? $_POST['user_lname'] : 'N/A';
?>
<p>Hello. wer user_firstname name is <?php echo $user_fname; ?>.</p>
<p>And your user_lastname name is <?php echo $user_lname; ?>.</p>
<?php
die(); 

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

    <p>Hello. wer user_firstname name is admin.</p>
    <p>And your user_lastname name is Greg Pierce.</p>

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( '<p>Error all the module retrieving data. Please simple try again.</p>' );
        }
    });

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';
        ?>
        <p>Hello. wer First Name is <?php echo $user_fname; ?>.</p>
        <p>And your user_lastname name is <?php echo $user_lname; ?>.</p>
        <?php
        wp_die(); 
    }

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(){
	?>
	<p>here click to Please log in.</p>
	<?php
	wp_die(); 
}

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.


Related FAQ

Here are some more FAQ related to this Article:

  1.   How to get all products in WooCommerce
  2.   PHP Download Remote File from URL using CURL
  3.   Simple Laravel Log Viewer API Example
  4.   Helper classess in laravel 6 Example - PHP
  5.   How do I enable appearance in WordPress?
  6.   how to get values from xml object array in php?
  7.   WordPress AJAX - Wordpress jQuery AJAX POST Request
  8.   PHP SQL INNER JOIN Query on Multiple Tables
  9.   country state city drop down list using JavaScript
  10.   Laravel check if ajax request or not example

Leave a Reply

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