Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Last active December 11, 2022 07:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jchristopher/c21550e512554c6fc909 to your computer and use it in GitHub Desktop.
Save jchristopher/c21550e512554c6fc909 to your computer and use it in GitHub Desktop.
Prepend a dropdown in SearchWP Shortcode search forms with Supplemental Engines
<?php
/*
Plugin Name: SearchWP Customizations
Plugin URI:
Description:
Version:
Author: Jonathan Christopher
Author URI:
License:
License URI:
*/
function my_searchwp_shortcodes_before_input() {
$settings = SWP()->settings;
$engines_settings = isset( $settings['engines'] ) ? $settings['engines'] : array();
if ( empty( $engines_settings ) ) {
return;
}
// get all supplemental engine names
$engines = array();
foreach ( $engines_settings as $engine_name => $engine_settings ) {
if ( isset( $engine_settings['searchwp_engine_label'] ) ) {
$engines[ $engine_name ] = $engine_settings['searchwp_engine_label'];
}
}
if ( empty( $engines ) ) {
return;
}
$active_engine = isset( $_GET['my_searchwp_engine'] ) ? $_GET['my_searchwp_engine'] : false;
?>
<select name="my_searchwp_engine">
<?php foreach ( $engines as $engine_name => $engine_label ) : ?>
<option value="<?php echo esc_attr( $engine_name ); ?>" <?php selected( $active_engine, $engine_name ); ?>><?php echo esc_html( $engine_label ); ?></option>
<?php endforeach; ?>
</select>
<?php }
add_action( 'searchwp_shortcodes_before_input', 'my_searchwp_shortcodes_before_input' );
// dynamically swap out the engine config when applicable
function my_searchwp_shortcodes_engine( $engine ) {
if ( ! isset( $_GET['my_searchwp_engine'] ) ) {
return $engine;
}
$engine_name = sanitize_key( $_GET['my_searchwp_engine'] );
$settings = SWP()->settings;
// if valid engine name, return that
return isset( $settings['engines'][ $engine_name ] ) ? $engine_name : $engine;
}
add_filter( 'searchwp_shortcodes_engine', 'my_searchwp_shortcodes_engine' );
function my_searchwp_shortcodes_pagination_engine_fix( $url ) {
if ( isset( $_GET['my_searchwp_engine'] ) ) {
$url = remove_query_arg( 'engine', $url );
$url = add_query_arg( array(
'my_searchwp_engine' => sanitize_key( $_GET['my_searchwp_engine'] ),
), $url );
}
return esc_url( $url );
}
add_filter( 'searchwp_shortcodes_pagination_prev', 'my_searchwp_shortcodes_pagination_engine_fix' );
add_filter( 'searchwp_shortcodes_pagination_next', 'my_searchwp_shortcodes_pagination_engine_fix' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment