custom-search-plugin.php aktualisiert

This commit is contained in:
M_Viper 2025-03-16 19:24:55 +00:00
parent e7ff1ee33e
commit 6766fb3e51

View File

@ -18,7 +18,7 @@ if (!defined('ABSPATH')) {
exit; // Sicherheitsmaßnahme
}
// 1⃣ **Aktualisiertes Suchformular mit Umschaltoption**
// 1⃣ **Suchformular-Funktion mit integriertem CSS**
function csp_search_form() {
ob_start();
$search_type = isset($_GET['search_type']) ? sanitize_text_field($_GET['search_type']) : 'all';
@ -28,7 +28,7 @@ function csp_search_form() {
display: flex;
flex-direction: column;
gap: 8px;
max-width: 400px;
max-width: auto;
margin: 10px auto;
padding: 15px;
background: #f8f9fa;
@ -108,7 +108,7 @@ function csp_search_form() {
// 2⃣ **Shortcode für das Suchformular**
add_shortcode('custom_search', 'csp_search_form');
// 3⃣ **Aktualisiertes Suchfeld als Widget**
// 3⃣ **Suchfeld als Widget**
class CSP_Search_Widget extends WP_Widget {
public function __construct() {
parent::__construct(
@ -161,19 +161,19 @@ function csp_render_settings_page() {
// 6⃣ **Einstellungen registrieren**
function csp_register_settings() {
register_setting('csp_settings_group', 'csp_selected_menu');
register_setting('csp_settings_group', 'csp_shiftnav_position');
add_settings_section(
'csp_main_section',
'Menu Selection',
'ShiftNav Position',
'csp_section_callback',
'custom-search-settings'
);
add_settings_field(
'csp_menu_select',
'Select Menu',
'csp_menu_select_callback',
'csp_shiftnav_position_select',
'Select ShiftNav Position',
'csp_shiftnav_position_select_callback',
'custom-search-settings',
'csp_main_section'
);
@ -182,31 +182,48 @@ 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:';
echo 'Choose where to display the search form in ShiftNav:';
}
// 8⃣ **Callback für das Auswahlfeld**
function csp_menu_select_callback() {
$menus = wp_get_nav_menus();
$selected_menu = get_option('csp_selected_menu');
function csp_shiftnav_position_select_callback() {
$positions = array(
'top' => 'Top of ShiftNav',
'bottom' => 'Bottom of ShiftNav',
'none' => 'Do not display in ShiftNav'
);
$selected_position = get_option('csp_shiftnav_position', 'none');
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 name="csp_shiftnav_position">';
foreach ($positions as $value => $label) {
echo '<option value="' . esc_attr($value) . '" ' . selected($selected_position, $value, false) . '>' . esc_html($label) . '</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>';
// 9⃣ **Funktion zum Einfügen des Suchformulars in ShiftNav**
function csp_insert_search_form_in_shiftnav($nav_menu, $args) {
// Überprüfen, ob es sich um das ShiftNav-Menü handelt
if ($args->theme_location !== 'shiftnav') {
return $nav_menu;
}
return $items;
$position = get_option('csp_shiftnav_position', 'none');
if ($position === 'none') {
return $nav_menu;
}
add_filter('wp_nav_menu_items', 'csp_add_search_to_menus', 10, 2);
$search_form = '<li class="menu-item">' . csp_search_form() . '</li>';
if ($position === 'top') {
return $search_form . $nav_menu;
} elseif ($position === 'bottom') {
return $nav_menu . $search_form;
}
return $nav_menu;
}
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) {