I'm creating some dynamic content but I need to run a conditional is_single() check inside the php function

add_action( 'wp_ajax_rando_func', 'rando_func' );
add_action( 'wp_ajax_nopriv_rando_func', 'rando_func' );

function rando_func() {
   if (is_single()) {
        //do stuff
   }
}

This doesn't work so I tried adding the ID

function rando_func() {
   $page_id = $_POST["page_id"];
   if (is_single($page_id)) {
        //do stuff
   }
}

No luck with that either. How do I accomplish this?

  • It needs to be $_POST["page_id"], do you have the same error in your code? – kero Sep 7 '17 at 12:40
  • Sorry that was just a typo – user300979 Sep 7 '17 at 12:41
  • Still an error id"} -> id"]. Then it should work. Are you sure, $_POST['id'] has the id, because then it should work. Can you show, how you are calling the function from your frontend? – kero Sep 7 '17 at 12:44
  • Yes I added it as data to the parent container to test that it's passing through. – user300979 Sep 7 '17 at 12:49
  • To my mind knowing which template is rendered in the AJAX response isn't the way to go. I'd suggest having different AJAX actions based on what you want to receive. So on a single post you'd send a different request that you would if it were an archive. Then you output the right action in the template or with wp_localize_script() based on is_single() etc. – Jacob Peattie Sep 7 '17 at 13:51

is_single returns if there is a single page displayed within the actual server process. Within the Ajax-Function, nothing is "displayed", you're doing Ajax.

To check if the currently displayed object in your Browser is a single object, you need to transfer the information about this from the server process that serves your Browser to the server process that handles your ajax request.

Please try the following: Step 1: get the information into your Javascript function. You get the information from the $wp_query object. If you directly echo the javascript which does the AJAX Request, you can do it like this:

<script>
var is_single = <?php echo (is_single() ? '1' : '0' ); ?>;
</script>

If you're enqueueing your ajax-caller-script, you can use wp_localize_script .

Step 2: POST the is_single information to the Ajax Function

Step 3: Change your Ajax PHP Function to incorporate the is_single information:

function rando_stuff(){   
 $single = (int)$_POST['is_single'];
    if($single){
       //do stuff
    }
}

Step 4: Profit ;)

  • Shouldn't is_single( id ) return, if id is a single? No need for current displayed, loop, etc – kero Sep 7 '17 at 13:04
  • @kero An ID can't be is_single because is_single is a property of the main query, not of a page or post. – Jacob Peattie Sep 7 '17 at 13:25
  • Yes and no. The important thing to understand is that with ajax functions, you have two seperate processes that the server does: Process 1 is the one that displays your website in your browser. Within this process, is_single may be true (because maybe your browser shows the single post or page) Process 2 is the one where your ajax request is processed. In this process, is_single is always false, because nothing is displayed. You don't request a single post from the browser, instead the javascript acts as a quasi-browser to request the admin-ajax.php. – kuchenundkakao Sep 7 '17 at 13:26
  • A somewhat example to explain: You (the ajax request) and your twin brother (the "normal" browser) both go and buy cars at two different places. The merchant you go to says: i'll give you an offer for 200$ less, but only if you can proof that your brother bought a green car (do stuff if is_single()). If you don't have a picture of your brothers new green car (The POST data you have to transmit with the information is_single), the salesman will never give you that offer. – kuchenundkakao Sep 7 '17 at 13:31

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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