Compare commits

..

No commits in common. "main" and "1.0" have entirely different histories.
main ... 1.0

2 changed files with 141 additions and 879 deletions

View File

@ -1,53 +1,2 @@
WP Multi Kategorie ist ein Plugin für WordPress, mit dem du bestimmte Kategorien aus verschiedenen Bereichen deiner Website ausblenden kannst. Du kannst auswählen, welche Kategorien auf der Startseite, in Feeds, in Archiven und in Suchergebnissen ausgeblendet werden sollen.
# WP-Multi-Kategorie
**Funktionen:**
- Kategorien auf der Startseite ausblenden
- Kategorien in Feeds ausblenden
- Kategorien in Archiven ausblenden
- Kategorien in Suchergebnissen ausblenden
- Benutzerfreundliches Admin-Interface
## Installation
1. Lade das Plugin herunter und entpacke es.
2. Lade den Ordner in das Verzeichnis `wp-content/plugins` deiner WordPress-Installation hoch.
3. Gehe zu **Plugins > Installierte Plugins** in deinem WordPress-Adminbereich und aktiviere das Plugin "WP Multi Kategorie".
4. Das Plugin benötigt das Plugin **WP Multi Toolkit**. Stelle sicher, dass es installiert und aktiv ist, da sonst dieses Plugin nicht funktioniert.
## Verwendung
Nach der Installation findest du das Plugin unter **Beiträge > Kategorie-Filter**. Dort kannst du die Kategorien auswählen, die du in den verschiedenen Bereichen deiner Website ausblenden möchtest (Startseite, Feeds, Archive, Suche).
## Abhängigkeiten
Dieses Plugin erfordert das Plugin **WP Multi Toolkit**, um korrekt zu funktionieren.
Falls **WP Multi Toolkit** nicht installiert oder aktiviert ist, wirst du eine Fehlermeldung im Adminbereich sehen, die dich dazu auffordert, es herunterzuladen und zu installieren.
## Funktionen und Optionen
- **Startseite:** Blende ausgewählte Kategorien auf der Startseite aus.
- **Feeds:** Blende Kategorien in Feeds aus.
- **Archive:** Blende Kategorien in Archivseiten aus.
- **Suche:** Blende Kategorien in den Suchergebnissen aus.
## Screenshots
1. **Einstellungen des Plugins**
Screenshot von der Admin-Seite zur Auswahl der auszublendenden Kategorien.
2. **Fehlermeldung bei fehlender Abhängigkeit**
Screenshot der Admin-Fehlermeldung, wenn **WP Multi Toolkit** nicht aktiv ist.
## Deinstallation
Um das Plugin zu deinstallieren, gehe zu **Plugins > Installierte Plugins**, deaktiviere "WP Multi Kategorie" und lösche das Plugin anschließend.
## Lizenz
Dieses Plugin ist unter der [GPLv2 oder später](https://www.gnu.org/licenses/gpl-2.0.html) lizenziert.
## Unterstützen
Für Unterstützung oder bei Fragen besuche die [Support-Seite](https://git.viper.ipv64.net/M_Viper/WP-Multi-Kategorie) oder kontaktiere den Autor über die [Webseite von M_Viper](https://m-viper.de).

View File

@ -1,827 +1,140 @@
<?php
/*
* Plugin Name: WP Multi Kategorie
* Plugin URI: https://git.viper.ipv64.net/M_Viper/WP-Multi-Kategorie
* Description: Blende einzelne Kategorien aus verschiedenen Bereichen deiner Website (Startseite, Feeds, Archive, Suche) aus. Teil der WP Multi-Reihe.
* Version: 1.3
* Author: M_Viper
* Author URI: https://m-viper.de
* Requires at least: 6.7.2
* Tested up to: 6.7.2
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
defined('ABSPATH') or die('Kein direkter Zugriff erlaubt.');
/*
* Passwortschutz für Kategorie
*/
class Password_Protected_Category {
private $option_name = 'ppc_settings';
public function __construct() {
// Admin-Menü hinzufügen
add_action('admin_menu', [$this, 'add_admin_menu']);
// Admin-Skripte und -Styles laden
add_action('admin_enqueue_scripts', [$this, 'enqueue_admin_scripts']);
// Frontend-Skripte und -Styles laden
add_action('wp_enqueue_scripts', [$this, 'enqueue_frontend_scripts']);
// Einstellungen speichern
add_action('admin_init', [$this, 'register_settings']);
// Kategoriezugriff prüfen
add_action('template_redirect', [$this, 'restrict_category_access']);
// Suche manipulieren
add_filter('pre_get_posts', [$this, 'exclude_protected_category_from_search']);
// AJAX-Handler für Nachricht
add_action('wp_ajax_ppc_send_request', [$this, 'handle_access_request']);
add_action('wp_ajax_nopriv_ppc_send_request', [$this, 'handle_access_request']);
add_action('wp_ajax_ppc_check_password', [$this, 'check_password']);
add_action('wp_ajax_nopriv_ppc_check_password', [$this, 'check_password']);
}
// Admin-Menü als Submenü unter Beiträge
public function add_admin_menu() {
add_submenu_page(
'edit.php',
'Password Protected Category',
'Protected Categories',
'manage_options',
'password-protected-category',
[$this, 'settings_page']
);
}
// Einstellungen registrieren
public function register_settings() {
register_setting('ppc_settings_group', $this->option_name, [
'sanitize_callback' => [$this, 'sanitize_settings']
]);
}
// Einstellungen validieren
public function sanitize_settings($input) {
$new_input = [];
$new_input['protected_categories'] = [];
$new_input['admin_email'] = isset($input['admin_email']) ? sanitize_email($input['admin_email']) : get_option('admin_email');
if (isset($input['protected_categories'])) {
foreach ($input['protected_categories'] as $cat_id => $settings) {
$cat_id = absint($cat_id);
if ($cat_id && isset($settings['enabled']) && $settings['enabled'] === '1') {
$new_input['protected_categories'][$cat_id] = [
'enabled' => '1',
'password' => isset($settings['password']) ? sanitize_text_field($settings['password']) : '',
'popup_image' => isset($settings['popup_image']) ? esc_url_raw($settings['popup_image']) : ''
];
error_log('Sanitized Password for Cat ' . $cat_id . ': ' . $new_input['protected_categories'][$cat_id]['password']); // Debugging
}
}
}
return $new_input;
}
// Admin-Skripte laden
public function enqueue_admin_scripts($hook) {
if ($hook !== 'posts_page_password-protected-category') {
return;
}
wp_enqueue_media();
// Inline CSS für Admin-Grid
wp_add_inline_style('admin-menu', '
.ppc-categories-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}
.ppc-category-card {
background: #fff;
padding: 15px;
border: 1px solid #ddd;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.ppc-category-card label {
display: block;
margin-bottom: 10px;
font-weight: bold;
}
.ppc-category-card input[type="text"],
.ppc-category-card button {
width: 100%;
margin-bottom: 10px;
}
.ppc-category-card button {
margin-top: 5px;
}
@media (max-width: 960px) {
.ppc-categories-grid {
grid-template-columns: repeat(2, 1fr);
}
}
@media (max-width: 600px) {
.ppc-categories-grid {
grid-template-columns: 1fr;
}
}
');
}
// Frontend-Skripte und -Styles laden
public function enqueue_frontend_scripts() {
$options = get_option($this->option_name);
$protected_categories = isset($options['protected_categories']) ? array_keys($options['protected_categories']) : [];
if (!empty($protected_categories) && (is_category($protected_categories) || (is_single() && has_category($protected_categories)))) {
wp_enqueue_script('jquery-cookie', 'https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js', ['jquery'], '1.4.1', true);
wp_localize_script('jquery', 'ppcSettings', [
'ajax_url' => admin_url('admin-ajax.php'),
'category_id' => $this->get_current_protected_category(),
'popup_image' => $this->get_popup_image(),
'nonce' => wp_create_nonce('ppc_nonce'),
'home_url' => home_url()
]);
}
}
// Aktuelle geschützte Kategorie ermitteln
private function get_current_protected_category() {
$options = get_option($this->option_name);
$protected_categories = isset($options['protected_categories']) ? array_keys($options['protected_categories']) : [];
if (is_category()) {
$current_cat = get_queried_object_id();
if (in_array($current_cat, $protected_categories)) {
return $current_cat;
}
} elseif (is_single()) {
$categories = wp_get_post_categories(get_the_ID());
foreach ($categories as $cat_id) {
if (in_array($cat_id, $protected_categories)) {
return $cat_id;
}
}
}
return 0;
}
// Popup-Bild für aktuelle Kategorie holen
private function get_popup_image() {
$options = get_option($this->option_name);
$cat_id = $this->get_current_protected_category();
return isset($options['protected_categories'][$cat_id]['popup_image']) ? $options['protected_categories'][$cat_id]['popup_image'] : '';
}
// Kategoriezugriff einschränken
public function restrict_category_access() {
$options = get_option($this->option_name);
$protected_categories = isset($options['protected_categories']) ? array_keys($options['protected_categories']) : [];
if (!empty($protected_categories) && (is_category($protected_categories) || (is_single() && has_category($protected_categories)))) {
$current_cat = $this->get_current_protected_category();
if ($current_cat && (!isset($_COOKIE['ppc_access_granted_' . $current_cat]) || $_COOKIE['ppc_access_granted_' . $current_cat] !== '1')) {
if (is_single()) {
wp_redirect(home_url());
exit;
}
}
}
}
// Geschützte Kategorien aus Suche ausschließen
public function exclude_protected_category_from_search($query) {
if (!is_admin() && $query->is_main_query() && $query->is_search()) {
$options = get_option($this->option_name);
$protected_categories = isset($options['protected_categories']) ? array_keys($options['protected_categories']) : [];
if (!empty($protected_categories)) {
$query->set('category__not_in', $protected_categories);
}
}
return $query;
}
// Einstellungsseite rendern
public function settings_page() {
$options = get_option($this->option_name);
$categories = get_categories(['hide_empty' => false]);
?>
<div class="wrap">
<h1>Password Protected Categories</h1>
<form method="post" action="options.php">
<?php settings_fields('ppc_settings_group'); ?>
<table class="form-table">
<tr>
<th>
<label for="ppc_admin_email">Admin-E-Mail für Anfragen</label>
</th>
<td>
<input type="email" id="ppc_admin_email" name="<?php echo $this->option_name; ?>[admin_email]" value="<?php echo isset($options['admin_email']) ? esc_attr($options['admin_email']) : esc_attr(get_option('admin_email')); ?>" class="regular-text"/>
<p class="description">E-Mail-Adresse, an die Zugangsanfragen gesendet werden.</p>
</td>
</tr>
</table>
<h2>Geschützte Kategorien</h2>
<div class="ppc-categories-grid">
<?php foreach ($categories as $category) : ?>
<div class="ppc-category-card">
<label>
<input type="checkbox" name="<?php echo $this->option_name; ?>[protected_categories][<?php echo $category->term_id; ?>][enabled]" value="1" <?php checked(isset($options['protected_categories'][$category->term_id]['enabled']) && $options['protected_categories'][$category->term_id]['enabled'] === '1'); ?>>
<?php echo esc_html($category->name); ?>
</label>
<p>
<label>Passwort</label>
<input type="text" name="<?php echo $this->option_name; ?>[protected_categories][<?php echo $category->term_id; ?>][password]" value="<?php echo isset($options['protected_categories'][$category->term_id]['password']) ? esc_attr($options['protected_categories'][$category->term_id]['password']) : ''; ?>" class="regular-text"/>
</p>
<p>
<label>Popup-Bild-URL</label>
<input type="text" name="<?php echo $this->option_name; ?>[protected_categories][<?php echo $category->term_id; ?>][popup_image]" value="<?php echo isset($options['protected_categories'][$category->term_id]['popup_image']) ? esc_attr($options['protected_categories'][$category->term_id]['popup_image']) : ''; ?>" class="regular-text ppc-popup-image"/>
<button type="button" class="button ppc-upload-image">Bild auswählen</button>
</p>
</div>
<?php endforeach; ?>
</div>
<?php submit_button(); ?>
</form>
</div>
<?php
}
// Zugangsanfrage verarbeiten
public function handle_access_request() {
check_ajax_referer('ppc_nonce', 'nonce');
$options = get_option($this->option_name);
$name = isset($_POST['name']) ? sanitize_text_field($_POST['name']) : '';
$email = isset($_POST['email']) ? sanitize_email($_POST['email']) : '';
$message = isset($_POST['message']) ? sanitize_textarea_field($_POST['message']) : '';
$category_id = isset($_POST['category_id']) ? absint($_POST['category_id']) : 0;
if (empty($name) || empty($email) || empty($message)) {
wp_send_json_error(['message' => 'Bitte füllen Sie alle Felder aus.']);
}
$admin_email = !empty($options['admin_email']) ? $options['admin_email'] : get_option('admin_email');
$category = get_term($category_id, 'category');
$category_name = $category ? $category->name : 'Unbekannte Kategorie';
$subject = 'Zugangsanfrage für geschützte Kategorie: ' . $category_name;
$body = "Name: $name\n";
$body .= "E-Mail: $email\n";
$body .= "Kategorie: $category_name\n";
$body .= "Nachricht:\n$message";
$headers = ['From: ' . $name . ' <' . $email . '>'];
$sent = wp_mail($admin_email, $subject, $body, $headers);
if ($sent) {
wp_send_json_success(['message' => 'Ihre Anfrage wurde gesendet.']);
} else {
wp_send_json_error(['message' => 'Fehler beim Senden der Anfrage.']);
}
}
public function check_password() {
check_ajax_referer('ppc_nonce', 'nonce');
$options = get_option('ppc_settings');
$input_password = isset($_POST['password']) ? sanitize_text_field($_POST['password']) : '';
$category_id = isset($_POST['category_id']) ? absint($_POST['category_id']) : 0;
if ($category_id && isset($options['protected_categories'][$category_id])) {
$stored_password = $options['protected_categories'][$category_id]['password'];
error_log('Input Password: ' . $input_password);
error_log('Stored Password: ' . $stored_password);
if ($input_password === $stored_password) {
// Cookie setzen
setcookie('ppc_access_granted_' . $category_id, '1', time() + 2 * 3600, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true);
wp_send_json_success(['message' => 'Passwort korrekt']);
} else {
wp_send_json_error(['message' => 'Falsches Passwort']);
}
} else {
wp_send_json_error(['message' => 'Ungültige Kategorie oder Passwort nicht gesetzt']);
}
wp_die();
}
}
// Inline CSS für Frontend
add_action('wp_head', function () {
$options = get_option('ppc_settings');
$protected_categories = isset($options['protected_categories']) ? array_keys($options['protected_categories']) : [];
if (!empty($protected_categories) && (is_category($protected_categories) || (is_single() && has_category($protected_categories)))) {
?>
<style id="ppc-frontend-style">
/* Unscharfer Hintergrund */
body.ppc-blurred > *:not(.ppc-overlay) {
filter: blur(10px);
pointer-events: none;
}
.ppc-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.7);
z-index: 9999;
display: none;
overflow: auto;
}
.ppc-popup-container {
min-height: 100%;
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
box-sizing: border-box;
}
.ppc-popup {
background: #fff;
padding: 2rem;
border-radius: 12px;
max-width: 400px;
width: 100%;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
text-align: center;
animation: ppcFadeIn 0.3s ease-out;
}
@keyframes ppcFadeIn {
from {
opacity: 0;
transform: scale(0.9);
}
to {
opacity: 1;
transform: scale(1);
}
}
.ppc-popup img {
max-width: 100%;
height: auto;
margin-bottom: 1rem;
border-radius: 8px;
}
.ppc-popup input[type="password"],
.ppc-popup input[type="text"],
.ppc-popup input[type="email"],
.ppc-popup textarea {
width: 100%;
padding: 0.75rem;
margin-bottom: 1rem;
border: 1px solid #ddd;
border-radius: 6px;
font-size: 1rem;
transition: border-color 0.3s;
}
.ppc-popup input:focus,
.ppc-popup textarea:focus {
border-color: #007bff;
outline: none;
}
.ppc-popup textarea {
height: 100px;
resize: vertical;
}
.ppc-popup button {
background: #007bff;
color: #fff;
padding: 0.75rem 1.5rem;
border: none;
border-radius: 6px;
font-size: 1rem;
cursor: pointer;
transition: background 0.3s;
margin: 0.5rem;
}
.ppc-popup button:hover {
background: #0056b3;
}
.ppc-popup a.ppc-request-link {
display: block;
margin-top: 1rem;
color: #007bff;
text-decoration: none;
font-size: 0.9rem;
}
.ppc-popup a.ppc-request-link:hover {
text-decoration: underline;
}
.ppc-error,
.ppc-success {
margin-top: 0.5rem;
font-size: 0.9rem;
}
.ppc-error {
color: #dc3545;
}
.ppc-success {
color: #28a745;
}
.ppc-request-form {
display: none;
}
</style>
<?php
}
});
// Inline JS für Frontend
add_action('wp_footer', function () {
$options = get_option('ppc_settings');
$protected_categories = isset($options['protected_categories']) ? array_keys($options['protected_categories']) : [];
if (!empty($protected_categories) && (is_category($protected_categories) || (is_single() && has_category($protected_categories)))) {
?>
<script id="ppc-frontend-script">
jQuery(document).ready(function ($) {
// Hintergrund unscharf machen
$('body').addClass('ppc-blurred');
// Popup HTML erstellen
const popupHtml = `
<div class="ppc-overlay">
<div class="ppc-popup-container">
<div class="ppc-popup">
${ppcSettings.popup_image ? `<img src="${ppcSettings.popup_image}" alt="Popup Image">` : ''}
<div class="ppc-password-form">
<input type="password" id="ppc-password" placeholder="Passwort eingeben">
<button id="ppc-submit">Login</button>
<a href="#" class="ppc-request-link">Kein Passwort? Zugang anfragen</a>
<div class="ppc-error" style="display: none;"></div>
</div>
<div class="ppc-request-form">
<input type="text" id="ppc-name" placeholder="Ihr Name">
<input type="email" id="ppc-email" placeholder="Ihre E-Mail">
<textarea id="ppc-message" placeholder="Ihre Nachricht"></textarea>
<button id="ppc-request-submit">Anfrage senden</button>
<button id="ppc-back">Zurück</button>
<div class="ppc-error" style="display: none;"></div>
<div class="ppc-success" style="display: none;"></div>
</div>
</div>
</div>
</div>
`;
$('body').append(popupHtml);
// Popup anzeigen
$('.ppc-overlay').fadeIn();
// Klick außerhalb des Popups
$('.ppc-overlay').on('click', function (e) {
if ($(e.target).hasClass('ppc-overlay') || $(e.target).hasClass('ppc-popup-container')) {
$('body').removeClass('ppc-blurred');
$('.ppc-overlay').fadeOut();
window.location.href = ppcSettings.home_url;
}
});
// Passwort prüfen
$('#ppc-submit').on('click', function () {
const password = $('#ppc-password').val();
console.log('Sending password:', password);
console.log('Category ID:', ppcSettings.category_id);
$.ajax({
url: ppcSettings.ajax_url,
type: 'POST',
data: {
action: 'ppc_check_password',
password: password,
category_id: ppcSettings.category_id,
nonce: ppcSettings.nonce
},
success: function (response) {
console.log('AJAX response:', response);
if (response.success) {
$('body').removeClass('ppc-blurred');
$('.ppc-overlay').fadeOut();
} else {
$('.ppc-password-form .ppc-error').text(response.data.message).show();
}
},
error: function (xhr, status, error) {
console.log('AJAX error:', error);
}
});
});
// Enter-Taste unterstützen
$('#ppc-password').on('keypress', function (e) {
if (e.which === 13) {
$('#ppc-submit').click();
}
});
// Zugangsanfrage anzeigen
$('.ppc-request-link').on('click', function (e) {
e.preventDefault();
$('.ppc-password-form').hide();
$('.ppc-request-form').show();
});
// Zurück zum Passwortformular
$('#ppc-back').on('click', function () {
$('.ppc-request-form').hide();
$('.ppc-password-form').show();
$('.ppc-error, .ppc-success').hide();
});
// Zugangsanfrage senden
$('#ppc-request-submit').on('click', function () {
const name = $('#ppc-name').val();
const email = $('#ppc-email').val();
const message = $('#ppc-message').val();
$.ajax({
url: ppcSettings.ajax_url,
type: 'POST',
data: {
action: 'ppc_send_request',
name: name,
email: email,
message: message,
category_id: ppcSettings.category_id,
nonce: ppcSettings.nonce
},
success: function (response) {
if (response.success) {
$('.ppc-request-form .ppc-success').text(response.data.message).show();
$('.ppc-request-form .ppc-error').hide();
$('#ppc-name, #ppc-email, #ppc-message').val('');
} else {
$('.ppc-request-form .ppc-error').text(response.data.message).show();
$('.ppc-request-form .ppc-success').hide();
}
}
});
});
});
</script>
<?php
}
});
// Inline JS für Admin
add_action('admin_footer', function () {
if (get_current_screen()->id !== 'posts_page_password-protected-category') {
return;
}
?>
<script id="ppc-admin-script">
jQuery(document).ready(function ($) {
$('.ppc-upload-image').click(function (e) {
e.preventDefault();
const button = $(this);
const input = button.prev('.ppc-popup-image');
const frame = wp.media({
title: 'Bild auswählen',
button: {
text: 'Bild verwenden'
},
multiple: false
});
frame.on('select', function () {
const attachment = frame.state().get('selection').first().toJSON();
input.val(attachment.url);
});
frame.open();
});
});
</script>
<?php
});
// Plugin initialisieren
new Password_Protected_Category();
/*
* WP Multi Toolkit Prüfung
*/
// Funktion zur Überprüfung des WP Multi Toolkit Plugins
function wp_multi_kategorie_check_dependency() {
if (!function_exists('is_plugin_active')) {
include_once(ABSPATH . 'wp-admin/includes/plugin.php');
}
// Prüft, ob WP Multi Toolkit installiert und aktiv ist
if (!is_plugin_active('wp-multi-toolkit/wp-multi-toolkit.php')) {
add_action('admin_notices', 'wp_multi_kategorie_dependency_notice');
return false;
}
return true;
}
// Fehlermeldung für Admin-Bereich mit Download-Button
function wp_multi_kategorie_dependency_notice() {
?>
<div class="notice notice-error">
<p>
<?php _e('Das Plugin "WP Multi Kategorie" benötigt "WP Multi Toolkit", um zu funktionieren. Bitte installieren und aktivieren Sie "WP Multi Toolkit".', 'wp-multi-kategorie'); ?>
<a href="https://git.viper.ipv64.net/M_Viper/wp-multi-toolkit/releases" target="_blank" class="button button-primary" style="margin-left: 10px;">
<?php _e('WP Multi Toolkit herunterladen', 'wp-multi-kategorie'); ?>
</a>
</p>
</div>
<?php
}
// Plugin nur initialisieren, wenn Abhängigkeit erfüllt ist
if (wp_multi_kategorie_check_dependency()) {
// Hier würde der restliche Plugin-Code folgen
} else {
// Optional: Plugin komplett deaktivieren, wenn Abhängigkeit fehlt
add_action('admin_init', function() {
deactivate_plugins(plugin_basename(__FILE__));
});
}
// Admin-Menü und Filter
add_action('admin_menu', 'wpmkategorie_admin_menu');
add_filter('pre_get_posts', 'wpmkategorie_exclude_categories');
// Admin-Menü unter "Beiträge"
function wpmkategorie_admin_menu() {
add_submenu_page(
'edit.php', // Elternmenü: "Beiträge"
'WP Multi Kategorie', // Seitentitel
'Kategorie-Filter', // Menüpunkt-Name
'manage_options', // Berechtigung
'wp-multi-kategorie', // Slug
'wpmkategorie_options_page' // Callback-Funktion
);
}
// CSS für modernes Design
// CSS nur für die Seite des Plugins
function wpmkategorie_enqueue_styles() {
// Überprüfen, ob wir auf der Seite des Plugins sind
if (isset($_GET['page']) && $_GET['page'] === 'wp-multi-kategorie') {
echo '<style>
.wrap {
background-color: #f9f9f9;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
h1 {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
font-size: 1.8rem;
color: #333;
margin-bottom: 20px;
}
.button {
background-color: #0073aa;
color: white;
border: none;
padding: 8px 15px;
border-radius: 5px;
cursor: pointer;
font-size: 14px;
}
.button-primary {
background-color: #00a0d2;
}
.button:hover {
background-color: #006e8a;
}
.widefat th, .widefat td {
padding: 12px;
text-align: center;
font-size: 14px;
border-bottom: 1px solid #ddd;
}
.widefat tr.alternate {
background-color: #f1f1f1;
}
.submit {
margin-top: 20px;
}
.notice-success, .notice-error {
font-size: 16px;
background-color: #e7f9e7;
border: 1px solid #6ecf6e;
padding: 15px;
margin-bottom: 20px;
border-radius: 5px;
}
.notice-error {
background-color: #fbe9e9;
border-color: #f57c7c;
}
.wp-multi-kategorie-form input[type="checkbox"] {
width: 18px;
height: 18px;
margin: 0;
}
/* Banner mit Blauem Hintergrund und Logo zentriert */
.wp-multi-kategorie-banner {
background-color: #0073aa; /* Blaues Banner */
color: white;
height: 150px;
display: flex;
justify-content: center;
align-items: center;
margin-bottom: 20px;
}
.wp-multi-kategorie-banner img {
max-height: 100px;
max-width: 100%;
object-fit: contain;
}
</style>';
}
}
add_action('admin_head', 'wpmkategorie_enqueue_styles');
// Optionsseite mit modernem Design
function wpmkategorie_options_page() {
if (isset($_POST['wpmkategorie'])) {
check_admin_referer('wpmkategorie_form');
$message = wpmkategorie_process();
}
$options = wpmkategorie_get_options();
?>
<div class="wrap">
<div class="wp-multi-kategorie-banner">
<img src="https://m-viper.de/img/logo.png" alt="Logo"> <!-- Logo im Banner zentriert -->
</div>
<h1>WP Multi Kategorie</h1>
<?php if (isset($message)) { echo $message; } ?>
<p>Wähle Kategorien aus, die du aus bestimmten Bereichen deiner Website ausblenden möchtest.</p>
<form action="" method="post" class="wp-multi-kategorie-form">
<?php wp_nonce_field('wpmkategorie_form'); ?>
<table class="widefat fixed">
<thead>
<tr>
<th>Kategorie</th>
<th>Startseite</th>
<th>Feeds</th>
<th>Archive</th>
<th>Suche</th>
</tr>
</thead>
<tbody>
<?php
$categories = get_categories(array('hide_empty' => 0, 'order' => 'ASC'));
$alt = 0;
foreach ($categories as $cat) :
$alt_class = ($alt++ % 2) ? ' class="alternate"' : '';
?>
<tr<?php echo $alt_class; ?>>
<td><?php echo esc_html($cat->cat_name); ?></td>
<td><input type="checkbox" name="exclude_main[]" value="<?php echo $cat->cat_ID; ?>" <?php checked(in_array($cat->cat_ID, $options['exclude_main'])); ?> /></td>
<td><input type="checkbox" name="exclude_feed[]" value="<?php echo $cat->cat_ID; ?>" <?php checked(in_array($cat->cat_ID, $options['exclude_feed'])); ?> /></td>
<td><input type="checkbox" name="exclude_archives[]" value="<?php echo $cat->cat_ID; ?>" <?php checked(in_array($cat->cat_ID, $options['exclude_archives'])); ?> /></td>
<td><input type="checkbox" name="exclude_search[]" value="<?php echo $cat->cat_ID; ?>" <?php checked(in_array($cat->cat_ID, $options['exclude_search'])); ?> /></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<p class="submit">
<input type="submit" name="wpmkategorie" class="button button-primary" value="Einstellungen speichern" />
</p>
</form>
</div>
<?php
}
// Verarbeitung der Formulardaten
function wpmkategorie_process() {
$options = array(
'exclude_main' => isset($_POST['exclude_main']) ? array_map('intval', $_POST['exclude_main']) : array(),
'exclude_feed' => isset($_POST['exclude_feed']) ? array_map('intval', $_POST['exclude_feed']) : array(),
'exclude_archives' => isset($_POST['exclude_archives']) ? array_map('intval', $_POST['exclude_archives']) : array(),
'exclude_search' => isset($_POST['exclude_search']) ? array_map('intval', $_POST['exclude_search']) : array()
);
update_option('wpmkategorie_exclusions', $options);
return '<div class="notice notice-success is-dismissible"><p>Die Einstellungen wurden erfolgreich gespeichert.</p></div>';
}
// Optionen abrufen
function wpmkategorie_get_options() {
$defaults = array(
'exclude_main' => array(),
'exclude_feed' => array(),
'exclude_archives' => array(),
'exclude_search' => array()
);
$options = get_option('wpmkategorie_exclusions', $defaults);
if (!is_array($options)) {
$options = $defaults;
update_option('wpmkategorie_exclusions', $options);
}
return $options;
}
// Kategorien ausblenden
function wpmkategorie_exclude_categories($query) {
if (!is_admin() && $query->is_main_query()) {
$options = wpmkategorie_get_options();
if ($query->is_home() && !empty($options['exclude_main'])) {
$query->set('category__not_in', $options['exclude_main']);
}
if ($query->is_feed() && !empty($options['exclude_feed'])) {
$query->set('category__not_in', $options['exclude_feed']);
}
if ($query->is_archive() && !empty($options['exclude_archives'])) {
$query->set('category__not_in', $options['exclude_archives']);
}
if ($query->is_search() && !empty($options['exclude_search'])) {
$query->set('category__not_in', $options['exclude_search']);
}
}
return $query;
}
// Deinstallation
function wpmkategorie_uninstall() {
delete_option('wpmkategorie_exclusions');
}
register_uninstall_hook(__FILE__, 'wpmkategorie_uninstall');
<?php
/*
* Plugin Name: WP Multi Kategorie
* Plugin URI: https://git.viper.ipv64.net/M_Viper/wp-multi-kategorie
* Description: Blende einzelne Kategorien aus verschiedenen Bereichen deiner Website (Startseite, Feeds, Archive, Suche) aus. Teil der WP Multi-Reihe.
* Version: 1.0.0
* Author: M_Viper
* Author URI: https://m-viper.de
* Requires at least: 6.7.2
* Tested up to: 6.7.2
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
defined('ABSPATH') or die('Kein direkter Zugriff erlaubt.');
// Admin-Menü und Filter
add_action('admin_menu', 'wpmkategorie_admin_menu');
add_filter('pre_get_posts', 'wpmkategorie_exclude_categories');
// Admin-Menü unter "Beiträge"
function wpmkategorie_admin_menu() {
add_submenu_page(
'edit.php', // Elternmenü: "Beiträge"
'WP Multi Kategorie', // Seitentitel
'Kategorie-Filter', // Menüpunkt-Name
'manage_options', // Berechtigung
'wp-multi-kategorie', // Slug
'wpmkategorie_options_page' // Callback-Funktion
);
}
// Optionsseite
function wpmkategorie_options_page() {
if (isset($_POST['wpmkategorie'])) {
check_admin_referer('wpmkategorie_form');
$message = wpmkategorie_process();
}
$options = wpmkategorie_get_options();
?>
<div class="wrap">
<h1>WP Multi Kategorie</h1>
<?php if (isset($message)) { echo $message; } ?>
<p>Wähle Kategorien aus, die du aus bestimmten Bereichen deiner Website ausblenden möchtest.</p>
<form action="" method="post">
<?php wp_nonce_field('wpmkategorie_form'); ?>
<table class="widefat fixed">
<thead>
<tr>
<th>Kategorie</th>
<th>Startseite ausblenden?</th>
<th>Feeds ausblenden?</th>
<th>Archive ausblenden?</th>
<th>Suche ausblenden?</th>
</tr>
</thead>
<tbody>
<?php
$categories = get_categories(array('hide_empty' => 0, 'order' => 'ASC'));
$alt = 0;
foreach ($categories as $cat) :
$alt_class = ($alt++ % 2) ? ' class="alternate"' : '';
?>
<tr<?php echo $alt_class; ?>>
<td><?php echo esc_html($cat->cat_name); ?></td>
<td><input type="checkbox" name="exclude_main[]" value="<?php echo $cat->cat_ID; ?>" <?php checked(in_array($cat->cat_ID, $options['exclude_main'])); ?> /></td>
<td><input type="checkbox" name="exclude_feed[]" value="<?php echo $cat->cat_ID; ?>" <?php checked(in_array($cat->cat_ID, $options['exclude_feed'])); ?> /></td>
<td><input type="checkbox" name="exclude_archives[]" value="<?php echo $cat->cat_ID; ?>" <?php checked(in_array($cat->cat_ID, $options['exclude_archives'])); ?> /></td>
<td><input type="checkbox" name="exclude_search[]" value="<?php echo $cat->cat_ID; ?>" <?php checked(in_array($cat->cat_ID, $options['exclude_search'])); ?> /></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<p class="submit">
<input type="submit" name="wpmkategorie" class="button button-primary" value="Aktualisieren" />
</p>
</form>
</div>
<?php
}
// Verarbeitung der Formulardaten
function wpmkategorie_process() {
$options = array(
'exclude_main' => isset($_POST['exclude_main']) ? array_map('intval', $_POST['exclude_main']) : array(),
'exclude_feed' => isset($_POST['exclude_feed']) ? array_map('intval', $_POST['exclude_feed']) : array(),
'exclude_archives' => isset($_POST['exclude_archives']) ? array_map('intval', $_POST['exclude_archives']) : array(),
'exclude_search' => isset($_POST['exclude_search']) ? array_map('intval', $_POST['exclude_search']) : array()
);
update_option('wpmkategorie_exclusions', $options);
return '<div class="notice notice-success is-dismissible"><p>Einstellungen erfolgreich aktualisiert.</p></div>';
}
// Optionen abrufen
function wpmkategorie_get_options() {
$defaults = array(
'exclude_main' => array(),
'exclude_feed' => array(),
'exclude_archives' => array(),
'exclude_search' => array()
);
$options = get_option('wpmkategorie_exclusions', $defaults);
if (!is_array($options)) {
$options = $defaults;
update_option('wpmkategorie_exclusions', $options);
}
return $options;
}
// Kategorien ausblenden
function wpmkategorie_exclude_categories($query) {
if (!is_admin() && $query->is_main_query()) {
$options = wpmkategorie_get_options();
if ($query->is_home() && !empty($options['exclude_main'])) {
$query->set('category__not_in', $options['exclude_main']);
}
if ($query->is_feed() && !empty($options['exclude_feed'])) {
$query->set('category__not_in', $options['exclude_feed']);
}
if ($query->is_archive() && !empty($options['exclude_archives'])) {
$query->set('category__not_in', $options['exclude_archives']);
}
if ($query->is_search() && !empty($options['exclude_search'])) {
$query->set('category__not_in', $options['exclude_search']);
}
}
return $query;
}
// Deinstallation
function wpmkategorie_uninstall() {
delete_option('wpmkategorie_exclusions');
}
register_uninstall_hook(__FILE__, 'wpmkategorie_uninstall');