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 …','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)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘Create shortcode from php snippet’ is closed to new replies.