最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

search - Echoing home_url() returns current url in searchform.php

programmeradmin1浏览0评论

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
  • You will want to read this guide about Paths & Uri in WordPress. Note: The two answers below are correct. – kaiser Commented May 3, 2016 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 Commented May 3, 2016 at 15:20
  • @kaiser WordPress core uses slash in native search form, it's irrelevant to the issue. – Rarst Commented May 3, 2016 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 Commented May 3, 2016 at 15:47
  • Wait a second. home_url() does not echo. My fault. It's the current blogs function… – kaiser Commented May 3, 2016 at 15:50
Add a comment  | 

1 Answer 1

Reset to default 1

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.

发布评论

评论列表(0)

  1. 暂无评论