Title: Create shortcode from php snippet
Last modified: August 7, 2019

---

# Create shortcode from php snippet

 *  [pmstudio1](https://wordpress.org/support/users/pmstudio1/)
 * (@pmstudio1)
 * [6 years, 8 months ago](https://wordpress.org/support/topic/create-shortcode-from-php-snippet/)
 * 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 &hellip;','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)

 *  [tapiohuuhaa](https://wordpress.org/support/users/tapiohuuhaa/)
 * (@tapiohuuhaa)
 * [6 years, 8 months ago](https://wordpress.org/support/topic/create-shortcode-from-php-snippet/#post-11811117)
 * 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](https://wordpress.org/support/users/pmstudio1/)
 * (@pmstudio1)
 * [6 years, 7 months ago](https://wordpress.org/support/topic/create-shortcode-from-php-snippet/#post-11828216)
 * 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.
 *  [tapiohuuhaa](https://wordpress.org/support/users/tapiohuuhaa/)
 * (@tapiohuuhaa)
 * [6 years, 7 months ago](https://wordpress.org/support/topic/create-shortcode-from-php-snippet/#post-11828243)
 * Sorry. I put by accident filter function as shortcode. Use as shortcode some 
   of your own functions.
 *  Plugin Author [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * (@bungeshea)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/create-shortcode-from-php-snippet/#post-12352791)
 * Hi [@pmstudio1](https://wordpress.org/support/users/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 ] );
       } );
       ```
   
 *  [tlterry](https://wordpress.org/support/users/tlterry/)
 * (@tlterry)
 * [6 years ago](https://wordpress.org/support/topic/create-shortcode-from-php-snippet/#post-12615083)
 * 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.

 * ![](https://ps.w.org/code-snippets/assets/icon.svg?rev=2148878)
 * [Code Snippets](https://wordpress.org/plugins/code-snippets/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/code-snippets/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/code-snippets/)
 * [Active Topics](https://wordpress.org/support/plugin/code-snippets/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/code-snippets/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/code-snippets/reviews/)

## Tags

 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)

 * 5 replies
 * 4 participants
 * Last reply from: [tlterry](https://wordpress.org/support/users/tlterry/)
 * Last activity: [6 years ago](https://wordpress.org/support/topic/create-shortcode-from-php-snippet/#post-12615083)
 * Status: not a support question