Do you want to add advanced search capabilities without relying on third-party plug-ins or topics? Build your own advanced word press search plug-in by supporting automatic suggestions, custom post types, categories, custom fields, and caching from scratch. This article describes how to build an object-oriented plug-in that can filter advanced search forms based on shortcut code using custom metadata generated using custom post types (CPT), custom categories, and advanced control fields. (ACF) The search form also provides Ajax based automatic suggestions, and uses the temporary function of word press to cache the results. The
Free plug-ins are a popular way to add more features, but they often have unique problems. Moreover, there are too many factors to consider when creating a combination correctly. Sometimes, in a larger plan, it may be more beneficial to develop your own plug-ins for areas that require more fine-tuning of methods. You can also have better control over availability, performance, and application security. Note: this document is for intermediate word press developers. Suppose you have practical knowledge of PHP, JavaScript, word press loop, transients, and word press plugin APIs. If you want to review, please read the following: The
Create custom content in WordPress: develop WordPress for intermediate users of categories and fields: build plug-ins advanced WordPress development: temporary tasks advanced WordPress development: introduction to object-oriented programming this article uses shortcut code to add custom WordPress that automatically suggests activating search forms to WordPress pages I have prepared plug-ins. You can also fine tune the search results to single or multiple custom post types and display them in the set-top box grid layout. You can my search plug-in here and follow up the article. The
Later in the article, the same plug-in will be expanded and complex search functions will be added, such as multi classification and custom field filtering for the virtual video library. However, the examples can be easily extended to all scenarios, such as product search or panel. Search. CPT and ACF add great power and functionality to WordPress, which is common in all custom development projects. My goal is to integrate these advanced word press features to help them work together in real applications. Here we go! Note: in the rest of the article and code, the word post type is the default post and pages post type and the registered custom post type. The
Before viewing the control flow code for a custom search plug-in, you can view an advanced view of how the plug-in works behind the scenes. The plug-in’s search engine will start after the user enters keywords in the search window or submits a search form. And there will be great changes according to the transition state. The post title cached by the control flow of the custom search plug-in is not only wp\u localize\u script, but also locally cached data in JavaScript. Because Ajax requests are made every time a search key is entered, AutoSuggest has a significant impact on performance. The
For resource sensitive areas, such as status application search for deleting transactions, we have implemented the following ideas: For automatic caching recommendations, you only need to call Ajax instead of the entire wp\u query object. Determine whether the temporary information does not retain the old information, and provide settings to control the types of posts that will be included in the search. Of course, one size is not suitable for everyone. The application
The complexity of needs to be adjusted slightly. Let’s look at the structure of the plug-in and see where it goes. The structure of the advanced search plug-in the search plug-in was originally based on the in fork plug-in template of the WordPress plugin boilerplate project. The structured methods of the back end include: The
Inc\/core\/* – the core function of the plug-in, inc\/core\/class init. PHP – Admin menus, shortcut code, AJAX processors, administrative notifications, scripts, and style hook registrations inc\/admin\/class-admin. PHP – manage the functionality of the dashboard plug-in settings area inc\/common\/class-common. PHP – provides functions of Ajax processor based on shortcut code search and automatic suggestion inc\/common\/views\/* – search form and search results inc\/common\/js\/* – enable automatic suggestion processor inc\/common\/css\/* – when CSS of search form is enabled, the plug-in adds options page as a child menu item to the settings management menu. Lists the custom post types registered with the default post and page post types, which will be included in the advanced search. The
Male select the plug-in setting page of the post type. If I just want to see how to add the administrator page, I won’t go to the detail define\u admin\u hooks() method inc\/core\/class-init. PHP and add\u plugin\admin\menu() methods inc\/admin\/class-admin. PHP The label on the setting page is inc\/admin\/views\/html-nds-advance-search-admin-options. You can find it in PHP. In fact, the interesting place is inc\/common\/class-common. PHP. Its entry point is inc\/core\/class-init. Use PHP’s define\u common\u hooks() method inc\/core\/class-init. PHP. The
To load the summary 0e584ae9450b1ab9b3c1d7d61c8b01, start with the short code. Load the shortcut code inc\/common\/class-common for the custom search form. PHP’s register_shortcodes() method adds shortcut code [nds-advance-search] for connecting custom search forms. View the summary on the loading summary 5e52011542082b940ae71b6eb0f500b8. You can see that the shortcut code content (such as search form) is returned by the shortcut code\nds\u advanced\u search() callback function. You can: The
Load the summary d41849a407c0dc49c79b25034a67f4d1, but I took a slightly different approach. Rather than directly recalling the output of search form annotations from the get\u search\u form code, use the get\u search\u form filter and the get\u search\u form() function to load the search form. In order to better control the rendering of the load summary 9ecd1b55fa3a31fa22379bdfe271de10 search form, we performed this operation. Let’s see how it works. Get\u search\u form filter hook and template tagget\u search\u form() template tag is a good way to control the display of word press search. First, search the form in the topic. Ensure that PHP is included and load the defined annotations. Otherwise, the built-in search form provided by WordPress will be loaded. The
Alternatively, you can use the get_search_form filter hook to completely replace the search form. In this case, get_search_form() will return the annotation defined by the filter callback. When submitting the default search form, WordPress has the following conditions:
The get_transient() function returns the transition state. If it exists, the cached post ID of the required post type and the array containing the post title are returned. If it is temporarily expired, does not exist, or deleted during the post or setting update, it returns false. Get\u posts() is called through the cache\u post\u in\u post\u types() method. The load summary 5183a85c67fc40917f1d416686b59ce3 controls the cached post ID, so it may be necessary to adjust the acquisition of get\u posts() as required. The cache_posts_in_post_types() method is also used by the AutoSuggest handler to cache the post title. If you do not want to use AutoSuggest, you can use the return fields parameter to search only for IDS, thus further improving the speed of get_posts() return fields. User defined query search results of flexbox layout now let’s take a look at the last piece of the puzzle: search auto suggestion. Add the auto suggestion function to the search form, which is already included in WordPress. Therefore, the jQuery UI auto completion component provides search suggestions. However, there are many other external libraries available. Inc\/common\/class-common The enqueue_scripts method in PHP handles the required script loading. If the post title cached by the loading points b8172aa19d803b363036612ff3de2 is temporarily available, it can also be used in the auto suggest JavaScript through the wp_localize_script function. This helps minimize the number of Ajax calls to WordPress for search suggestions. The following code is very basic, but is used to handle automatic suggestions. Load summary 38b541bf057403bf1c72aa3c34895f the following are some important aspects of the AutoSuggest script. Jquery Use grep() to filter post titles. Regular expressions can be used to further refine matches. To reduce grep calls, the search keys will be cached back to the local object, but the cache can also be permanently created using the HTML web storage API. Ajax requests only occur when wp\u localize\u script passes a null value for the post title. Let’s look at how Ajax requests are handled. Auto suggest Ajax processor action: \
On the basis of creating a search filter using user-defined metadata, a custom field for the video article type is created using the group field type provided with the ACF advanced version. To use the language and duration custom fields as drop-down lists, you must first extract unique values. For this purpose, we use the ACF function get_field_object() to return a unique array according to the custom field type. The data object returned by ACF’s get\u field\u object() function may be boring to identify the required fields in the large array, so the debugger will be very convenient. After I confirm that the required information is available in the selection array, I can generate annotations for the HTML select element. The search filter that loads the summary 42e915395dfcea55933655da861f331 date range is created using the jQuery UI datepicker. For more information, see the complete code for the search filter. To query posts using custom fields, you must set the meta\u query parameter of wp\u query. Query the dynamic meta\u query of Posts using custom fields to create parameters of arrays that represent metadata in the form of each inner array key \/ value pair, similar to tax\u query meta\u query. The internal meta_query array is created according to the user-defined field type selected by the user. Load the summary on summary 19ab4bcf48e8aa44aaba35cc80fd0fb5 use the compare \
You can create your own exciting advanced search pages. Does your site use custom search? Please tell us the following comments. Tags: custom category search custom post type search word press advanced search