This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Hiding Search Bar

Viewing 15 posts - 1 through 15 (of 20 total)
  • Author
    Posts
  • #51682
    KimberlyAllenKimberlyAllen
    Participant

    Hi 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?

    #51685
    KimberlyAllenKimberlyAllen
    Participant

    Sorry to clarify my last message, is it possible for the widget to only show on the homepage?

    • This reply was modified 1 year, 7 months ago by KimberlyAllen.
    #51689
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi 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;
    } );
    #51691
    KimberlyAllenKimberlyAllen
    Participant

    Hi 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

    #51693
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Sure!

    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 );
    #51694
    KimberlyAllenKimberlyAllen
    Participant

    Thanks 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?

    #51695
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Well, 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?

    #51696
    KimberlyAllenKimberlyAllen
    Participant

    Hi 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?

    #51697
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Sure, 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 );
    #51698
    KimberlyAllenKimberlyAllen
    Participant

    Hi 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 );
    #51699
    Ernest MarcinkoErnest Marcinko
    Keymaster

    That 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.

    #51700
    KimberlyAllenKimberlyAllen
    Participant

    That 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

    #51701
    Ernest MarcinkoErnest Marcinko
    Keymaster

    I 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?

    #51702
    KimberlyAllenKimberlyAllen
    Participant

    Thanks Ernest, can you think of reasons why both searches are being removed from page ID 2428 when using this code?

    #51703
    Ernest MarcinkoErnest Marcinko
    Keymaster

    With 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.

Viewing 15 posts - 1 through 15 (of 20 total)
  • You must be logged in to reply to this topic.