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?
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!
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;
}
?>