Site logo

Archived topic

Search Box In Blog Header

15 replies · Started by Giovanni on September 23, 2017

Viewing posts 1–15 of 16

Hi everyone,

I would like to add a Search Box in the Blog Header to help to browse through blog posts.
I know one way to do it would be with a widget-shortcode plugin and GP hooks.
My questions are:
1) How would I do that with coding and without plugins?
2) Which solution is better in terms of website performance?

Thanks

Hi Leo,

Yes, I'm referring to the blog page header.
The fact is, I prefer to have as few plugins as possible. Which code should I use?
Also, is there a 'better' option amongst the two (plugin vs code) in terms of website performance?

Page header content box doesn't take PHP so try the plugin route.

That's a broad question. Huge depends on how good the code is and how well the plugin is coded.

If you add code then you are responsible to maintain in, verses the plugin author maintain it for you.

I would give Widget Shortcode a shot.

Ok, I will try the plugin.
Thank you

No problem!

The plugin works perfectly, just two follow-up questions.

1) If I want to move the search box from the blog page header to inside the content container on the blog page, what should I do? I've tried the code above but can't seem to make it work.

2) Is it possible to change the placeholder from "search" to something else?

1) My mistake, now it works but the box is too narrow and aligned left. How can I increase the width and center it?
2) If I change it will it affect every search box on the site?

1) Can you link me to the site?

2) Yes but we could use conditional tag to make it only on that page if you'd like?

1. Try this:

.site-main > .search-form {
    text-align: center;
}

.site-main > .search-form input[type="search"] {
    width: 500px;
    max-width: 100%;
}

2. Here's an example:

add_filter( 'generate_search_placeholder', 'tu_search_placeholder' );
function tu_search_placeholder( $text ) {
    if ( is_home() ) {
        return 'Custom placeholder text';
    }

    return $text;
}

Sorry for the dumb question but, where should I paste the filter?
Also, how would you remove the search button and make a search box like the one you use here https://docs.generatepress.com/?

You can add it like this:

To remove the search button, do this:

.site-main > .search-form input[type="button"] {
    display: none;
}

Unfortunately a solution like that takes a lot of custom code, including AJAX. It's a bit too complicated to help with in the support forum.

Tom, I don't get how to add the filter. Maybe something is missing in your post?
I tried the code in the CSS but the button is still there.

This archived topic is closed to new replies.