In searchform.php
my search form looks like this:
<form role="search" method="get" class="search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<div>
<input type="text" placeholder="<?php _e('Type something and hit enter...', 'wbs'); ?>" name="s" />
</div>
</form>
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 /?s=term
I'm getting /?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 /
as expected.
Any ideas why this is happening? Am I missing something?
In searchform.php
my search form looks like this:
<form role="search" method="get" class="search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<div>
<input type="text" placeholder="<?php _e('Type something and hit enter...', 'wbs'); ?>" name="s" />
</div>
</form>
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/?s=term
I'm getting http://example/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/
as expected.
Any ideas why this is happening? Am I missing something?
Share Improve this question asked May 3, 2016 at 12:56 KamilKamil 11 bronze badge 5 |1 Answer
Reset to default 1Just 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.
action
attribute is plain wrong above and will probably output1
. One can not useecho home_url()
– this needs to beget_home_url()
instead to work. – kaiser Commented May 3, 2016 at 15:47home_url()
does not echo. My fault. It's the current blogs function… – kaiser Commented May 3, 2016 at 15:50