1

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.

14
  • So you are doing something like this? <form role="search" method="get" action="<?php echo esc_url( home_url( '/' ) ); ?>">. Because the action attribute is what defines the website/URL the user is taken to when the search query is submitted.
    – its_me
    Oct 26, 2013 at 4:40
  • That is exactly what am doing; but it is taking me to a different site of mine, which is odd and when I view the source I see the other site's url.
    – Jacob
    Oct 26, 2013 at 4:46
  • Under Settings > General in wordpress dashboard, what do you see for "Site Address (URL)"? You might want see if it's set to something else. Otherwise, I am not sure what could be wrong.
    – its_me
    Oct 26, 2013 at 7:30
  • 1
    I'm checking out the source of the function get_home_url() here to see what can influence it's output.
    – birgire
    Oct 28, 2013 at 20:39
  • 1
    ok great to hear it's solved ;-) Maybe you can answer it and accept your answer?
    – birgire
    Oct 28, 2013 at 20:58

2 Answers 2

1

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.

1

Consider the following code:

<form role="search" method="get" action="<?php echo esc_url( home_url( '/' ) ); ?>">

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):

function get_home_url( $blog_id = null, $path = '', $scheme = null ) {
    $orig_scheme = $scheme;

    if ( empty( $blog_id ) || !is_multisite() ) {
        $url = get_option( 'home' );
    } else {
        switch_to_blog( $blog_id );
        $url = get_option( 'home' );
        restore_current_blog();
    }

    if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) {
        if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $GLOBALS['pagenow'] )
            $scheme = 'https';
        else
            $scheme = parse_url( $url, PHP_URL_SCHEME );
    }

    $url = set_url_scheme( $url, $scheme );

    if ( $path && is_string( $path ) )
        $url .= '/' . ltrim( $path, '/' );

    return apply_filters( 'home_url', $url, $path, $orig_scheme, $blog_id );
}

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:

grep -nr "http://randomurl.com"

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 as awk) 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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.