0

I have a pop up that I would like to display the author information for that particular post.

I am using WordPress Popup Maker and have created a function that I turned into a shortcode so I could use in Popup Maker.

What I want to do is push the ID of a link onclick to a the function that displays the user data.

Here is my Jquery in a hook in functions.php:

add_action( 'wp_footer', 'my_custom_popup_scripts', 500 );
function my_custom_popup_scripts() { ?>
<script type="text/javascript">
(function ($, document, undefined) {


$('.author-popup').click(function() {
  var id = $(this).attr('id');

});


    // Your custom code goes here.
}(jQuery, document))
</script><?php
}

And here is my start of function where I want to control what user data is displayed:

function my_author_box() { ?>


<?php


 $args = array(
'author'        =>  "31", //this what I want to change with Jquery 
);
query_posts($args);
?>

And here is my link to trigger:

<a href="#" class="author-popup" id="33">some user</a>

I'm not really good with jquery or AJAX which I have read might be a solution. If anyone has any ideas I would really appreciate.

Thanks in advance

1

You have to use AJAX for that. What you have to do is:

  1. Create a JS script which sends an AJAX request to admin-ajax.php file:

<script>
 jQuery(".author-popup").on("click", function(){
  var id = jQuery(this).attr("id");
  jQuery.post("http://www.yourdomain.com/admin-ajax.php", {action: "my_author_box", id: id}, function(response){
   console.log("Response: " + response);
   //NOTE that 'action' MUST be the same as PHP function name you want to fire
   //you can do whatever you want here with your response
  });
 })
</script>
  1. Create new file e.x. myajax.php and include it to functions.php
  2. Now write a function you want to fire on click, e.x.:

function my_author_box(){
   $args = array(
    'author' => $_REQUEST['id'];
   );
   query_posts($args);
   die();
}
add_action( 'wp_ajax_my_author_box', 'my_author_box' );
add_action( 'wp_ajax_nopriv_my_author_box', 'my_author_box' );

That's all. As I said note that actionhas to be the same as PHP function. If you want to have some response from your PHP function just echo it and this is what you will get as a response. If you'll get 0 as a response it means that function you want to fire does not exists or you didn't call die() at the end of your function. Should work

  • Thanks Sebastian, worked perfectly. I even got the '0' error because I didn't call die() so that info helped too. Thanks again – aido14 Oct 27 '16 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.