custom-search-plugin.php aktualisiert

This commit is contained in:
M_Viper 2025-03-16 19:10:23 +00:00
parent 59e41f28cf
commit e7ff1ee33e

View File

@ -1,308 +1,308 @@
<?php
/**
* Plugin Name: Custom Search Plugin
* Plugin URI:
* Description: Fügt eine Suchfunktion als Shortcode, Widget und Menüeintrag hinzu.(Shortcode: [display_search_form])
* Version: 1.1
* Author: M_Viper
* Author URI: https://m-viper.de
* Requires at least: 6.7.2
* Tested up to: 6.7.2
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: wp-search
* Tags: search Autorsearch
*/
if (!defined('ABSPATH')) {
exit; // Sicherheitsmaßnahme
}
// 1⃣ **Aktualisiertes Suchformular mit Umschaltoption**
function csp_search_form() {
ob_start();
$search_type = isset($_GET['search_type']) ? sanitize_text_field($_GET['search_type']) : 'all';
?>
<style>
.csp-search-form {
display: flex;
flex-direction: column;
gap: 8px;
max-width: 400px;
margin: 10px auto;
padding: 15px;
background: #f8f9fa;
border-radius: 15px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
.csp-search-fields {
display: flex;
gap: 8px;
}
.csp-search-form input[type="text"] {
flex: 1;
padding: 10px;
border: 1px solid #ddd;
border-radius: 20px;
font-size: 16px;
}
.csp-search-type {
display: flex;
gap: 10px;
margin-bottom: 10px;
}
.csp-search-type label {
display: flex;
align-items: center;
gap: 5px;
cursor: pointer;
}
.csp-search-type input[type="radio"] {
margin: 0;
}
.csp-search-form input[type="submit"] {
background: #0073aa;
color: white;
border: none;
padding: 10px 15px;
border-radius: 20px;
cursor: pointer;
font-size: 16px;
transition: background 0.3s;
}
.csp-search-form input[type="submit"]:hover {
background: #005f8d;
}
</style>
<form class="csp-search-form" role="search" method="get" action="<?php echo esc_url(home_url('/')); ?>">
<div class="csp-search-type">
<label>
<input type="radio" name="search_type" value="all" <?php checked($search_type, 'all'); ?>>
<?php _e('Alle Inhalte', 'textdomain'); ?>
</label>
<label>
<input type="radio" name="search_type" value="title" <?php checked($search_type, 'title'); ?>>
<?php _e('Nur Titel', 'textdomain'); ?>
</label>
<label>
<input type="radio" name="search_type" value="guest_author" <?php checked($search_type, 'guest_author'); ?>>
<?php _e('Nur Gastautoren', 'textdomain'); ?>
</label>
</div>
<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="Suchen">
</div>
</form>
<?php
return ob_get_clean();
}
// 2⃣ **Shortcode für das Suchformular**
add_shortcode('custom_search', 'csp_search_form');
// 3⃣ **Aktualisiertes Suchfeld als Widget**
class CSP_Search_Widget extends WP_Widget {
public function __construct() {
parent::__construct(
'csp_search_widget',
__('Suche Widget', 'textdomain'),
array('description' => __('Suchfeld mit Optionen für Titel- und Gastautoren-Suche', 'textdomain'))
);
}
public function widget($args, $instance) {
echo $args['before_widget'];
echo '<h3 class="widget-title">' . __('Suche', 'textdomain') . '</h3>';
echo csp_search_form();
echo $args['after_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(
'Custom Search Settings',
'Custom 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() {
?>
<div class="wrap">
<h1>Custom Search Settings</h1>
<form method="post" action="options.php">
<?php
settings_fields('csp_settings_group');
do_settings_sections('custom-search-settings');
submit_button();
?>
</form>
</div>
<?php
}
// 6⃣ **Einstellungen registrieren**
function csp_register_settings() {
register_setting('csp_settings_group', 'csp_selected_menu');
add_settings_section(
'csp_main_section',
'Menu Selection',
'csp_section_callback',
'custom-search-settings'
);
add_settings_field(
'csp_menu_select',
'Select Menu',
'csp_menu_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 which menu to display the search form in:';
}
// 8⃣ **Callback für das Auswahlfeld**
function csp_menu_select_callback() {
$menus = wp_get_nav_menus();
$selected_menu = get_option('csp_selected_menu');
echo '<select name="csp_selected_menu">';
echo '<option value="">Select a menu</option>';
foreach ($menus as $menu) {
echo '<option value="' . $menu->term_id . '" ' . selected($selected_menu, $menu->term_id, false) . '>' . esc_html($menu->name) . '</option>';
}
echo '</select>';
}
// 9⃣ **Suchfeld zum ausgewählten Menü hinzufügen**
function csp_add_search_to_menus($items, $args) {
$selected_menu = get_option('csp_selected_menu');
if ($selected_menu && $args->menu->term_id == $selected_menu) {
$items .= '<li class="menu-item search-menu-item">' . csp_search_form() . '</li>';
}
return $items;
}
add_filter('wp_nav_menu_items', 'csp_add_search_to_menus', 10, 2);
// 🔟 **Aktualisierte Suchfunktion mit Suchtyp-Unterscheidung**
function csp_custom_search_results($query) {
if (!is_admin() && $query->is_search() && $query->is_main_query()) {
$search_type = isset($_GET['search_type']) ? sanitize_text_field($_GET['search_type']) : 'all';
$search_term = $query->get('s');
switch($search_type) {
case 'title':
$query->set('post_title_like', $search_term);
$query->set('s', '');
add_filter('posts_where', 'csp_title_search_where', 10, 2);
break;
case 'guest_author':
$meta_query = array(
array(
'key' => '_guest_author',
'value' => $search_term,
'compare' => '='
)
);
$query->set('meta_query', $meta_query);
$query->set('s', '');
$query->set('post_type', 'post');
break;
default: // 'all'
$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);
add_filter('posts_groupby', 'csp_custom_search_groupby', 10, 2);
break;
}
}
}
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')) {
$where .= $wpdb->prepare(
" AND {$wpdb->posts}.post_title LIKE %s",
'%' . $wpdb->esc_like($search_term) . '%'
);
}
return $where;
}
function csp_custom_search_where($where, $query) {
global $wpdb;
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)",
$search, $search, $search, $search, $search
);
}
return $where;
}
function csp_custom_search_join($join, $query) {
global $wpdb;
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 $join;
}
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);
?>
<?php
/**
* Plugin Name: Custom Search Plugin
* Plugin URI: https://git.viper.ipv64.net/M_Viper/WP-Suche
* Description: Fügt eine Suchfunktion als Shortcode, Widget und Menüeintrag hinzu.(Shortcode: [display_search_form])
* Version: 1.1
* Author: M_Viper
* Author URI: https://m-viper.de
* Requires at least: 6.7.2
* Tested up to: 6.7.2
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: wp-search
* Tags: search Autor-search
*/
if (!defined('ABSPATH')) {
exit; // Sicherheitsmaßnahme
}
// 1⃣ **Aktualisiertes Suchformular mit Umschaltoption**
function csp_search_form() {
ob_start();
$search_type = isset($_GET['search_type']) ? sanitize_text_field($_GET['search_type']) : 'all';
?>
<style>
.csp-search-form {
display: flex;
flex-direction: column;
gap: 8px;
max-width: 400px;
margin: 10px auto;
padding: 15px;
background: #f8f9fa;
border-radius: 15px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
.csp-search-fields {
display: flex;
gap: 8px;
}
.csp-search-form input[type="text"] {
flex: 1;
padding: 10px;
border: 1px solid #ddd;
border-radius: 20px;
font-size: 16px;
}
.csp-search-type {
display: flex;
gap: 10px;
margin-bottom: 10px;
}
.csp-search-type label {
display: flex;
align-items: center;
gap: 5px;
cursor: pointer;
}
.csp-search-type input[type="radio"] {
margin: 0;
}
.csp-search-form input[type="submit"] {
background: #0073aa;
color: white;
border: none;
padding: 10px 15px;
border-radius: 20px;
cursor: pointer;
font-size: 16px;
transition: background 0.3s;
}
.csp-search-form input[type="submit"]:hover {
background: #005f8d;
}
</style>
<form class="csp-search-form" role="search" method="get" action="<?php echo esc_url(home_url('/')); ?>">
<div class="csp-search-type">
<label>
<input type="radio" name="search_type" value="all" <?php checked($search_type, 'all'); ?>>
<?php _e('Alle Inhalte', 'textdomain'); ?>
</label>
<label>
<input type="radio" name="search_type" value="title" <?php checked($search_type, 'title'); ?>>
<?php _e('Nur Titel', 'textdomain'); ?>
</label>
<label>
<input type="radio" name="search_type" value="guest_author" <?php checked($search_type, 'guest_author'); ?>>
<?php _e('Nur Gastautoren', 'textdomain'); ?>
</label>
</div>
<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="Suchen">
</div>
</form>
<?php
return ob_get_clean();
}
// 2⃣ **Shortcode für das Suchformular**
add_shortcode('custom_search', 'csp_search_form');
// 3⃣ **Aktualisiertes Suchfeld als Widget**
class CSP_Search_Widget extends WP_Widget {
public function __construct() {
parent::__construct(
'csp_search_widget',
__('Suche Widget', 'textdomain'),
array('description' => __('Suchfeld mit Optionen für Titel- und Gastautoren-Suche', 'textdomain'))
);
}
public function widget($args, $instance) {
echo $args['before_widget'];
echo '<h3 class="widget-title">' . __('Suche', 'textdomain') . '</h3>';
echo csp_search_form();
echo $args['after_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(
'Custom Search Settings',
'Custom 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() {
?>
<div class="wrap">
<h1>Custom Search Settings</h1>
<form method="post" action="options.php">
<?php
settings_fields('csp_settings_group');
do_settings_sections('custom-search-settings');
submit_button();
?>
</form>
</div>
<?php
}
// 6⃣ **Einstellungen registrieren**
function csp_register_settings() {
register_setting('csp_settings_group', 'csp_selected_menu');
add_settings_section(
'csp_main_section',
'Menu Selection',
'csp_section_callback',
'custom-search-settings'
);
add_settings_field(
'csp_menu_select',
'Select Menu',
'csp_menu_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 which menu to display the search form in:';
}
// 8⃣ **Callback für das Auswahlfeld**
function csp_menu_select_callback() {
$menus = wp_get_nav_menus();
$selected_menu = get_option('csp_selected_menu');
echo '<select name="csp_selected_menu">';
echo '<option value="">Select a menu</option>';
foreach ($menus as $menu) {
echo '<option value="' . $menu->term_id . '" ' . selected($selected_menu, $menu->term_id, false) . '>' . esc_html($menu->name) . '</option>';
}
echo '</select>';
}
// 9⃣ **Suchfeld zum ausgewählten Menü hinzufügen**
function csp_add_search_to_menus($items, $args) {
$selected_menu = get_option('csp_selected_menu');
if ($selected_menu && $args->menu->term_id == $selected_menu) {
$items .= '<li class="menu-item search-menu-item">' . csp_search_form() . '</li>';
}
return $items;
}
add_filter('wp_nav_menu_items', 'csp_add_search_to_menus', 10, 2);
// 🔟 **Aktualisierte Suchfunktion mit Suchtyp-Unterscheidung**
function csp_custom_search_results($query) {
if (!is_admin() && $query->is_search() && $query->is_main_query()) {
$search_type = isset($_GET['search_type']) ? sanitize_text_field($_GET['search_type']) : 'all';
$search_term = $query->get('s');
switch($search_type) {
case 'title':
$query->set('post_title_like', $search_term);
$query->set('s', '');
add_filter('posts_where', 'csp_title_search_where', 10, 2);
break;
case 'guest_author':
$meta_query = array(
array(
'key' => '_guest_author',
'value' => $search_term,
'compare' => '='
)
);
$query->set('meta_query', $meta_query);
$query->set('s', '');
$query->set('post_type', 'post');
break;
default: // 'all'
$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);
add_filter('posts_groupby', 'csp_custom_search_groupby', 10, 2);
break;
}
}
}
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')) {
$where .= $wpdb->prepare(
" AND {$wpdb->posts}.post_title LIKE %s",
'%' . $wpdb->esc_like($search_term) . '%'
);
}
return $where;
}
function csp_custom_search_where($where, $query) {
global $wpdb;
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)",
$search, $search, $search, $search, $search
);
}
return $where;
}
function csp_custom_search_join($join, $query) {
global $wpdb;
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 $join;
}
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);
?>