Compare commits

...

5 Commits
1.2 ... main

Author SHA1 Message Date
894f70af8a wp-multi-search.php aktualisiert 2025-03-26 06:47:01 +00:00
2c8e4448ce plugin-metadata.json gelöscht 2025-03-26 06:46:40 +00:00
f4af6056ba wp-multi-search.php aktualisiert 2025-03-18 19:16:38 +00:00
afdf7f0c21 plugin-metadata.json aktualisiert 2025-03-18 16:14:35 +00:00
c172cde6f9 Dateien nach "/" hochladen 2025-03-18 16:14:21 +00:00

View File

@ -1,325 +1,348 @@
<?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.4
* 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** // Textdomain laden
function csp_search_form() { function csp_load_textdomain() {
ob_start(); load_plugin_textdomain('wp-multi-search', false, dirname(plugin_basename(__FILE__)) . '/languages');
$search_type = isset($_GET['search_type']) ? sanitize_text_field($_GET['search_type']) : 'all'; }
?> add_action('plugins_loaded', 'csp_load_textdomain');
<style>
.csp-search-form { // 1⃣ **Suchformular-Funktion mit integriertem CSS**
display: flex; function csp_search_form() {
flex-direction: column; ob_start();
gap: 8px; $search_type = isset($_GET['search_type']) ? sanitize_text_field($_GET['search_type']) : 'all';
max-width: auto; ?>
margin: 10px auto; <style>
padding: 15px; .csp-search-form {
background: #f8f9fa; display: flex;
border-radius: 15px; flex-direction: column;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); gap: 8px;
} max-width: 500px;
margin: 10px auto;
.csp-search-fields { padding: 15px;
display: flex; background: #f8f9fa;
gap: 8px; border-radius: 15px;
} box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
.csp-search-form input[type="text"] {
flex: 1; .csp-search-fields {
padding: 10px; display: flex;
border: 1px solid #ddd; gap: 8px;
border-radius: 20px; }
font-size: 16px;
} .csp-search-form input[type="text"] {
flex: 1;
.csp-search-type { padding: 10px;
display: flex; border: 1px solid #ddd;
gap: 10px; border-radius: 20px;
margin-bottom: 10px; font-size: 16px;
} }
.csp-search-type label { .csp-search-type {
display: flex; display: flex;
align-items: center; gap: 10px;
gap: 5px; margin-bottom: 10px;
cursor: pointer; }
}
.csp-search-type label {
.csp-search-type input[type="radio"] { display: flex;
margin: 0; align-items: center;
} gap: 5px;
cursor: pointer;
.csp-search-form input[type="submit"] { }
background: #0073aa;
color: white; .csp-search-type input[type="radio"] {
border: none; margin: 0;
padding: 10px 15px; }
border-radius: 20px;
cursor: pointer; .csp-search-form input[type="submit"] {
font-size: 16px; background: #0073aa;
transition: background 0.3s; color: white;
} border: none;
padding: 10px 15px;
.csp-search-form input[type="submit"]:hover { border-radius: 20px;
background: #005f8d; cursor: pointer;
} font-size: 16px;
</style> transition: background 0.3s;
<form class="csp-search-form" role="search" method="get" action="<?php echo esc_url(home_url('/')); ?>"> }
<div class="csp-search-type">
<label> .csp-search-form input[type="submit"]:hover {
<input type="radio" name="search_type" value="all" <?php checked($search_type, 'all'); ?>> background: #005f8d;
<?php _e('Alle Inhalte', 'textdomain'); ?> }
</label> </style>
<label> <form class="csp-search-form" role="search" method="get" action="<?php echo esc_url(home_url('/')); ?>">
<input type="radio" name="search_type" value="title" <?php checked($search_type, 'title'); ?>> <div class="csp-search-type">
<?php _e('Nur Titel', 'textdomain'); ?> <label>
</label> <input type="radio" name="search_type" value="all" <?php checked($search_type, 'all'); ?>>
<label> <?php _e('Alle Inhalte', 'wp-multi-search'); ?>
<input type="radio" name="search_type" value="guest_author" <?php checked($search_type, 'guest_author'); ?>> </label>
<?php _e('Nur Gastautoren', 'textdomain'); ?> <label>
</label> <input type="radio" name="search_type" value="title" <?php checked($search_type, 'title'); ?>>
</div> <?php _e('Titel', 'wp-multi-search'); ?>
<div class="csp-search-fields"> </label>
<input type="text" name="s" placeholder="🔍 Suchbegriff eingeben" value="<?php echo esc_attr(get_search_query()); ?>"> <label>
<input type="submit" value="Suchen"> <input type="radio" name="search_type" value="guest_author" <?php checked($search_type, 'guest_author'); ?>>
</div> <?php _e('Autoren', 'wp-multi-search'); ?>
</form> </label>
<?php </div>
return ob_get_clean(); <div class="csp-search-fields">
} <input type="text" name="s" placeholder="🔍 Suchbegriff eingeben" value="<?php echo esc_attr(get_search_query()); ?>">
<input type="submit" value="<?php _e('Suchen', 'wp-multi-search'); ?>">
// 2⃣ **Shortcode für das Suchformular** </div>
add_shortcode('custom_search', 'csp_search_form'); </form>
<?php
// 3⃣ **Suchfeld als Widget** return ob_get_clean();
class CSP_Search_Widget extends WP_Widget { }
public function __construct() {
parent::__construct( // 2⃣ **Shortcode für das Suchformular**
'csp_search_widget', add_shortcode('custom_search', 'csp_search_form');
__('Suche Widget', 'textdomain'),
array('description' => __('Suchfeld mit Optionen für Titel- und Gastautoren-Suche', 'textdomain')) // 3⃣ **Suchfeld als Widget**
); class CSP_Search_Widget extends WP_Widget {
} public function __construct() {
parent::__construct(
public function widget($args, $instance) { 'csp_search_widget',
echo $args['before_widget']; __('Suche Widget', 'wp-multi-search'),
echo '<h3 class="widget-title">' . __('Suche', 'textdomain') . '</h3>'; array('description' => __('Suchfeld mit Optionen für Titel- und Gastautoren-Suche', 'wp-multi-search'))
echo csp_search_form(); );
echo $args['after_widget']; }
}
} public function widget($args, $instance) {
echo $args['before_widget'];
function csp_register_widget() { echo '<h3 class="widget-title">' . __('Suche', 'wp-multi-search') . '</h3>';
register_widget('CSP_Search_Widget'); echo csp_search_form();
} echo $args['after_widget'];
add_action('widgets_init', 'csp_register_widget'); }
}
// 4⃣ **Einstellungsseite hinzufügen**
function csp_add_menu_page() { function csp_register_widget() {
add_options_page( register_widget('CSP_Search_Widget');
'WP-Multi-Search Settings', }
'WP-Multi-Search', add_action('widgets_init', 'csp_register_widget');
'manage_options',
'custom-search-settings', // 4⃣ **Einstellungsseite hinzufügen**
'csp_render_settings_page' function csp_add_menu_page() {
); add_options_page(
} 'WP-Multi-Search Settings',
add_action('admin_menu', 'csp_add_menu_page'); 'WP-Multi-Search',
'manage_options',
// 5⃣ **Einstellungsseite rendern** 'custom-search-settings',
function csp_render_settings_page() { 'csp_render_settings_page'
?> );
<div class="wrap"> }
<h1>WP-Multi Search Settings</h1> add_action('admin_menu', 'csp_add_menu_page');
<form method="post" action="options.php">
<?php // 5⃣ **Einstellungsseite rendern**
settings_fields('csp_settings_group'); function csp_render_settings_page() {
do_settings_sections('custom-search-settings'); ?>
submit_button(); <div class="wrap">
?> <h1><?php _e('WP-Multi Search Settings', 'wp-multi-search'); ?></h1>
</form> <form method="post" action="options.php">
</div> <?php
<?php settings_fields('csp_settings_group');
} do_settings_sections('custom-search-settings');
wp_nonce_field('csp_settings_save', 'csp_settings_nonce');
// 6⃣ **Einstellungen registrieren** submit_button();
function csp_register_settings() { ?>
register_setting('csp_settings_group', 'csp_shiftnav_position'); </form>
</div>
add_settings_section( <?php
'csp_main_section', }
'ShiftNav Position',
'csp_section_callback', // 6⃣ **Einstellungen registrieren**
'custom-search-settings' function csp_register_settings() {
); register_setting('csp_settings_group', 'csp_shiftnav_position');
add_settings_field( add_settings_section(
'csp_shiftnav_position_select', 'csp_main_section',
'Select ShiftNav Position', 'ShiftNav Position',
'csp_shiftnav_position_select_callback', 'csp_section_callback',
'custom-search-settings', 'custom-search-settings'
'csp_main_section' );
);
} add_settings_field(
add_action('admin_init', 'csp_register_settings'); 'csp_shiftnav_position_select',
'Select ShiftNav Position',
// 7⃣ **Callback für die Sektion** 'csp_shiftnav_position_select_callback',
function csp_section_callback() { 'custom-search-settings',
echo 'Choose where to display the search form in ShiftNav:'; 'csp_main_section'
} );
}
// 8⃣ **Callback für das Auswahlfeld** add_action('admin_init', 'csp_register_settings');
function csp_shiftnav_position_select_callback() {
$positions = array( // 7⃣ **Callback für die Sektion**
'top' => 'Top of ShiftNav', function csp_section_callback() {
'bottom' => 'Bottom of ShiftNav', echo __('Choose where to display the search form in ShiftNav:', 'wp-multi-search');
'none' => 'Do not display in ShiftNav' }
);
$selected_position = get_option('csp_shiftnav_position', 'none'); // 8⃣ **Callback für das Auswahlfeld**
function csp_shiftnav_position_select_callback() {
echo '<select name="csp_shiftnav_position">'; $positions = array(
foreach ($positions as $value => $label) { 'top' => __('Top of ShiftNav', 'wp-multi-search'),
echo '<option value="' . esc_attr($value) . '" ' . selected($selected_position, $value, false) . '>' . esc_html($label) . '</option>'; 'bottom' => __('Bottom of ShiftNav', 'wp-multi-search'),
} 'none' => __('Do not display in ShiftNav', 'wp-multi-search')
echo '</select>'; );
} $selected_position = get_option('csp_shiftnav_position', 'none');
// 9⃣ **Funktion zum Einfügen des Suchformulars in ShiftNav** echo '<select name="csp_shiftnav_position">';
function csp_insert_search_form_in_shiftnav($nav_menu, $args) { foreach ($positions as $value => $label) {
// Überprüfen, ob es sich um das ShiftNav-Menü handelt echo '<option value="' . esc_attr($value) . '" ' . selected($selected_position, $value, false) . '>' . esc_html($label) . '</option>';
if ($args->theme_location !== 'shiftnav') { }
return $nav_menu; echo '</select>';
} }
$position = get_option('csp_shiftnav_position', 'none'); // 9⃣ **Funktion zum Einfügen des Suchformulars in ShiftNav**
if ($position === 'none') { function csp_insert_search_form_in_shiftnav($nav_menu, $args) {
return $nav_menu; if ($args->theme_location !== 'shiftnav') {
} return $nav_menu;
}
$search_form = '<li class="menu-item">' . csp_search_form() . '</li>';
$position = get_option('csp_shiftnav_position', 'none');
if ($position === 'top') { if ($position === 'none') {
return $search_form . $nav_menu; return $nav_menu;
} elseif ($position === 'bottom') { }
return $nav_menu . $search_form;
} $search_form = '<li class="menu-item">' . csp_search_form() . '</li>';
return $nav_menu; if ($position === 'top') {
} return $search_form . $nav_menu;
add_filter('wp_nav_menu_items', 'csp_insert_search_form_in_shiftnav', 10, 2); } elseif ($position === 'bottom') {
return $nav_menu . $search_form;
// 🔟 **Aktualisierte Suchfunktion mit Suchtyp-Unterscheidung** }
function csp_custom_search_results($query) {
if (!is_admin() && $query->is_search() && $query->is_main_query()) { return $nav_menu;
$search_type = isset($_GET['search_type']) ? sanitize_text_field($_GET['search_type']) : 'all'; }
$search_term = $query->get('s'); add_filter('wp_nav_menu_items', 'csp_insert_search_form_in_shiftnav', 10, 2);
switch($search_type) { // 🔟 **Aktualisierte Suchfunktion mit Suchtyp-Unterscheidung**
case 'title': function csp_custom_search_results($query) {
$query->set('post_title_like', $search_term); if (!is_admin() && $query->is_search() && $query->is_main_query()) {
$query->set('s', ''); $search_type = isset($_GET['search_type']) ? sanitize_text_field($_GET['search_type']) : 'all';
add_filter('posts_where', 'csp_title_search_where', 10, 2); $search_term = $query->get('s');
break;
switch($search_type) {
case 'guest_author': case 'title':
$meta_query = array( $query->set('post_title_like', $search_term);
array( $query->set('s', '');
'key' => '_guest_author', add_filter('posts_where', 'csp_title_search_where', 10, 2);
'value' => $search_term, break;
'compare' => '='
) case 'guest_author':
); $meta_query = array(
$query->set('meta_query', $meta_query); array(
$query->set('s', ''); 'key' => '_guest_author',
$query->set('post_type', 'post'); 'value' => $search_term,
break; 'compare' => '='
)
default: // 'all' );
$query->set('_meta_or_title', $search_term); $query->set('meta_query', $meta_query);
add_filter('posts_search', 'csp_custom_search_where', 10, 2); $query->set('s', '');
add_filter('posts_join', 'csp_custom_search_join', 10, 2); $query->set('post_type', 'post');
add_filter('posts_groupby', 'csp_custom_search_groupby', 10, 2); break;
break;
} default: // 'all'
} $query->set('_meta_or_title', $search_term);
} add_filter('posts_search', 'csp_custom_search_where', 10, 2);
add_action('pre_get_posts', 'csp_custom_search_results'); add_filter('posts_join', 'csp_custom_search_join', 10, 2);
add_filter('posts_groupby', 'csp_custom_search_groupby', 10, 2);
// 1⃣1⃣ **Hilfsfunktionen für verschiedene Suchtypen** break;
function csp_title_search_where($where, $query) { }
global $wpdb;
if ($search_term = $query->get('post_title_like')) { // Filter nach der Abfrage entfernen
$where .= $wpdb->prepare( add_action('wp', function() use ($search_type) {
" AND {$wpdb->posts}.post_title LIKE %s", switch($search_type) {
'%' . $wpdb->esc_like($search_term) . '%' case 'title':
); remove_filter('posts_where', 'csp_title_search_where', 10);
} break;
return $where; case 'guest_author':
} // Keine zusätzlichen Filter zum Entfernen
break;
function csp_custom_search_where($where, $query) { default:
global $wpdb; remove_filter('posts_search', 'csp_custom_search_where', 10);
if ($search_term = $query->get('_meta_or_title')) { remove_filter('posts_join', 'csp_custom_search_join', 10);
$search = '%' . $wpdb->esc_like($search_term) . '%'; remove_filter('posts_groupby', 'csp_custom_search_groupby', 10);
$where .= $wpdb->prepare( break;
" 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)", add_action('pre_get_posts', 'csp_custom_search_results');
$search, $search, $search, $search, $search
); // 1⃣1⃣ **Hilfsfunktionen für verschiedene Suchtypen**
} function csp_title_search_where($where, $query) {
return $where; global $wpdb;
} if ($search_term = $query->get('post_title_like')) {
$where .= $wpdb->prepare(
function csp_custom_search_join($join, $query) { " AND {$wpdb->posts}.post_title LIKE %s",
global $wpdb; '%' . $wpdb->esc_like($search_term) . '%'
if ($query->get('_meta_or_title')) { );
$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 $where;
} }
return $join;
} function csp_custom_search_where($where, $query) {
global $wpdb;
function csp_custom_search_groupby($groupby, $query) { if ($search_term = $query->get('_meta_or_title')) {
global $wpdb; $search = '%' . $wpdb->esc_like($search_term) . '%';
if ($query->get('_meta_or_title')) { $where .= $wpdb->prepare(
$groupby = "{$wpdb->posts}.ID"; " OR ({$wpdb->posts}.post_title LIKE %s)
} OR ({$wpdb->posts}.post_content LIKE %s)
return $groupby; OR ({$wpdb->posts}.post_excerpt LIKE %s)
} OR (guest_author.meta_value LIKE %s)
OR ({$wpdb->users}.display_name LIKE %s)",
// 1⃣2⃣ **Hilfsfunktion für Gastautoren-Suche** $search, $search, $search, $search, $search
function csp_guest_author_search_where($where, $query) { );
global $wpdb; }
if ($query->get('meta_query')) { return $where;
$where .= " AND EXISTS ( }
SELECT 1 FROM {$wpdb->postmeta}
WHERE {$wpdb->postmeta}.post_id = {$wpdb->posts}.ID function csp_custom_search_join($join, $query) {
AND {$wpdb->postmeta}.meta_key = '_guest_author' global $wpdb;
AND {$wpdb->postmeta}.meta_value != '' if ($query->get('_meta_or_title')) {
)"; $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 $where; }
} return $join;
add_filter('posts_where', 'csp_guest_author_search_where', 10, 2); }
function csp_custom_search_groupby($groupby, $query) {
global $wpdb;
if ($query->get('_meta_or_title')) {
$groupby = "{$wpdb->posts}.ID";
}
return $groupby;
}
// 1⃣2⃣ **Hilfsfunktion für Gastautoren-Suche**
function csp_guest_author_search_where($where, $query) {
global $wpdb;
if ($query->get('meta_query')) {
$where .= " AND EXISTS (
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 != ''
)";
}
return $where;
}
add_filter('posts_where', 'csp_guest_author_search_where', 10, 2);
?> ?>