In searchform.php my search form looks like this:

<form role="search" method="get" class="search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>">
        <input type="text" placeholder="<?php _e('Type something and hit enter...', 'wbs'); ?>" name="s" />

However, I noticed that the search doesn't work as intended, the ?s= parameter isn't added to the home URL but rather to the current URL.

So, when I'm on the single post page, instead of http://example.com/?s=term I'm getting http://example.com/post-name/?s=term. Similar results while searching from archives and pages. Searching like this obviously doesn't work.

What's more, if I completely remove searchform.php I'm getting the same results so it looks like home_url() just doesn't return the correct URL. I have permalinks set up to Post Name.

Funnily enough, simply running <?php echo esc_url( home_url( '/' ) ); ?> in other files (e.g. footer.php) always returns http://example.com/ as expected.

Any ideas why this is happening? Am I missing something?

  • You will want to read this guide about Paths & Uri in WordPress. Note: The two answers below are correct. – kaiser May 3 '16 at 14:44
  • What is the exact resulting URL in page source? If it's different from expected then you have something interfering with WP API. – Rarst May 3 '16 at 15:20
  • @kaiser WordPress core uses slash in native search form, it's irrelevant to the issue. – Rarst May 3 '16 at 15:21
  • @Rarst I know, that's why I wrote that both answers are correct. And I wrote nothing about any slash anywhere. One problem that I actually see is that the action attribute is plain wrong above and will probably output 1. One can not use echo home_url() – this needs to be get_home_url() instead to work. – kaiser May 3 '16 at 15:47
  • Wait a second. home_url() does not echo. My fault. It's the current blogs function… – kaiser May 3 '16 at 15:50

Just like @cjbj says, use home_url() without the slash.

home_url( $path, $scheme ); - $path (string) (optional) Path relative to the home URL.

instead of home_url() you can use bloginfo('url') to get the root/home of your wordpress site.

| improve this answer | |
  • Curiously enough, echo get_bloginfo('url') didn't work either. Exactly the same results as explained in OP. Could it be something about my server configuration? – Kamil May 3 '16 at 13:43
  • @Kamil, yes that probably the case here. Look in you dashboard and go to Settings > General and see Wordpress-Address (URL) or SiteAddress, and see if it's http://example.com/ – Refilon May 3 '16 at 13:45
  • @Refilion, yup, checked there too. Both URLs looks like they should. – Kamil May 3 '16 at 14:04
  • What does bloginfo('url') return? @Kamil – Refilon May 3 '16 at 15:10
  • @Kamil OP means original poster, which would be you ;) – kaiser May 3 '16 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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