wp-multi-search.php aktualisiert

This commit is contained in:
M_Viper 2025-03-18 19:16:38 +00:00
parent afdf7f0c21
commit f4af6056ba

View File

@ -1,325 +1,326 @@
<?php <?php
/** /**
* Plugin Name: WP-Multi Search * Plugin Name: WP-Multi Search
* Plugin URI: https://git.viper.ipv64.net/M_Viper/WP-Multi-Search * Plugin URI: https://git.viper.ipv64.net/M_Viper/WP-Multi-Search
* Description: Fügt eine Suchfunktion als Shortcode, Widget und Menüeintrag hinzu.(Shortcode: [display_search_form]) * Description: Fügt eine Suchfunktion als Shortcode, Widget und Menüeintrag hinzu.(Shortcode: [custom_search])
* Version: 1.2 * Version: 1.3
* Author: M_Viper * Author: M_Viper
* Author URI: https://m-viper.de * Author URI: https://m-viper.de
* Requires at least: 6.7.2 * Requires at least: 6.7.2
* Tested up to: 6.7.2 * Tested up to: 6.7.2
* License: GPL2 * License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html * License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: wp-multi-search * Text Domain: wp-multi-search
* Tags: search Autor-search * Tags: search Autor-search
*/ */
if (!defined('ABSPATH')) { if (!defined('ABSPATH')) {
exit; // Sicherheitsmaßnahme exit; // Sicherheitsmaßnahme
} }
// 1⃣ **Suchformular-Funktion mit integriertem CSS** // 1⃣ **Suchformular-Funktion mit integriertem CSS**
function csp_search_form() { function csp_search_form() {
ob_start(); ob_start();
$search_type = isset($_GET['search_type']) ? sanitize_text_field($_GET['search_type']) : 'all'; $search_type = isset($_GET['search_type']) ? sanitize_text_field($_GET['search_type']) : 'all';
?> ?>
<style> <style>
.csp-search-form { .csp-search-form {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
gap: 8px; gap: 8px;
max-width: auto; max-width: auto;
margin: 10px auto; margin: 10px auto;
padding: 15px; padding: 15px;
background: #f8f9fa; background: #f8f9fa;
border-radius: 15px; border-radius: 15px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
} }
.csp-search-fields { .csp-search-fields {
display: flex; display: flex;
gap: 8px; gap: 8px;
} }
.csp-search-form input[type="text"] { .csp-search-form input[type="text"] {
flex: 1; flex: 1;
padding: 10px; padding: 10px;
border: 1px solid #ddd; border: 1px solid #ddd;
border-radius: 20px; border-radius: 20px;
font-size: 16px; font-size: 16px;
} }
.csp-search-type { .csp-search-type {
display: flex; display: flex;
gap: 10px; gap: 10px;
margin-bottom: 10px; margin-bottom: 10px;
} }
.csp-search-type label { .csp-search-type label {
display: flex; display: flex;
align-items: center; align-items: center;
gap: 5px; gap: 5px;
cursor: pointer; cursor: pointer;
} }
.csp-search-type input[type="radio"] { .csp-search-type input[type="radio"] {
margin: 0; margin: 0;
} }
.csp-search-form input[type="submit"] { .csp-search-form input[type="submit"] {
background: #0073aa; background: #0073aa;
color: white; color: white;
border: none; border: none;
padding: 10px 15px; padding: 10px 15px;
border-radius: 20px; border-radius: 20px;
cursor: pointer; cursor: pointer;
font-size: 16px; font-size: 16px;
transition: background 0.3s; transition: background 0.3s;
} }
.csp-search-form input[type="submit"]:hover { .csp-search-form input[type="submit"]:hover {
background: #005f8d; background: #005f8d;
} }
</style> </style>
<form class="csp-search-form" role="search" method="get" action="<?php echo esc_url(home_url('/')); ?>"> <form class="csp-search-form" role="search" method="get" action="<?php echo esc_url(home_url('/')); ?>">
<div class="csp-search-type"> <div class="csp-search-type">
<label> <label>
<input type="radio" name="search_type" value="all" <?php checked($search_type, 'all'); ?>> <input type="radio" name="search_type" value="all" <?php checked($search_type, 'all'); ?>>
<?php _e('Alle Inhalte', 'textdomain'); ?> <?php _e('Alle Inhalte', 'textdomain'); ?>
</label> </label>
<label> <label>
<input type="radio" name="search_type" value="title" <?php checked($search_type, 'title'); ?>> <input type="radio" name="search_type" value="title" <?php checked($search_type, 'title'); ?>>
<?php _e('Nur Titel', 'textdomain'); ?> <?php _e('Titel', 'textdomain'); ?>
</label> </label>
<label> <label>
<input type="radio" name="search_type" value="guest_author" <?php checked($search_type, 'guest_author'); ?>> <input type="radio" name="search_type" value="guest_author" <?php checked($search_type, 'guest_author'); ?>>
<?php _e('Nur Gastautoren', 'textdomain'); ?> <?php _e('Autoren', 'textdomain'); ?>
</label> </label>
</div> </div>
<div class="csp-search-fields"> <div class="csp-search-fields">
<input type="text" name="s" placeholder="🔍 Suchbegriff eingeben" value="<?php echo esc_attr(get_search_query()); ?>"> <input type="text" name="s" placeholder="🔍 Suchbegriff eingeben" value="<?php echo esc_attr(get_search_query()); ?>">
<input type="submit" value="Suchen">
</div> </div>
</form> <input type="submit" value="Suchen">
<?php </form>
return ob_get_clean(); <?php
} return ob_get_clean();
}
// 2⃣ **Shortcode für das Suchformular**
add_shortcode('custom_search', 'csp_search_form'); // 2⃣ **Shortcode für das Suchformular**
add_shortcode('custom_search', 'csp_search_form');
// 3⃣ **Suchfeld als Widget**
class CSP_Search_Widget extends WP_Widget { // 3⃣ **Suchfeld als Widget**
public function __construct() { class CSP_Search_Widget extends WP_Widget {
parent::__construct( public function __construct() {
'csp_search_widget', parent::__construct(
__('Suche Widget', 'textdomain'), 'csp_search_widget',
array('description' => __('Suchfeld mit Optionen für Titel- und Gastautoren-Suche', 'textdomain')) __('Suche Widget', 'textdomain'),
); array('description' => __('Suchfeld mit Optionen für Titel- und Gastautoren-Suche', 'textdomain'))
} );
}
public function widget($args, $instance) {
echo $args['before_widget']; public function widget($args, $instance) {
echo '<h3 class="widget-title">' . __('Suche', 'textdomain') . '</h3>'; echo $args['before_widget'];
echo csp_search_form(); echo '<h3 class="widget-title">' . __('Suche', 'textdomain') . '</h3>';
echo $args['after_widget']; echo csp_search_form();
} echo $args['after_widget'];
} }
}
function csp_register_widget() {
register_widget('CSP_Search_Widget'); function csp_register_widget() {
} register_widget('CSP_Search_Widget');
add_action('widgets_init', 'csp_register_widget'); }
add_action('widgets_init', 'csp_register_widget');
// 4⃣ **Einstellungsseite hinzufügen**
function csp_add_menu_page() { // 4⃣ **Einstellungsseite hinzufügen**
add_options_page( function csp_add_menu_page() {
'WP-Multi-Search Settings', add_options_page(
'WP-Multi-Search', 'WP-Multi-Search Settings',
'manage_options', 'WP-Multi-Search',
'custom-search-settings', 'manage_options',
'csp_render_settings_page' 'custom-search-settings',
); 'csp_render_settings_page'
} );
add_action('admin_menu', 'csp_add_menu_page'); }
add_action('admin_menu', 'csp_add_menu_page');
// 5⃣ **Einstellungsseite rendern**
function csp_render_settings_page() { // 5⃣ **Einstellungsseite rendern**
?> function csp_render_settings_page() {
<div class="wrap"> ?>
<h1>WP-Multi Search Settings</h1> <div class="wrap">
<form method="post" action="options.php"> <h1>WP-Multi Search Settings</h1>
<?php <form method="post" action="options.php">
settings_fields('csp_settings_group'); <?php
do_settings_sections('custom-search-settings'); settings_fields('csp_settings_group');
submit_button(); do_settings_sections('custom-search-settings');
?> submit_button();
</form> ?>
</div> </form>
<?php </div>
} <?php
}
// 6⃣ **Einstellungen registrieren**
function csp_register_settings() { // 6⃣ **Einstellungen registrieren**
register_setting('csp_settings_group', 'csp_shiftnav_position'); function csp_register_settings() {
register_setting('csp_settings_group', 'csp_shiftnav_position');
add_settings_section(
'csp_main_section', add_settings_section(
'ShiftNav Position', 'csp_main_section',
'csp_section_callback', 'ShiftNav Position',
'custom-search-settings' 'csp_section_callback',
); 'custom-search-settings'
);
add_settings_field(
'csp_shiftnav_position_select', add_settings_field(
'Select ShiftNav Position', 'csp_shiftnav_position_select',
'csp_shiftnav_position_select_callback', 'Select ShiftNav Position',
'custom-search-settings', 'csp_shiftnav_position_select_callback',
'csp_main_section' 'custom-search-settings',
); 'csp_main_section'
} );
add_action('admin_init', 'csp_register_settings'); }
add_action('admin_init', 'csp_register_settings');
// 7⃣ **Callback für die Sektion**
function csp_section_callback() { // 7⃣ **Callback für die Sektion**
echo 'Choose where to display the search form in ShiftNav:'; function csp_section_callback() {
} echo 'Choose where to display the search form in ShiftNav:';
}
// 8⃣ **Callback für das Auswahlfeld**
function csp_shiftnav_position_select_callback() { // 8⃣ **Callback für das Auswahlfeld**
$positions = array( function csp_shiftnav_position_select_callback() {
'top' => 'Top of ShiftNav', $positions = array(
'bottom' => 'Bottom of ShiftNav', 'top' => 'Top of ShiftNav',
'none' => 'Do not display in ShiftNav' 'bottom' => 'Bottom of ShiftNav',
); 'none' => 'Do not display in ShiftNav'
$selected_position = get_option('csp_shiftnav_position', 'none'); );
$selected_position = get_option('csp_shiftnav_position', 'none');
echo '<select name="csp_shiftnav_position">';
foreach ($positions as $value => $label) { echo '<select name="csp_shiftnav_position">';
echo '<option value="' . esc_attr($value) . '" ' . selected($selected_position, $value, false) . '>' . esc_html($label) . '</option>'; foreach ($positions as $value => $label) {
} echo '<option value="' . esc_attr($value) . '" ' . selected($selected_position, $value, false) . '>' . esc_html($label) . '</option>';
echo '</select>'; }
} echo '</select>';
}
// 9⃣ **Funktion zum Einfügen des Suchformulars in ShiftNav**
function csp_insert_search_form_in_shiftnav($nav_menu, $args) { // 9⃣ **Funktion zum Einfügen des Suchformulars in ShiftNav**
// Überprüfen, ob es sich um das ShiftNav-Menü handelt function csp_insert_search_form_in_shiftnav($nav_menu, $args) {
if ($args->theme_location !== 'shiftnav') { // Überprüfen, ob es sich um das ShiftNav-Menü handelt
return $nav_menu; if ($args->theme_location !== 'shiftnav') {
} return $nav_menu;
}
$position = get_option('csp_shiftnav_position', 'none');
if ($position === 'none') { $position = get_option('csp_shiftnav_position', 'none');
return $nav_menu; if ($position === 'none') {
} return $nav_menu;
}
$search_form = '<li class="menu-item">' . csp_search_form() . '</li>';
$search_form = '<li class="menu-item">' . csp_search_form() . '</li>';
if ($position === 'top') {
return $search_form . $nav_menu; if ($position === 'top') {
} elseif ($position === 'bottom') { return $search_form . $nav_menu;
return $nav_menu . $search_form; } elseif ($position === 'bottom') {
} return $nav_menu . $search_form;
}
return $nav_menu;
} return $nav_menu;
add_filter('wp_nav_menu_items', 'csp_insert_search_form_in_shiftnav', 10, 2); }
add_filter('wp_nav_menu_items', 'csp_insert_search_form_in_shiftnav', 10, 2);
// 🔟 **Aktualisierte Suchfunktion mit Suchtyp-Unterscheidung**
function csp_custom_search_results($query) { // 🔟 **Aktualisierte Suchfunktion mit Suchtyp-Unterscheidung**
if (!is_admin() && $query->is_search() && $query->is_main_query()) { function csp_custom_search_results($query) {
$search_type = isset($_GET['search_type']) ? sanitize_text_field($_GET['search_type']) : 'all'; if (!is_admin() && $query->is_search() && $query->is_main_query()) {
$search_term = $query->get('s'); $search_type = isset($_GET['search_type']) ? sanitize_text_field($_GET['search_type']) : 'all';
$search_term = $query->get('s');
switch($search_type) {
case 'title': switch($search_type) {
$query->set('post_title_like', $search_term); case 'title':
$query->set('s', ''); $query->set('post_title_like', $search_term);
add_filter('posts_where', 'csp_title_search_where', 10, 2); $query->set('s', '');
break; add_filter('posts_where', 'csp_title_search_where', 10, 2);
break;
case 'guest_author':
$meta_query = array( case 'guest_author':
array( $meta_query = array(
'key' => '_guest_author', array(
'value' => $search_term, 'key' => '_guest_author',
'compare' => '=' 'value' => $search_term,
) 'compare' => '='
); )
$query->set('meta_query', $meta_query); );
$query->set('s', ''); $query->set('meta_query', $meta_query);
$query->set('post_type', 'post'); $query->set('s', '');
break; $query->set('post_type', 'post');
break;
default: // 'all'
$query->set('_meta_or_title', $search_term); default: // 'all'
add_filter('posts_search', 'csp_custom_search_where', 10, 2); $query->set('_meta_or_title', $search_term);
add_filter('posts_join', 'csp_custom_search_join', 10, 2); add_filter('posts_search', 'csp_custom_search_where', 10, 2);
add_filter('posts_groupby', 'csp_custom_search_groupby', 10, 2); add_filter('posts_join', 'csp_custom_search_join', 10, 2);
break; add_filter('posts_groupby', 'csp_custom_search_groupby', 10, 2);
} break;
} }
} }
add_action('pre_get_posts', 'csp_custom_search_results'); }
add_action('pre_get_posts', 'csp_custom_search_results');
// 1⃣1⃣ **Hilfsfunktionen für verschiedene Suchtypen**
function csp_title_search_where($where, $query) { // 1⃣1⃣ **Hilfsfunktionen für verschiedene Suchtypen**
global $wpdb; function csp_title_search_where($where, $query) {
if ($search_term = $query->get('post_title_like')) { global $wpdb;
$where .= $wpdb->prepare( if ($search_term = $query->get('post_title_like')) {
" AND {$wpdb->posts}.post_title LIKE %s", $where .= $wpdb->prepare(
'%' . $wpdb->esc_like($search_term) . '%' " AND {$wpdb->posts}.post_title LIKE %s",
); '%' . $wpdb->esc_like($search_term) . '%'
} );
return $where; }
} return $where;
}
function csp_custom_search_where($where, $query) {
global $wpdb; function csp_custom_search_where($where, $query) {
if ($search_term = $query->get('_meta_or_title')) { global $wpdb;
$search = '%' . $wpdb->esc_like($search_term) . '%'; if ($search_term = $query->get('_meta_or_title')) {
$where .= $wpdb->prepare( $search = '%' . $wpdb->esc_like($search_term) . '%';
" OR ({$wpdb->posts}.post_title LIKE %s) $where .= $wpdb->prepare(
OR ({$wpdb->posts}.post_content LIKE %s) " OR ({$wpdb->posts}.post_title LIKE %s)
OR ({$wpdb->posts}.post_excerpt LIKE %s) OR ({$wpdb->posts}.post_content LIKE %s)
OR (guest_author.meta_value LIKE %s) OR ({$wpdb->posts}.post_excerpt LIKE %s)
OR ({$wpdb->users}.display_name LIKE %s)", OR (guest_author.meta_value LIKE %s)
$search, $search, $search, $search, $search OR ({$wpdb->users}.display_name LIKE %s)",
); $search, $search, $search, $search, $search
} );
return $where; }
} return $where;
}
function csp_custom_search_join($join, $query) {
global $wpdb; function csp_custom_search_join($join, $query) {
if ($query->get('_meta_or_title')) { global $wpdb;
$join .= " LEFT JOIN {$wpdb->postmeta} guest_author ON ({$wpdb->posts}.ID = guest_author.post_id AND guest_author.meta_key = '_guest_author')"; if ($query->get('_meta_or_title')) {
$join .= " LEFT JOIN {$wpdb->users} ON ({$wpdb->posts}.post_author = {$wpdb->users}.ID)"; $join .= " LEFT JOIN {$wpdb->postmeta} guest_author ON ({$wpdb->posts}.ID = guest_author.post_id AND guest_author.meta_key = '_guest_author')";
} $join .= " LEFT JOIN {$wpdb->users} ON ({$wpdb->posts}.post_author = {$wpdb->users}.ID)";
return $join; }
} return $join;
}
function csp_custom_search_groupby($groupby, $query) {
global $wpdb; function csp_custom_search_groupby($groupby, $query) {
if ($query->get('_meta_or_title')) { global $wpdb;
$groupby = "{$wpdb->posts}.ID"; if ($query->get('_meta_or_title')) {
} $groupby = "{$wpdb->posts}.ID";
return $groupby; }
} return $groupby;
}
// 1⃣2⃣ **Hilfsfunktion für Gastautoren-Suche**
function csp_guest_author_search_where($where, $query) { // 1⃣2⃣ **Hilfsfunktion für Gastautoren-Suche**
global $wpdb; function csp_guest_author_search_where($where, $query) {
if ($query->get('meta_query')) { global $wpdb;
$where .= " AND EXISTS ( if ($query->get('meta_query')) {
SELECT 1 FROM {$wpdb->postmeta} $where .= " AND EXISTS (
WHERE {$wpdb->postmeta}.post_id = {$wpdb->posts}.ID SELECT 1 FROM {$wpdb->postmeta}
AND {$wpdb->postmeta}.meta_key = '_guest_author' WHERE {$wpdb->postmeta}.post_id = {$wpdb->posts}.ID
AND {$wpdb->postmeta}.meta_value != '' AND {$wpdb->postmeta}.meta_key = '_guest_author'
)"; AND {$wpdb->postmeta}.meta_value != ''
} )";
return $where; }
} return $where;
add_filter('posts_where', 'csp_guest_author_search_where', 10, 2); }
add_filter('posts_where', 'csp_guest_author_search_where', 10, 2);
?>
?>