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 trialKelvin Atawura
Front End Web Development Techdegree Student 19,022 Pointsfunction 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
Kelvin Atawura
Front End Web Development Techdegree Student 19,022 Pointsl don't mess about with my .htaccess as am working in wordpress. Also the project would be put across to clients
mikes02
Courses Plus Student 16,968 PointsWordPress 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
31,709 PointsKelvin, 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() );
}
}
?>
Kelvin Atawura
Front End Web Development Techdegree Student 19,022 Pointsl 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
31,709 PointsWhen are you trying to run the redirect, meaning under what conditions?
Kelvin Atawura
Front End Web Development Techdegree Student 19,022 Pointsl want it to run whenever the user tries to access the url -> mysite/shop. basically wheever they type the url mysite/site.
Kelvin Atawura
Front End Web Development Techdegree Student 19,022 Pointsthat didnt seem to work any other ideas?
Andrew Shook
31,709 PointsSo anytime the go to "www.yourwebsite.com/shop" you want to redirect them to "www.yourwebsite.com"?
Kelvin Atawura
Front End Web Development Techdegree Student 19,022 Pointsyeah mate :)
Andrew Shook
31,709 PointsThis 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");
Kelvin Atawura
Front End Web Development Techdegree Student 19,022 Pointsthat didn't work but resolved it by putting wp_redirect (home_url()); exit; .thanks for your help and replies guys.
mikes02
Courses Plus Student 16,968 PointsKelvin, 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.
Kelvin Atawura
Front End Web Development Techdegree Student 19,022 Pointsl 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
34,995 Pointstry using the superglobal $_SERVER ! Then you can do cool stuff like $_SERVER['REQUEST_URI'];
Kelvin Atawura
Front End Web Development Techdegree Student 19,022 PointsProbz you can share the whole code here? always better to know more than one way of doing something
mikes02
Courses Plus Student 16,968 Pointsmikes02
Courses Plus Student 16,968 PointsWhat is the reason for redirecting them? I actually think this would be better accomplished with .htaccess.