0

Goal: I have modified the permalink to show the category name for a specific category: grill-rezepte. By default the permalink is: https://website.com/post-title just the grill-rezepte is now: https://website.com/grill-rezepte/post-title

I therefore want to set up 301 redirects on all the grill recipies so that they move to the new URL. I have the following code (see below) which leads to an infinite loop.

Code:

add_action('template_redirect', 'post_redirect_by_custom_filters');
function post_redirect_by_custom_filters() {
    global $post;
    // this array can contain category names, slugs or even IDs.
    $catArray = ['grill-rezepte','Test2'];
    if (is_single($post->ID) && has_category($catArray, $post)) {
        $new_url = "https://bbqpit.de/grill-rezepte/{$post->post_name}/";  
    wp_redirect($new_url, 301);
        exit;
    }
}

I tried adding an if conditional

if (strpos($url,'grill-rezept') !== false) { 
 Above Code 
}

But this does not help because it seems like the redirect is placed once and is not removed on page load. So it leads to a loop. Is there any way I could fix this? From what I understand the redirect command is placed once and is left there permanently and so I can't think of any logic to over come this problem. Also, I obviously do not want to redirect (not even once, for SEO purposes) if the URL is already https://website.com/grill-rezepte/post-title

I've spent 5 hours trying to figure something out and read a lot of posts. I'm not a wordpress guru, so I'm asking for help. Thanks.

  • What is $url / how do you get it? – kero Aug 24 '18 at 10:44
  • Oh... From what I read that's how you search within a URL for a string? So if that string is present on the URL of the current page, do not redirect. It needs to be !== false going to edit it now. – kepler456 Aug 24 '18 at 10:47
  • You're right. However, you need to retrieve it first, $url is probably empty right now (You can see that if you do something like var_dump($url); wp_die();). I don't have the time right now to test this myself and write a proper answer. Can you try this code $url = $_SERVER['REQUEST_URI']; if(strpos($url, ... (so just prepend what you have by url = server)? – kero Aug 24 '18 at 10:56
  • Awesome kero, that worked like a charm. Like I said at the end I'm no WordPress guru. I assumed that stuff like this was a global variable of some sort, obviously, it wasn't. If you add the same thing as a separate answer, I'd be happy to select it as the accepted answer. – kepler456 Aug 24 '18 at 11:28
0

You've got most of it right. However, there is one little caveat: $url doesn't mean anything in your context. You can easily prove that by using something like this for debugging

add_action('template_redirect', 'post_redirect_by_custom_filters');
function post_redirect_by_custom_filters() {
    var_dump($url);
    wp_die();
}

So all that is left for your code to work is actually defining $url as the requested URL (or at least the path):

add_action('template_redirect', 'post_redirect_by_custom_filters');
function post_redirect_by_custom_filters() {
    global $post;

    // this array can contain category names, slugs or even IDs.
    $catArray = ['grill-rezepte','Test2'];

    // get current path
    $url = $_SERVER['REQUEST_URI']; 

    // slug is already in URL, return early
    if (strpos($url,'grill-rezept') !== false) {
        return;
    }

    if (is_single($post->ID) && has_category($catArray, $post)) {
        $new_url = "https://bbqpit.de/grill-rezepte/{$post->post_name}/";  
        wp_redirect($new_url, 301);
        exit;
    }
}
  • Thanks kero. Btw it does need to be false and not true for the logic to work if (strpos($url,'grill-rezept') == false) :) my bad on the many changes to this hehe. – kepler456 Aug 24 '18 at 14:57
  • @user3764238 You mean in my example it should be == false? – kero Aug 25 '18 at 11:30
  • Nope, your example is good. I had to edit mine to get it to work. I should have commented on the original post, instead of here (your answer) and causing this confusion. – kepler456 Aug 27 '18 at 9:10
  • @user3764238 Ah I see, no worries! strpos() can be tricky with its return values, so I wasn't 100% sure without having to run the code myself. Glad to hear it cleared up – kero Aug 27 '18 at 12:22

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.