Made it work, if there’s a better solution, please let me know. This is in case there isn’t one and anyone else has a similar problem.
For a full disclosure: I have NO IDEA how WordPress works. The methods for making this work was page-inspection and clicking on GeneratePress site version relevant icons and a different theme that did work icons, comparing, figuring out the names and what is used for what, with a help of a friend who’s also got no idea about WordPress but has some php and css experience. Took us a few hours to make it work, and I’m far from convinced it’s the best, fastest and the most elegant solution.
1) First thing to note
From what I found out, GeneratePress doesn’t use the searchform.php for main menu search button searches (final test of this was completely removing the searchform.php, making sure any page caching is avoided and then seeing that the function still worked perfectly).
2) Finding where the search is “defined”.
Turns out the main menu search is set at this directory (within the themes directory, of course):
generatepress/inc/structure/navigation.php
3) Editing the navigation.php
if ( ! function_exists( ‘generate_navigation_search’ ) ) {
add_action( ‘generate_inside_navigation’, ‘generate_navigation_search’ );
/**
* Add the search bar to the navigation.
*
* @since 1.1.4
*/
function generate_navigation_search() {
$generate_settings = wp_parse_args(
get_option( ‘generate_settings’, array() ),
generate_get_defaults()
);
if ( ‘enable’ !== $generate_settings[‘nav_search’] ) {
return;
}
// BEGINNING OF CUSTOM SEARCH CHANGES
// SNIPPED PART
/*echo apply_filters( ‘generate_navigation_search_output’, sprintf( // WPCS: XSS ok, sanitization ok.
‘<form method=”get” class=”search-form navigation-search” action=”%1$s”>
<input type=”search” class=”search-field” value=”%2$s” name=”s” title=”%3$s” />
</form>’,
esc_url( home_url( ‘/’ ) ),
esc_attr( get_search_query() ),
esc_attr_x( ‘Search’, ‘label’, ‘generatepress’ )
));
*/
// THE END OF SNIPPED PART – DIDN’T DELETE IT, JUST TURNED IT INTO A COMMENT, TO BE MORE CLEAR
// CUSTOM SEARCH PART ADDITION
// “MOJ” MEANS MINE IN SERBIAN, HENCE “mojs”
$mojs=” <script>
(function() {
// BEGGINING OF COPY-PASTED GOOGLE PROVIDED CODE
var cx = ‘partner-pub-MY_GOOGLE_NUMBER:MORE_GOOGLE_NUMBERS’;
var gcse = document.createElement(‘script’);
gcse.type = ‘text/javascript’;
gcse.async = true;
gcse.src = ‘https://cse.google.com/cse.js?cx=’ + cx;
var s = document.getElementsByTagName(‘script’)[0];
s.parentNode.insertBefore(gcse, s);
})();
</script>
<gcse:searchbox-only></gcse:searchbox-only>”;
// END OF COPY-PASTED GOOGLE PROVIDED CODE
echo apply_filters( ‘generate_navigation_search_output’, sprintf( // WPCS: XSS ok, sanitization ok.
‘<form method=”get” class=”search-form navigation-search” action=”%1$s” >
<input type=”search” style=”display:none” class=”search-field” value=”%2$s” name=”s” title=”%3$s” />
%4$s
</form>’,
esc_url( home_url( ‘/’ ) ),
esc_attr( get_search_query() ),
esc_attr_x( ‘Search’, ‘label’, ‘generatepress’ ),
$mojs
));
// THE END OF CUSTOM SEARCH ADDITION
}
}
4) Making it all look not too bad on desktops and mobiles:
Some Additional CSS added for this purpose. Without the padding the Google search button would overlap with the hteme’s button for closing the search. This was as god as I could make it.
Would have preffered to have the Google searc button always below, but without moving it a bit to the right, it looks bad/overlaps on smaller screens for some reason. Will play with it a bit more.
#___gcse_0{
margin-top:60px;
background-color: #222;
}
.gsc-search-button{
/*padding-left:0px;*/
/*height:60px;*/
/*width:60px;*/
padding-right:60px;
}
.gsc-input{
/*padding-right:0px !important ;*/
}
5) Potential problems:
If google changes their code, this implementation won’t work. So if gsc-search-button (Google Search Custom? ) becomes some other name, the solution won’t work.
-
This reply was modified 6 years, 1 month ago by Relja.