esc_url returns incorrect URL

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.

Related posts

2 comments

  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.

  2. 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 🙂

Comments are closed.