Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Hiding Search Bar
- This topic has 19 replies, 2 voices, and was last updated 1 year, 7 months ago by
Ernest Marcinko.
-
AuthorPosts
-
November 3, 2024 at 1:56 pm #51682
KimberlyAllen
ParticipantHi there, I am using the Ajax Search Pro plugin and have setup and few different searches appearing in different areas of the site. I have a main search bar that is placed within the site header menu but I don’t want it to be visible on the other pages. Is this possible to hide?
November 4, 2024 at 8:34 am #51685KimberlyAllen
ParticipantNovember 4, 2024 at 11:43 am #51689Ernest Marcinko
KeymasterHi Kimberly,
Surely! I assume you are using a widget/block in the header, but the header is globally displyed everywhere, so you probably can’t adjust that there. However it might still be possible via a custom code.
I have looked up your home page, it’s ID is 389, so I made a custom code snippet for you, which will check the current page ID, and if it’s not 389, then will intercept the search shortcode and will not print it.
Try adding this code via the Code Snippets plugin or to the functions.php file in your theme/child theme directory – make sure to have a full server back-up first for safety. For more details you can check the safe coding guidelines.
add_filter( 'asp_shortcode_output', function( $output ){ if ( get_the_ID() !== 389 ) { return ''; } return $output; } );November 4, 2024 at 12:07 pm #51691KimberlyAllen
ParticipantHi Ernest,
Thank you for the code. I have tested added this to functions.php and it seemed to work ok in terms of hiding the search box from all pages other than the home page. However, the side affect to this was that it also hid my other search boxes.
Please see attached screenshot for one of my pages which has 2 search boxes. https://sla.thespencergroup.co.uk/video-tutorials/
Would it be possible to tweak the code so the 2nd search box still remains active?
Thanks
November 4, 2024 at 12:15 pm #51693Ernest Marcinko
KeymasterSure!
I assume you only want to apply this rule to the first search bar (id=1), then this modified version will do the trick:
add_filter( 'asp_shortcode_output', function( $output, $id ){ if ( $id != 1 ) { return $output; } if ( get_the_ID() !== 389 ) { return ''; } return $output; }, 10, 2 );November 4, 2024 at 12:20 pm #51694KimberlyAllen
ParticipantThanks Ernest, I have tried this new code and nothing changes.
I am just wondering if it would be easier to have header search appear everywhere on the site except from two pages that have other ajax searches on. Is this possible?
So for example from the web link sent before the only pages I wouldn’t want the header menu search to be visible would be on the All courses and Video Help Guides pages.
Can this be done?
November 4, 2024 at 12:25 pm #51695Ernest Marcinko
KeymasterWell, this might be better to control via a plugin instead of a custom code.
Are you using some sort of a block in the header? I’m asking because there is this plugin, which may do exactly what you are looking for. Would that work for you?
November 4, 2024 at 12:50 pm #51696KimberlyAllen
ParticipantHi Ernest,
I am so sorry for this query, before I start looking into plugins can you check one thing for me, is it possible for the main header search to stay visible through the site apart from pages 13702, and 2428?
On these pages instead I need their individual searches as shown on the last screenshot?
November 4, 2024 at 12:55 pm #51697Ernest Marcinko
KeymasterSure, we can try that!
Okay, so with this, the first search (ID=1) on pages 13702 and 2428 should be hidden. Should no affect any other search bars:
add_filter( 'asp_shortcode_output', function( $output, $id ){ if ( $id != 1 ) { return $output; } if ( get_the_ID() == 13702 || get_the_ID() == 2428 ) { return ''; } return $output; }, 10, 2 );November 4, 2024 at 2:11 pm #51698KimberlyAllen
ParticipantHi Ernest,
We have tried the above code but it doesn’t seem to remove any search boxes from any of the pages.
Is it because on every page the first part of the if statement will always be true?
I did try flipping the if statements as below but that again hides all search boxes on the 2 pages (13702 and 2428)
if ( get_the_ID() == 13702 || get_the_ID() == 2428 ) { return ''; } if ( $id != 1 ) { return $output; } return $output; }, 10, 2 );November 4, 2024 at 2:21 pm #51699Ernest Marcinko
KeymasterThat very likely means that the search ID is incorrect in my code. Which search bar is the one in the header? I assumed “HomePage Search” from the screenshot, which had the ID=1.
November 4, 2024 at 2:28 pm #51700KimberlyAllen
ParticipantThat is correct, the search that sits within the header is ID 1, the search that sits on page ID 2428 is 5 and the search that sits on page ID 13702 is wd-asp id=5
November 4, 2024 at 2:30 pm #51701Ernest Marcinko
KeymasterI don’t know then, the conditions look okay to me. The first condition terminates when the search ID is not 1, aka for all the other search instances in every case.
Then it goes further down to check the page IDs.If you want, I can check directly – but I need temporary back-end and sFTP access to make changes to the code. Can you please add those?
November 4, 2024 at 2:48 pm #51702KimberlyAllen
ParticipantThanks Ernest, can you think of reasons why both searches are being removed from page ID 2428 when using this code?
November 4, 2024 at 2:53 pm #51703Ernest Marcinko
KeymasterWith your code variation it happens because the function first checks the page IDs, and when they match, it terminates empty (meaning that the search is not visible).
The search ID is only checked afterwards and at that point it does nothing whatsoever as on both condition the function terminates with the same output.-
This reply was modified 1 year, 7 months ago by
Ernest Marcinko.
-
This reply was modified 1 year, 7 months ago by
-
AuthorPosts
- You must be logged in to reply to this topic.