Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Redirect single to index, retrieve single via index through ajax WordPress

  • SOLVED

Ok, so I'm creating this "one page" WP based site and for that I need to avoid direct access to all single pages. Instead I want to load this content in to the index page by some ajax functionalities (e.g. a portfolio display on one section of the page). The problem is rerouting the single pages to index without affecting the ajax request.

For example, if I try applying this inside functions.php -

add_action('wp', 'redirect_single_cpt');
function redirect_single_cpt(){
if (is_single()) {
wp_redirect( home_url() ); exit;
}
}


I end up retrieving another copy of index when I request a single page on the index page.
How can this be avoided?

Answers (2)

2012-09-16

John Cotton answers:

Try


add_action('wp', 'redirect_single_cpt');

function redirect_single_cpt(){
$isAjax = (defined('DOING_AJAX') && true === DOING_AJAX) ? true : false;

if(!$isAjax) {
if (is_single()) {

wp_redirect( home_url() ); exit;

}
}

}


Saturate comments:

Ah, got it. Sending along a parameter did the trick. Thanks John!

2012-09-16

Jatin Soni answers:

Debug it by placing below code to first line of your header.php

<?php
if (is_single()) {
wp_redirect( home_url() ); exit;
}
?>