Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

function not working

Can l get a hand with this function for my wordpress theme please. l am developing locally and using woocommerce and want to have a custom function that redirect the users to the home page when they put in the url(mysite/shop)

<?php
// Redirect frm the shop page
function redirectShop() {
  if($url == get_site_url('/shop')){
    wp_redirect( get_bloginfo( 'home' ) );
  }
}
?>

Thanks guys

What is the reason for redirecting them? I actually think this would be better accomplished with .htaccess.

l don't mess about with my .htaccess as am working in wordpress. Also the project would be put across to clients

WordPress uses .htaccess, though, and a redirect of that nature could very easily be accomplished within the .htaccess file quite simply:

Redirect 301 /shop http://example.com

2 Answers

Andrew Shook
Andrew Shook
31,709 Points

Kelvin, where are you setting the $url variable you are using in your if statement? Are you sure it is being set? My recommendation would be to pass the $url into the function as a parameter:

<?php
// Redirect frm the shop page
function redirectShop($url) {
  if($url == get_site_url('/shop')){
    wp_redirect( home_url() );
  }
}
?>

l have not set it anywhere. sort of thinking its been set by wordpress in the core. l think l need to revisit functions as a programming basics. Thanks for the reply and l would give it a go.

Andrew Shook
Andrew Shook
31,709 Points

When are you trying to run the redirect, meaning under what conditions?

l want it to run whenever the user tries to access the url -> mysite/shop. basically wheever they type the url mysite/site.

that didnt seem to work any other ideas?

Andrew Shook
Andrew Shook
31,709 Points

So anytime the go to "www.yourwebsite.com/shop" you want to redirect them to "www.yourwebsite.com"?

yeah mate :)

Andrew Shook
Andrew Shook
31,709 Points

This should work,let me know if there is a problem.

<?php
function runRedirect($query){
    $pageName = $query->query_vars['pagename'];
    if($pageName == home_url() . "/shop"){
        wp_redirect( home_url() );
    }

}
add_action( "parse_request", "runRedirect");

that didn't work but resolved it by putting wp_redirect (home_url()); exit; .thanks for your help and replies guys.

Kelvin, if you get a chance please share your updated and functional code, it may come in handy to others wanting to do something similar through use of a function.

l ended up not using a function l just but the code in the header tag. so its something like this

<?php  wp_redirect (home_url()); exit;  header();?>
Justin Estrada
Justin Estrada
34,995 Points

try using the superglobal $_SERVER ! Then you can do cool stuff like $_SERVER['REQUEST_URI'];

Probz you can share the whole code here? always better to know more than one way of doing something