Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • How to Use AJAX in a WordPress Shortcode?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.95k
    Comment on it

    We are going to process ajax request in wordpress shortcode on button click. First of all you need to add the shortcode in wordpress. Kindly write below code in function.php

    function callback_listing($atts, $content = null)
            'number' => '3',
            'columns' => '3',
            'order' => 'date',
            'role' => '',
            'ids' => '',
        ), $atts));
        echo '<ul>';
        foreach($users as $user) {
        <li><a href="<?php echo $user->ID; ?>" class="post_listing">Click here</a> to get the posts of user(<?php  echo $user->first_name; ?>) </li>
        echo '</ul>';
        echo '<h3>Post listing goes here</h3>';
        echo '<ul class="post_table">';
        echo '</ul>';

    In above php script, we added a short code named user_list which is useful to display the user listing and we are going to get posts of these user by ajax call. Kindly write below script in footer page and it is useful to process the ajax request. When any one click on the user listed in user list, an ajax request will be fired to get his post listing.

    var user_d = jQuery(this).attr('href');
    var formData = {
        type        : 'POST', 
        url         : "<?php echo admin_url( 'admin-ajax.php' ); ?>", 
        data        : formData 

    Kindly write down below script in function.php which is useful to fetch the post listing for particular user.

    function user_post_list(){
       $user_one = $_POST['user_id'];
       $args = array('post_author'=>$user_one);
       query_posts( $args );
    while ( have_posts() ) : the_post();
        echo '<li>';
        echo '</li>';

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: