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 exit; // Sicherheitsmaßnahme
} }
// 1⃣ **Aktualisiertes Suchformular mit Umschaltoption** // 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';
@ -28,7 +28,7 @@ function csp_search_form() {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
gap: 8px; gap: 8px;
max-width: 400px; max-width: auto;
margin: 10px auto; margin: 10px auto;
padding: 15px; padding: 15px;
background: #f8f9fa; background: #f8f9fa;
@ -108,7 +108,7 @@ function csp_search_form() {
// 2⃣ **Shortcode für das Suchformular** // 2⃣ **Shortcode für das Suchformular**
add_shortcode('custom_search', 'csp_search_form'); add_shortcode('custom_search', 'csp_search_form');
// 3⃣ **Aktualisiertes Suchfeld als Widget** // 3⃣ **Suchfeld als Widget**
class CSP_Search_Widget extends WP_Widget { class CSP_Search_Widget extends WP_Widget {
public function __construct() { public function __construct() {
parent::__construct( parent::__construct(
@ -161,19 +161,19 @@ function csp_render_settings_page() {
// 6⃣ **Einstellungen registrieren** // 6⃣ **Einstellungen registrieren**
function csp_register_settings() { function csp_register_settings() {
register_setting('csp_settings_group', 'csp_selected_menu'); register_setting('csp_settings_group', 'csp_shiftnav_position');
add_settings_section( add_settings_section(
'csp_main_section', 'csp_main_section',
'Menu Selection', 'ShiftNav Position',
'csp_section_callback', 'csp_section_callback',
'custom-search-settings' 'custom-search-settings'
); );
add_settings_field( add_settings_field(
'csp_menu_select', 'csp_shiftnav_position_select',
'Select Menu', 'Select ShiftNav Position',
'csp_menu_select_callback', 'csp_shiftnav_position_select_callback',
'custom-search-settings', 'custom-search-settings',
'csp_main_section' 'csp_main_section'
); );
@ -182,31 +182,48 @@ add_action('admin_init', 'csp_register_settings');
// 7⃣ **Callback für die Sektion** // 7⃣ **Callback für die Sektion**
function csp_section_callback() { 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** // 8⃣ **Callback für das Auswahlfeld**
function csp_menu_select_callback() { function csp_shiftnav_position_select_callback() {
$menus = wp_get_nav_menus(); $positions = array(
$selected_menu = get_option('csp_selected_menu'); '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 '<select name="csp_shiftnav_position">';
echo '<option value="">Select a menu</option>'; foreach ($positions as $value => $label) {
foreach ($menus as $menu) { echo '<option value="' . esc_attr($value) . '" ' . selected($selected_position, $value, false) . '>' . esc_html($label) . '</option>';
echo '<option value="' . $menu->term_id . '" ' . selected($selected_menu, $menu->term_id, false) . '>' . esc_html($menu->name) . '</option>';
} }
echo '</select>'; echo '</select>';
} }
// 9⃣ **Suchfeld zum ausgewählten Menü hinzufügen** // 9⃣ **Funktion zum Einfügen des Suchformulars in ShiftNav**
function csp_add_search_to_menus($items, $args) { function csp_insert_search_form_in_shiftnav($nav_menu, $args) {
$selected_menu = get_option('csp_selected_menu'); // Überprüfen, ob es sich um das ShiftNav-Menü handelt
if ($selected_menu && $args->menu->term_id == $selected_menu) { if ($args->theme_location !== 'shiftnav') {
$items .= '<li class="menu-item search-menu-item">' . csp_search_form() . '</li>'; 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** // 🔟 **Aktualisierte Suchfunktion mit Suchtyp-Unterscheidung**
function csp_custom_search_results($query) { function csp_custom_search_results($query) {