What would be the reason for <?php echo esc_url( home_url( '/' ) ); ?>
to be sending me to an incorrect website?
I use it in my search bar and when I search a value it sends me to one of my other websites, but I am not sure what I did to make this happen.
It ended up being something very easy, but something I overlooked. Since I always use
<?php get_search_form(); ?>
for my search forms, I naturally assume that in my section template for the search area, I was using that, but I was not.I built the theme custom off of another theme I built for a different website and for some reason in the form’s
action=""
I hardcode the url for the first site; that is why I kept getting forwarded there.I thought I checked the section, but must have overlooked it. After two days or so, I decided to re-check the section, thanks to the suggestions in the comments which made me re-think the problem.
So yeah, really stupid brain fart, but it is now solved.
Consider the following code:
home_url()
is a WordPress function that retrieves the home URL for the current site. When called with an optional$path
argument, it returns the Home URL with the optional$path
argument appended.Here’s the function definition (from
wp-includes/link-template.php
L#1959):Basically, there’s nothing in this function that would return a random URL out of nowhere.
When situations like this occur, the best thing to do is to
grep
your code for clues.Run this in a shell in the WordPress root installation directory:
This will list all the occurences of
http://randomurl.com
in your code-base and show the files that they appear in. If there are multiple occurrences, you can use a bit more advanced search (using command line options such asawk
) to see which one’s actually causing troubles.Note: I know this has already been solved, but this might be useful for future visitors, so I’m posting it as an answer 🙂