From 75c0cb0d6d35eabb5b53c5613533a6a102dcd3ba Mon Sep 17 00:00:00 2001 From: M_Viper Date: Wed, 9 Apr 2025 14:22:11 +0000 Subject: [PATCH] wp-multi-search.php aktualisiert --- wp-multi-search.php | 258 +++++++++++++------------------------------- 1 file changed, 76 insertions(+), 182 deletions(-) diff --git a/wp-multi-search.php b/wp-multi-search.php index b697181..a24a9d7 100644 --- a/wp-multi-search.php +++ b/wp-multi-search.php @@ -1,9 +1,9 @@ - - - + + + ' . __('Ein Fehler ist aufgetreten', 'wp-multi-search') . '

'; + } } -// 2️⃣ **Shortcode für das Suchformular** add_shortcode('custom_search', 'csp_search_form'); -// 3️⃣ **Suchfeld als Widget** class CSP_Search_Widget extends WP_Widget { public function __construct() { - parent::__construct( - 'csp_search_widget', - __('Suche Widget', 'wp-multi-search'), - array('description' => __('Suchfeld mit Optionen für Titel- und Gastautoren-Suche', 'wp-multi-search')) - ); - } - - public function widget($args, $instance) { - echo $args['before_widget']; - echo '

' . __('Suche', 'wp-multi-search') . '

'; - echo csp_search_form(); - echo $args['after_widget']; + parent::__construct('csp_search_widget', __('Suche Widget', 'wp-multi-search'), array('description' => __('Suchfeld mit Optionen für Titel- und Gastautoren-Suche', 'wp-multi-search'))); } + public function widget($args, $instance) { echo $args['before_widget'] . '

' . __('Suche', 'wp-multi-search') . '

' . csp_search_form() . $args['after_widget']; } + public function form($instance) { echo '

' . __('Keine Einstellungen verfügbar', 'wp-multi-search') . '

'; } + public function update($new_instance, $old_instance) { return $new_instance; } } -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'); -// 4️⃣ **Einstellungsseite hinzufügen** function csp_add_menu_page() { - add_options_page( - 'WP-Multi-Search Settings', - 'WP-Multi-Search', - 'manage_options', - 'custom-search-settings', - 'csp_render_settings_page' - ); + add_options_page('WP-Multi-Search Settings', 'WP-Multi-Search', 'manage_options', 'custom-search-settings', 'csp_render_settings_page'); } add_action('admin_menu', 'csp_add_menu_page'); -// 5️⃣ **Einstellungsseite rendern** function csp_render_settings_page() { + if (isset($_POST['csp_settings_nonce']) && !wp_verify_nonce($_POST['csp_settings_nonce'], 'csp_settings_save')) { + wp_die('Security check failed'); + } ?>

- +
'sanitize_text_field')); + add_settings_section('csp_main_section', 'ShiftNav Position', 'csp_section_callback', 'custom-search-settings'); + add_settings_field('csp_shiftnav_position_select', 'Select ShiftNav Position', 'csp_shiftnav_position_select_callback', 'custom-search-settings', 'csp_main_section'); } add_action('admin_init', 'csp_register_settings'); -// 7️⃣ **Callback für die Sektion** -function csp_section_callback() { - echo __('Choose where to display the search form in ShiftNav:', 'wp-multi-search'); -} +function csp_section_callback() { echo __('Choose where to display the search form in ShiftNav:', 'wp-multi-search'); } -// 8️⃣ **Callback für das Auswahlfeld** function csp_shiftnav_position_select_callback() { - $positions = array( - 'top' => __('Top of ShiftNav', 'wp-multi-search'), - 'bottom' => __('Bottom of ShiftNav', 'wp-multi-search'), - 'none' => __('Do not display in ShiftNav', 'wp-multi-search') - ); + $positions = array('top' => __('Top of ShiftNav', 'wp-multi-search'), 'bottom' => __('Bottom of ShiftNav', 'wp-multi-search'), 'none' => __('Do not display in ShiftNav', 'wp-multi-search')); $selected_position = get_option('csp_shiftnav_position', 'none'); - echo ''; } -// 9️⃣ **Funktion zum Einfügen des Suchformulars in ShiftNav** function csp_insert_search_form_in_shiftnav($nav_menu, $args) { - if ($args->theme_location !== 'shiftnav') { - return $nav_menu; - } - + if (!isset($args->theme_location) || $args->theme_location !== 'shiftnav') return $nav_menu; $position = get_option('csp_shiftnav_position', 'none'); - if ($position === 'none') { - return $nav_menu; - } - + if ($position === 'none') return $nav_menu; $search_form = ''; - - if ($position === 'top') { - return $search_form . $nav_menu; - } elseif ($position === 'bottom') { - return $nav_menu . $search_form; - } - - return $nav_menu; + return $position === 'top' ? $search_form . $nav_menu : $nav_menu . $search_form; } 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) { if (!is_admin() && $query->is_search() && $query->is_main_query()) { + $query->set('post_status', 'publish'); + $search_type = isset($_GET['search_type']) ? sanitize_text_field($_GET['search_type']) : 'all'; $search_term = $query->get('s'); @@ -249,7 +144,7 @@ function csp_custom_search_results($query) { array( 'key' => '_guest_author', 'value' => $search_term, - 'compare' => '=' + 'compare' => 'LIKE' ) ); $query->set('meta_query', $meta_query); @@ -257,7 +152,7 @@ function csp_custom_search_results($query) { $query->set('post_type', 'post'); break; - default: // 'all' + default: $query->set('_meta_or_title', $search_term); add_filter('posts_search', 'csp_custom_search_where', 10, 2); add_filter('posts_join', 'csp_custom_search_join', 10, 2); @@ -265,14 +160,17 @@ function csp_custom_search_results($query) { break; } - // Filter nach der Abfrage entfernen + add_filter('posts_request', function($sql) { + error_log('SQL Query: ' . $sql); + return $sql; + }); + add_action('wp', function() use ($search_type) { switch($search_type) { case 'title': remove_filter('posts_where', 'csp_title_search_where', 10); break; case 'guest_author': - // Keine zusätzlichen Filter zum Entfernen break; default: remove_filter('posts_search', 'csp_custom_search_where', 10); @@ -285,7 +183,6 @@ function csp_custom_search_results($query) { } add_action('pre_get_posts', 'csp_custom_search_results'); -// 1️⃣1️⃣ **Hilfsfunktionen für verschiedene Suchtypen** function csp_title_search_where($where, $query) { global $wpdb; if ($search_term = $query->get('post_title_like')) { @@ -302,11 +199,11 @@ function csp_custom_search_where($where, $query) { if ($search_term = $query->get('_meta_or_title')) { $search = '%' . $wpdb->esc_like($search_term) . '%'; $where .= $wpdb->prepare( - " OR ({$wpdb->posts}.post_title LIKE %s) - OR ({$wpdb->posts}.post_content LIKE %s) - OR ({$wpdb->posts}.post_excerpt LIKE %s) - OR (guest_author.meta_value LIKE %s) - OR ({$wpdb->users}.display_name LIKE %s)", + " AND ({$wpdb->posts}.post_title LIKE %s + OR {$wpdb->posts}.post_content LIKE %s + OR {$wpdb->posts}.post_excerpt LIKE %s + OR guest_author.meta_value LIKE %s + OR {$wpdb->users}.display_name LIKE %s)", $search, $search, $search, $search, $search ); } @@ -330,7 +227,6 @@ function csp_custom_search_groupby($groupby, $query) { return $groupby; } -// 1️⃣2️⃣ **Hilfsfunktion für Gastautoren-Suche** function csp_guest_author_search_where($where, $query) { global $wpdb; if ($query->get('meta_query')) { @@ -338,11 +234,9 @@ function csp_guest_author_search_where($where, $query) { SELECT 1 FROM {$wpdb->postmeta} WHERE {$wpdb->postmeta}.post_id = {$wpdb->posts}.ID AND {$wpdb->postmeta}.meta_key = '_guest_author' - AND {$wpdb->postmeta}.meta_value != '' + AND {$wpdb->postmeta}.meta_value LIKE '%" . $wpdb->esc_like($query->get('s')) . "%' )"; } return $where; } -add_filter('posts_where', 'csp_guest_author_search_where', 10, 2); - -?> \ No newline at end of file +add_filter('posts_where', 'csp_guest_author_search_where', 10, 2); \ No newline at end of file