SearchWP Form Parameter Usage on WordPress search.php
Overview
The swp_form $_REQUEST attribute is a critical mechanism that SearchWP uses to identify which search form configuration was used to initiate a search. This parameter acts as a bridge between the SearchWP form interface and the search results, enabling form-specific settings, engine selection, and advanced filtering.
Table of Contents
- What is
swp_form? - How
swp_formis Set - How
swp_formModifies WordPress Search Results - Integration with WordPress Native Search
- Form Configuration Impact
- Advanced Search Filtering
- Code Examples
- Reference Code Locations
What is swp_form?
swp_form is a URL parameter (array) that contains metadata about the SearchWP form that was used to initiate a search. It’s transmitted via $_GET when a search form is submitted.
Structure
?swp_form[form_id]=1
- Key:
swp_form(array) - Sub-key:
form_id(integer) - Value: The numeric ID of the SearchWP form configuration
Example URLs
https://example.com/?swp_form[form_id]=1&swps=coffee
https://example.com/search/?swp_form[form_id]=2&s=espresso&swppg=2
How swp_form is Set
The swp_form parameter is automatically added as a hidden input field in every SearchWP form.
Source: Forms/Frontend.php:189
<form id="searchwp-form-1"
role="search"
method="get"
class="searchwp-form"
action="<?php echo esc_url( $target_url ); ?>">
<input type="hidden" name="swp_form[form_id]" value="<?php echo absint( $form_id ); ?>">
<!-- Other form fields -->
</form>
Key Points:
- The form ID is embedded as a hidden field during form rendering
- The field name uses array notation:
swp_form[form_id] - The value is the form’s configuration ID from the SearchWP database
- This ensures every search request can be traced back to its originating form
How swp_form Modifies WordPress Search Results
The swp_form parameter influences search results through multiple interconnected mechanisms:
1. Form Identification and Settings Retrieval
Templates/Frontend.php:1061-1066
private static function get_form_from_request() {
$form_id = isset( $_GET['swp_form']['form_id'] ) ? absint( $_GET['swp_form']['form_id'] ) : 0;
if ( ! empty( $form_id ) ) {
return SearchWPFormsStorage::get( $form_id );
}
return false;
}
This method retrieves the complete form configuration based on the form_id from the URL parameter. The form configuration contains:
- Engine selection: Which search engine to use
- Target page: Where results should be displayed
- Input field name: Whether to use
sorswpsas the search parameter - Advanced search settings: Filters for categories, tags, authors, post types
- Display preferences: Button labels, styling, etc.
2. Engine Selection
The form configuration determines which SearchWP engine processes the query.
Templates/Frontend.php:1105-1131
private static function get_engine_name( $args, $form ) {
$available_engines = Settings::get_engines();
// Priority: direct args > form settings > default
if ( ! empty( $args['engine'] ) ) {
$selected_engine = sanitize_text_field( $args['engine'] );
if ( array_key_exists( $selected_engine, $available_engines ) ) {
return $selected_engine;
}
}
if ( ! empty( $form['engine'] ) ) {
$selected_engine = $form['engine'];
if ( array_key_exists( $selected_engine, $available_engines ) ) {
return $selected_engine;
}
}
return 'default';
}
Impact on Search Results:
- Different engines can index different content sources
- Engines have custom relevance algorithms
- Weighting and ranking can differ between engines
- Some engines may search specific post types, while others search users, media, or custom content
3. Search Parameter Detection
The form’s input_name setting determines which URL parameter contains the search query.
Forms/Frontend.php:208
$search_input_name = ! empty( $form['input_name'] ) ? $form['input_name'] : 's';
Templates/Frontend.php:54-56
$form = self::get_form_from_request();
$input_name = isset( $form['input_name'] ) ? $form['input_name'] : 's';
if ( $input_name !== 'swps' ) {
return; // Don't load SearchWP templates if using standard WordPress parameter
}
Two Search Parameter Options:
swps(SearchWP’s parameter)- Triggers SearchWP’s built-in template system
- Loads SearchWP assets (CSS/JS)
- Uses SearchWP’s rendering pipeline
s(WordPress standard parameter)- Integrates with WordPress’s native search
- Can use WordPress’s default search.php template
- Allows custom theme integration
- Still powered by SearchWP engine via Native integration
Integration with WordPress Native Search
When a form uses input_name='s', SearchWP integrates with WordPress’s native search system through the Native class.
Forms/Frontend.php:643-687
public static function set_native_search_engine( $args ) {
if ( is_admin() ) {
return $args; // Forms don't work on admin side
}
$form_id = isset( $_GET['swp_form']['form_id'] ) ? absint( $_GET['swp_form']['form_id'] ) : 0;
$form = Storage::get( $form_id );
if ( empty( $form ) ) {
return $args;
}
// Only proceed if using WordPress's 's' parameter
if ( ! empty( $form['input_name'] ) && $form['input_name'] !== 's' ) {
return $args;
}
$engine_name = ! empty( $form['engine'] ) ? $form['engine'] : 'default';
if ( $engine_name === 'default' ) {
return $args;
}
$engine_settings = Settings::get_engine_settings( $engine_name );
if ( empty( $engine_settings ) ) {
return $args;
}
// Check for custom (non-WP_Post) sources
$custom_sources = array_filter(
isset( $engine_settings['sources'] ) ? $engine_settings['sources'] : [],
function ( $key ) {
return strpos( $key, 'post' . SEARCHWP_SEPARATOR ) !== 0;
},
ARRAY_FILTER_USE_KEY
);
// Set form engine as native engine only if it doesn't contain custom sources
if ( empty( $custom_sources ) ) {
$args['engine'] = $engine_name;
}
return $args;
}
How This Works:
- Filter Hook:
searchwp\native\args(line 45) - Detection: Checks if
swp_form[form_id]exists in the URL - Engine Selection: Applies the form’s configured engine to native WordPress search
- Restriction: Only works if the engine contains only WP_Post sources (no users, terms, etc.)
Native.php Integration
The Native class intercepts WordPress’s search query:
Native.php:332-430
private function find_results() {
add_filter( 'posts_pre_query', function( $posts, $query ) {
if ( ! $this->is_applicable( $query ) ) {
return $posts;
}
$search_query = get_search_query();
$args = $query->query_vars;
$args['s'] = ! empty( $search_query ) ? $search_query : $args['s'];
$args['engine'] = $query->get( 'searchwp' );
// The searchwp\native\args filter is applied here
// This is where set_native_search_engine() modifies the engine
$this->results = new \SWP_Query(
apply_filters( 'searchwp\native\args', $args, $query )
);
// Override WordPress's results
$query->max_num_pages = $this->results->max_num_pages;
$query->found_posts = $this->results->found_posts;
return $this->results->posts;
}, 999, 2 );
}
Result:
- Your theme’s
search.phptemplate runs normally - But the results come from SearchWP’s engine instead of WordPress’s default search
- The
$wp_queryglobal contains SearchWP results - You can use standard WordPress template tags:
the_title(),the_content(),the_permalink(), etc.
Form Configuration Impact
The form configuration retrieved via swp_form[form_id] affects many aspects of search behavior:
Template Selection
Templates/Frontend.php:1082-1093
private static function get_template_id( $args, $form ) {
if ( ! empty( $args['id'] ) ) {
return absint( $args['id'] );
}
if ( ! empty( $form['results-template'] ) ) {
return absint( $form['results-template'] );
}
return 1; // Default template
}
The form’s results-template setting determines which SearchWP results template is used for rendering.
Target Page Configuration
Forms/Frontend.php:174-180
$target_url = ! empty( $form['target_url'] ) ? $form['target_url'] : false;
if ( empty( $target_url ) ) {
$target_page = isset( $form['target-page'] ) ? $form['target-page'] : false;
$target_url = ! empty( $target_page ) && is_numeric( $target_page )
? get_permalink( $target_page )
: home_url( '/' );
}
The form’s target page setting determines where search results are displayed:
- Custom page with SearchWP template
- WordPress homepage with native integration
- Specific page with custom template
Advanced Search Filtering
When swp_form[form_id] is present, SearchWP applies form-specific filtering via Mods (query modifications).
Category Filtering
Forms/Frontend.php:699-805
public static function taxonomy_mod( $mods, $query ) {
global $wpdb;
$form_id = isset( $_GET['swp_form']['form_id'] ) ? absint( $_GET['swp_form']['form_id'] ) : 0;
if ( empty( $form_id ) ) {
return $mods;
}
$form = Storage::get( $form_id );
$engine = isset( $form['engine'] ) ? $form['engine'] : 'default';
// Only apply if this form's engine matches the query's engine
if ( $engine !== $query->get_engine()->get_name() ) {
return $mods;
}
if ( empty( $form['advanced-search'] ) && empty( $form['category-search'] ) ) {
return $mods;
}
// Check for category/tag filter in URL
if ( empty( $_GET['swp_tax_limiter'] ) ) {
return $mods;
}
// Build taxonomy query
// ... (creates WP_Tax_Query and applies as SearchWP Mod)
return $mods;
}
URL Parameter: ?swp_tax_limiter[category]=5
Process:
- Extracts
form_idfrom URL - Loads form configuration
- Verifies the form has category filtering enabled
- Checks if
swp_tax_limiterparameter exists - Validates the category ID against form’s allowed categories
- Creates a
WP_Tax_Queryobject - Wraps it in a SearchWP
Mod - Adds SQL JOIN and WHERE clauses to filter results
Author Filtering
Forms/Frontend.php:817-860
public static function author_mod( $mods, $query ) {
global $wpdb;
$form_id = isset( $_GET['swp_form']['form_id'] ) ? absint( $_GET['swp_form']['form_id'] ) : 0;
if ( empty( $form_id ) ) {
return $mods;
}
$form = Storage::get( $form_id );
$engine = isset( $form['engine'] ) ? $form['engine'] : 'default';
if ( $engine !== $query->get_engine()->get_name() ) {
return $mods;
}
if ( empty( $form['advanced-search'] ) || ! in_array( 'authors', $form['advanced-search-filters'], true ) ) {
return $mods;
}
$author_id = isset( $_GET['swp_author_limiter'] ) ? absint( $_GET['swp_author_limiter'] ) : 0;
if ( empty( $author_id ) ) {
return $mods;
}
// Validate author exists and is allowed
if ( ! array_key_exists( $author_id, self::get_authors( $form ) ) ) {
return $mods;
}
// Create Mod to filter by author
$mod = new \SearchWP\Mod();
$mod->set_local_table( $wpdb->posts );
$mod->on( 'ID', [ 'column' => 'id' ] );
$mod->raw_where_sql( function ( $mod ) use ( $author_id ) {
return "{$mod->get_local_table_alias()}.post_author = " . $author_id;
} );
$mods[] = $mod;
return $mods;
}
URL Parameter: ?swp_author_limiter=3
Post Type Filtering
Forms/Frontend.php:872-933
public static function post_type_mod( $mods, $query ) {
$form_id = isset( $_GET['swp_form']['form_id'] ) ? absint( $_GET['swp_form']['form_id'] ) : 0;
if ( empty( $form_id ) ) {
return $mods;
}
$form = Storage::get( $form_id );
$form_engine = isset( $form['engine'] ) ? $form['engine'] : 'default';
if ( $form_engine !== $query->get_engine()->get_name() ) {
return $mods;
}
$form_post_types = (array) $form['post-type'];
if ( empty( $form_post_types ) ) {
return $mods;
}
$advanced_filter_enabled = ! empty( $form['advanced-search'] )
&& in_array( 'post_types', $form['advanced-search-filters'], true );
if ( $advanced_filter_enabled ) {
$filter_post_type = isset( $_GET['swp_post_type_limiter'] )
? sanitize_text_field( wp_unslash( $_GET['swp_post_type_limiter'] ) )
: '';
if ( ! empty( $filter_post_type ) && in_array( $filter_post_type, $form_post_types, true ) ) {
$form_post_types = [ $filter_post_type ];
}
}
// Create Mod to limit results to specific post types
$mod = new \SearchWP\Mod();
$mod->set_where( [
[
'column' => 'source',
'value' => $mod_value,
'compare' => 'IN',
]
] );
$mods[] = $mod;
return $mods;
}
URL Parameter: ?swp_post_type_limiter=post
Tag Filtering
Similar to category filtering, using:
URL Parameter: ?swp_tax_limiter[post_tag]=12
Code Examples
Example 1: Detecting Form ID in search.php
<?php
/**
* Template Name: Custom Search Results
* File: search.php or custom template
*/
get_header();
// Check if this search came from a SearchWP form
$form_id = isset( $_GET['swp_form']['form_id'] ) ? absint( $_GET['swp_form']['form_id'] ) : 0;
if ( $form_id ) {
echo '<p>This search was initiated by SearchWP Form ID: ' . $form_id . '</p>';
// You could load form-specific display logic here
if ( $form_id === 1 ) {
// Display format for form #1
} elseif ( $form_id === 2 ) {
// Display format for form #2
}
}
// Standard WordPress loop works because Native integration
// has already replaced the results with SearchWP results
if ( have_posts() ) :
while ( have_posts() ) : the_post();
get_template_part( 'template-parts/content', 'search' );
endwhile;
the_posts_pagination();
else :
get_template_part( 'template-parts/content', 'none' );
endif;
get_footer();
Example 2: Conditional Logic Based on Form
<?php
// In your search.php or functions.php
$form_id = isset( $_GET['swp_form']['form_id'] ) ? absint( $_GET['swp_form']['form_id'] ) : 0;
if ( $form_id === 2 ) {
// Form #2 is the "Product Search" form
// Use a different layout
get_template_part( 'template-parts/search', 'products' );
} else {
// Default search layout
get_template_part( 'template-parts/search', 'default' );
}
Example 3: Accessing Form Configuration
<?php
use SearchWP\Forms\Storage as SearchWPFormsStorage;
$form_id = isset( $_GET['swp_form']['form_id'] ) ? absint( $_GET['swp_form']['form_id'] ) : 0;
if ( $form_id ) {
$form_config = SearchWPFormsStorage::get( $form_id );
if ( $form_config ) {
echo '<div class="search-info">';
echo '<p>Search Engine: ' . esc_html( $form_config['engine'] ) . '</p>';
echo '<p>Form Name: ' . esc_html( $form_config['label'] ) . '</p>';
echo '</div>';
}
}
Example 4: Reading Advanced Filters in Template
<?php
$form_id = isset( $_GET['swp_form']['form_id'] ) ? absint( $_GET['swp_form']['form_id'] ) : 0;
$category_id = isset( $_GET['swp_tax_limiter']['category'] ) ? absint( $_GET['swp_tax_limiter']['category'] ) : 0;
$author_id = isset( $_GET['swp_author_limiter'] ) ? absint( $_GET['swp_author_limiter'] ) : 0;
$post_type = isset( $_GET['swp_post_type_limiter'] ) ? sanitize_text_field( $_GET['swp_post_type_limiter'] ) : '';
if ( $form_id && ( $category_id || $author_id || $post_type ) ) {
echo '<div class="active-filters">';
echo '<h3>Active Filters:</h3>';
echo '<ul>';
if ( $category_id ) {
$category = get_term( $category_id, 'category' );
echo '<li>Category: ' . esc_html( $category->name ) . '</li>';
}
if ( $author_id ) {
$author = get_user_by( 'id', $author_id );
echo '<li>Author: ' . esc_html( $author->display_name ) . '</li>';
}
if ( $post_type ) {
$post_type_object = get_post_type_object( $post_type );
echo '<li>Post Type: ' . esc_html( $post_type_object->labels->singular_name ) . '</li>';
}
echo '</ul>';
echo '</div>';
}
Reference Code Locations
Form Parameter Injection
- File:
includes/Forms/Frontend.php - Line: 189
- Function:
render() - Purpose: Adds hidden input field to form
Form Retrieval from Request
- File:
includes/Templates/Frontend.php - Line: 1061-1070
- Function:
get_form_from_request() - Purpose: Retrieves form configuration from database
Engine Selection
- File:
includes/Templates/Frontend.php - Line: 1105-1131
- Function:
get_engine_name() - Purpose: Determines which engine to use
Native Search Integration
- File:
includes/Forms/Frontend.php - Line: 643-687
- Function:
set_native_search_engine() - Hook:
searchwp\native\args(line 45) - Purpose: Sets engine for WordPress native search
Taxonomy Filtering
- File:
includes/Forms/Frontend.php - Line: 699-805
- Function:
taxonomy_mod() - Hook:
searchwp\query\mods(line 47) - Purpose: Filters results by category/tag
Author Filtering
- File:
includes/Forms/Frontend.php - Line: 817-860
- Function:
author_mod() - Hook:
searchwp\query\mods(line 48) - Purpose: Filters results by author
Post Type Filtering
- File:
includes/Forms/Frontend.php - Line: 872-933
- Function:
post_type_mod() - Hook:
searchwp\query\mods(line 49) - Purpose: Filters results by post type
Native Query Interception
- File:
includes/Native.php - Line: 332-430
- Function:
find_results() - Hook:
posts_pre_query(line 332) - Purpose: Replaces WordPress results with SearchWP results
Summary
The swp_form[form_id] parameter is the cornerstone of SearchWP’s form system:
- Identifies which form configuration initiated the search
- Determines which search engine processes the query
- Configures display templates and styling
- Enables advanced filtering (categories, authors, tags, post types)
- Integrates with WordPress native search on
search.phptemplates - Validates filter parameters against allowed form settings
When a user submits a SearchWP form:
- The form ID is sent via
swp_form[form_id] - SearchWP retrieves the form configuration
- The configured engine is selected
- Advanced filters are validated and applied
- Results are either rendered via SearchWP templates (with
swpsparameter) or integrated into WordPress’s native search (withsparameter) - Your theme’s
search.phpreceives SearchWP-powered results instead of WordPress’s default search results
This architecture allows SearchWP to provide powerful, customizable search while maintaining compatibility with existing WordPress themes and templates.