• Hi, love your plugin ! Really great stuff !

    I’m a newbie regarding PHP and snippets, so I’m browsing the internet to find usable code and to learn how to customize it to my situation. Your plugin helps me to keep an overview on the snippet collection and the related snippet documentation.

    I found this code snippet to place a WooCommerce Product Search bar in an specific hook area of the Astra theme. Can you please explain how to adjust this script so that it is not automatically linked to the theme hook, but converted into a shortcode that can be used anywhere on the site (post, page, widget, …) ?

    Looking forward to your reply. Many thanks in advance !

    
    // ----------------------------------
    // 1. ADD SEARCH TO ASTRA HEADER
      
    add_action( 'astra_content_before', 'pms_add_search_to_footer' );
      
    function pms_add_search_to_footer() {
    get_search_form();
    }
      
      
    // ----------------------------------
    // 2. LIMIT SEARCH TO POSTS OR PRODUCTS ONLY
      
    function SearchFilter($query) {
    if ( !is_admin() && $query->is_search ) {
    $query->set('post_type', 'product'); // USE 'PRODUCT' or 'POST'
    }
    return $query;
    }
      
    add_filter( 'pre_get_posts', 'SearchFilter' );
      
      
    // ----------------------------------
    // 3. CHANGE PLACEHOLDER & BUTTON TEXT
      
    function pms_astra_search_form_modify( $html ) {
        return str_replace( array('Search …','Search'), array('WooCommerce Products ...','Search Product'), $html );
    }
      
    add_filter( 'get_search_form', 'pms_astra_search_form_modify' );
      
      
    // ------------------------------
    // 4. ADD SEARCH ICON TO NAVIGATION MENU
      
    function pms_new_nav_menu_items($items) {
        $searchicon = '<li class="search"><a href="#colophon"><i class="fa fa-search" aria-hidden="true"></i></a></li>';
        $items = $items . $searchicon;
        return $items;
    }
      
    add_filter( 'wp_nav_menu_additional-resources_items', 'pms_new_nav_menu_items' );
    
Viewing 5 replies - 1 through 5 (of 5 total)
  • Use just

    add_shortcode( ‘wp_nav_menu_additional-resources_items’, ‘wp_nav_menu_additional-resources_items’ );

    And inside some text
    [wp_nav_menu_additional-resources_items]

    Thread Starter pmstudio1

    (@pmstudio1)

    Thank you tapiohuuhaa , but your suggestion doesn’t work.
    I have added the code lines at the end of the snippet, but no success. The short code is displayed on the page as text and is not converted to a product search bar.
    I’m a newbie regarding PHP and snippets, so I don’t know how to make it happen. Sorry.
    Any advise is welcome.

    Sorry. I put by accident filter function as shortcode. Use as shortcode some of your own functions.

    Plugin Author Shea Bunge

    (@bungeshea)

    Hi @pmstudio1,

    The code for modifying the search results remains pretty similar:

    add_filter( 'pre_get_posts', function ($query) {
    	if ( ! is_admin() && $query->is_search ) {
    		$query->set('post_type', 'product'); // USE 'PRODUCT' or 'POST'
    	}
    
    	return $query;
    } );

    Then you can create a shortcode to display the search form in a post or page like this:

    add_shortcode( 'search_form', function () {
    	return get_search_form( [ 'echo' => false ] );
    } );

    Hi Authur,

    I have another shortcode which using in every webpage called [include id=”14″ title=”Footer_English (Active)”].

    I would like to know how do we called the same above in your add_shortcode function without changing every webpage?

    i did try the following shortcode format but no luck to see anything on my webpage. Please advise.

    add_shortcode( ‘[include id=”14″ title=”Footer_English (Active)”]’, function () {
    //code here
    } );

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Create shortcode from php snippet’ is closed to new replies.