Simple Download Monitor – Child Theme Template Override

Estimated reading: 11 minutes 242 views

Übersicht

Diese Lösung ermöglicht es, die Standard-Templates des Simple Download Monitor Plugins über das Child-Theme zu überschreiben, ohne die Plugin-Dateien direkt zu bearbeiten. Das System ist Update-sicher und funktioniert mit allen Fancy-Template-Varianten.

Funktionsweise

Das System prüft automatisch bei jedem Download-Shortcode, ob eine entsprechende Template-Datei im Child-Theme existiert:

  • Shortcode: [ sdm_download id="123" fancy="1"]
  • Sucht nach: child-theme/simple-download-monitor/sdm-fancy-1.php
  • Vorhanden: → Verwendet das Child-Template
  • Nicht vorhanden: → Verwendet das Standard-Plugin-Template

Installation

Schritt 1: Funktionen in functions.php einfügen

/**
 * Simple Download Monitor Child Theme Template Override
 * 
 * Verzeichnisstruktur:
 * child-theme/simple-download-monitor/sdm-fancy-1.php
 * child-theme/simple-download-monitor/sdm-fancy-5.php
 * etc.
 */

// Hook für Template Override
add_filter( 'sdm_download_shortcode_output', 'sdm_child_theme_template_override', 5, 2 );

/**
 * Template Override Funktion
 * Prüft ob ein Child-Template existiert und verwendet es anstelle des Standard-Templates
 */
function sdm_child_theme_template_override( $output, $atts ) {
    // Prüfen ob ein fancy-Parameter gesetzt ist
    if ( !isset( $atts['fancy'] ) || empty( $atts['fancy'] ) ) {
        return $output; // Kein fancy-Parameter, original Output zurückgeben
    }
    
    $fancy_id = sanitize_text_field( $atts['fancy'] );
    
    // Pfad zur Child-Theme Template-Datei
    $child_template_path = get_stylesheet_directory() . '/simple-download-monitor/sdm-fancy-' . $fancy_id . '.php';
    
    // Prüfen ob die Child-Theme Template-Datei existiert
    if ( file_exists( $child_template_path ) ) {
        
        // Download-ID holen
        $download_id = isset( $atts['id'] ) ? intval( $atts['id'] ) : 0;
        
        if ( empty( $download_id ) ) {
            return '<p style="color: red;">Fehler: Keine Download-ID angegeben.</p>';
        }
        
        // Template-Daten vorbereiten
        $template_data = sdm_prepare_template_data( $download_id, $atts );
        
        if ( empty( $template_data ) ) {
            return '<p style="color: red;">Fehler: Download nicht gefunden.</p>';
        }
        
        // Template-Variablen verfügbar machen
        extract( $template_data );
        $shortcode_atts = $atts; // Für Kompatibilität
        
        // Output Buffer starten
        ob_start();
        
        try {
            // Child-Theme Template einbinden
            include $child_template_path;
        } catch ( Exception $e ) {
            ob_end_clean();
            return '<p style="color: red;">Template-Fehler: ' . esc_html( $e->getMessage() ) . '</p>';
        }
        
        // Template-Output holen
        $custom_output = ob_get_clean();
        
        if ( empty( trim( $custom_output ) ) ) {
            return '<p style="color: orange;">Warnung: Child-Template ist leer.</p>';
        }
        
        // Standard-CSS einbinden falls benötigt
        if ( defined( 'WP_SIMPLE_DL_MONITOR_URL' ) && defined( 'WP_SIMPLE_DL_MONITOR_VERSION' ) ) {
            wp_enqueue_style( 'sdm-styles', WP_SIMPLE_DL_MONITOR_URL . '/css/sdm_wp_styles.css', array(), WP_SIMPLE_DL_MONITOR_VERSION );
        }
        
        return $custom_output;
    }
    
    // Kein Child-Template gefunden, Standard verwenden
    return $output;
}

/**
 * Template-Daten vorbereiten
 * Sammelt alle nötigen Daten für das Template
 */
function sdm_prepare_template_data( $download_id, $atts ) {
    
    // Download-Post holen
    $download_post = get_post( $download_id );
    if ( !$download_post || $download_post->post_type !== 'sdm_downloads' ) {
        return array();
    }
    
    // Basis-Daten sammeln
    $data = array(
        'download_id' => $download_id,
        'atts' => $atts,
        'post' => $download_post,
        'title' => get_the_title( $download_id ),
        'description' => get_post_meta( $download_id, 'sdm_description', true ),
        'file_url' => get_post_meta( $download_id, 'sdm_upload', true ),
        'file_size' => get_post_meta( $download_id, 'sdm_item_file_size', true ),
        'version' => get_post_meta( $download_id, 'sdm_item_version', true ),
        'thumbnail' => get_post_meta( $download_id, 'sdm_upload_thumbnail', true ),
        'download_url' => home_url( '/?sdm_process_download=1&download_id=' . $download_id )
    );
    
    // Download-Count berechnen
    $data['download_count'] = sdm_calculate_download_count( $download_id );
    
    // Button-Text ermitteln
    $data['button_text'] = sdm_get_button_text( $download_id, $atts );
    
    // Weitere Attribute
    $data['color'] = isset( $atts['color'] ) ? $atts['color'] : 'green';
    $data['new_window'] = isset( $atts['new_window'] ) && $atts['new_window'] == '1' ? 'target="_blank"' : '';
    $data['is_password_protected'] = !empty( $download_post->post_password );
    
    // Kategorien und Tags
    $data['categories'] = get_the_terms( $download_id, 'sdm_categories' ) ?: array();
    $data['tags'] = get_the_terms( $download_id, 'sdm_tags' ) ?: array();
    
    return $data;
}

/**
 * Download-Count berechnen
 */
function sdm_calculate_download_count( $download_id ) {
    if ( function_exists( 'sdm_get_download_count_for_post' ) ) {
        return sdm_get_download_count_for_post( $download_id );
    }
    
    // Fallback: Manuelle Berechnung
    global $wpdb;
    $table = $wpdb->prefix . 'sdm_downloads';
    $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $table WHERE post_id = %d", $download_id ) );
    $offset = get_post_meta( $download_id, 'sdm_count_offset', true );
    
    return intval( $count ) + intval( $offset );
}

/**
 * Button-Text ermitteln
 */
function sdm_get_button_text( $download_id, $atts ) {
    // Shortcode-Parameter hat Priorität
    if ( isset( $atts['button_text'] ) && !empty( $atts['button_text'] ) ) {
        return $atts['button_text'];
    }
    
    // Post-Meta prüfen
    $meta_text = get_post_meta( $download_id, 'sdm_download_button_text', true );
    if ( !empty( $meta_text ) ) {
        return $meta_text;
    }
    
    // Standard-Text
    return __( 'Download Now!', 'simple-download-monitor' );
}

/**
 * Download-Button HTML generieren
 */
function sdm_generate_download_button( $download_url, $button_text, $color = 'green', $new_window = '', $additional_classes = '' ) {
    $classes = 'sdm_download ' . sanitize_html_class( $color );
    if ( !empty( $additional_classes ) ) {
        $classes .= ' ' . sanitize_html_class( $additional_classes );
    }
    
    return sprintf(
        '<a href="%s" class="%s" %s>%s</a>',
        esc_url( $download_url ),
        esc_attr( $classes ),
        $new_window,
        esc_html( $button_text )
    );
}

Kopieren Sie den kompletten Code in die functions.php Ihres Child-Themes (ohne die <?php Tags).

Schritt 2: Verzeichnisstruktur erstellen

Erstellen Sie folgende Struktur in Ihrem Child-Theme:

child-theme/
├── functions.php (← Code hier einfügen)
├── style.css
└── simple-download-monitor/
    ├── sdm-fancy-1.php
    ├── sdm-fancy-5.php
    ├── sdm-fancy-10.php
    └── ... (beliebige weitere Templates)

Schritt 3: Template-Dateien erstellen

Erstellen Sie Template-Dateien entsprechend Ihrer benötigten Fancy-Nummern:

  • sdm-fancy-1.php für fancy="1"
  • sdm-fancy-5.php für fancy="5"
  • sdm-fancy-99.php für fancy="99"
  • etc.

Template-Struktur

✅ Korrekte Template-Datei

<?php
// Verfügbare Variablen nutzen
if ( empty( $download_id ) ) {
    echo '<p style="color: red;">Fehler: Keine Download-ID verfügbar.</p>';
    return;
}
?>

<div class="my-custom-template">
    <h3><?php echo esc_html( $title ); ?></h3>
    <p><?php echo wp_kses_post( $description ); ?></p>
    
    <?php 
    echo sdm_generate_download_button( 
        $download_url, 
        $button_text, 
        $color, 
        $new_window 
    );
    ?>
</div>

<style>
.my-custom-template {
    /* Ihre CSS-Styles */
}
</style>

❌ Falsche Template-Datei

<?php
// NIEMALS Funktionen redefinieren!
function sdm_generate_fancy1_display_output() { // ← FEHLER!
    // Das führt zu Fatal Errors
}
?>

Verfügbare Template-Variablen

In jeder Template-Datei stehen folgende Variablen zur Verfügung:

VariableBeschreibungBeispiel
$download_idID des Downloads123
$titleDownload-Titel"Meine Datei"
$descriptionBeschreibung"Eine tolle Datei..."
$file_urlOriginal-Datei-URL"https://site.com/file.pdf"
$download_urlVerarbeitete Download-URL"https://site.com/?sdm_process..."
$file_sizeDateigröße"2.5 MB"
$versionVersionsnummer"1.2.0"
$thumbnailThumbnail-URL"https://site.com/thumb.jpg"
$download_countAnzahl Downloads42
$button_textButton-Text"Jetzt herunterladen"
$colorButton-Farbe"blue"
$new_windowNeues Fenster'target="_blank"' oder ''
$is_password_protectedPasswort-Schutztrue oder false
$categoriesKategorien-Array[{name: "Software"}, ...]
$tagsTags-Array[{name: "Free"}, ...]
$attsAlle Shortcode-Attribute['id' => '123', 'fancy' => '1']

Hilfsfunktionen

Download-Button generieren

echo sdm_generate_download_button( 
    $download_url,           // Download-URL
    'Herunterladen',         // Button-Text
    'blue',                  // Farbe
    'target="_blank"',       // Neues Fenster
    'my-custom-btn-class'    // Zusätzliche CSS-Klasse
);

Sicherheitsfunktionen

// Text ausgeben
echo esc_html( $title );

// HTML ausgeben (erlaubt bestimmte Tags)
echo wp_kses_post( $description );

// URL ausgeben
echo esc_url( $download_url );

// CSS-Klasse ausgeben
echo esc_attr( $css_class );

Beispiel-Verwendung

Shortcode im Content

Fehler: Download nicht gefunden.

Resultat

  1. System prüft: child-theme/simple-download-monitor/sdm-fancy-5.php
  2. Datei existiert: → Verwendet das Child-Template
  3. Datei existiert nicht: → Verwendet Standard-Plugin-Template

Fehlerbehebung

Häufige Probleme

Problem: Fatal Error „Cannot redeclare function“ Lösung: Entfernen Sie alle function Definitionen aus Template-Dateien

Problem: Template wird nicht verwendet Lösung:

  • Prüfen Sie den Dateinamen: sdm-fancy-X.php
  • Prüfen Sie den Verzeichnispfad: simple-download-monitor/
  • Aktivieren Sie Debug-Logging in der functions.php

Problem: Variablen sind leer Lösung: Prüfen Sie, ob die Download-ID korrekt ist und der Download existiert

Debug-Modus

Aktivieren Sie Debug-Logging in der functions.php:

// Zeile in der functions.php aktivieren:
error_log( 'SDM Template Check: ' . $child_template_path );

Prüfen Sie dann das WordPress-Debug-Log für Template-Pfade.

Beispiel: sdm-fancy-5.php

<?php
/**
 * Custom Simple Download Monitor Template - Fancy 5
 * Speichern Sie diese Datei als: child-theme/simple-download-monitor/sdm-fancy-5.php
 * 
 * WICHTIG: Keine Funktions-Redefinitionen! Nur reines HTML/PHP Template
 * 
 * Verfügbare Variablen:
 * $download_id, $title, $description, $file_url, $file_size, $version,
 * $thumbnail, $download_url, $download_count, $button_text, $color,
 * $new_window, $is_password_protected, $categories, $tags, $atts
 */

// Sicherstellen, dass wir die nötigen Daten haben
if ( empty( $download_id ) ) {
    echo '<p style="color: red;">Fehler: Keine Download-ID verfügbar.</p>';
    return;
}
?>

<!-- CUSTOM FANCY 5 TEMPLATE - KEINE FUNKTIONS-REDEFINITION -->
<div class="sdm_download_item sdm_fancy5_download custom-child-fancy-5">
    
    <div class="sdm_fancy5_header">
        <div class="sdm_fancy5_icon">
            <?php if ( !empty( $thumbnail ) ): ?>
                <img src="<?php echo esc_url( $thumbnail ); ?>" alt="<?php echo esc_attr( $title ); ?>" class="sdm_fancy5_thumbnail" />
            <?php else: ?>
                <div class="sdm_fancy5_default_icon">📥</div>
            <?php endif; ?>
        </div>
        
        <div class="sdm_fancy5_title_section">
            <h4 class="sdm_fancy5_title"><?php echo esc_html( $title ); ?></h4>
            
            <div class="sdm_fancy5_quick_info">
                <?php if ( !empty( $file_size ) ): ?>
                    <span class="sdm_fancy5_size">📁 <?php echo esc_html( $file_size ); ?></span>
                <?php endif; ?>
                
                <span class="sdm_fancy5_downloads">📊 <?php echo esc_html( $download_count ); ?> Downloads</span>
                
                <?php if ( !empty( $version ) ): ?>
                    <span class="sdm_fancy5_version">🏷️ v<?php echo esc_html( $version ); ?></span>
                <?php endif; ?>
            </div>
        </div>
        
        <div class="sdm_fancy5_button_section">
            <?php 
            echo sdm_generate_download_button( 
                $download_url, 
                $button_text, 
                $color, 
                $new_window,
                'sdm-fancy5-btn'
            );
            ?>
        </div>
    </div>
    
    <?php if ( !empty( $description ) ): ?>
    <div class="sdm_fancy5_description">
        <?php echo wp_kses_post( $description ); ?>
    </div>
    <?php endif; ?>
    
    <?php if ( !empty( $categories ) ): ?>
    <div class="sdm_fancy5_categories">
        <?php foreach ( $categories as $category ): ?>
            <span class="sdm_fancy5_category_tag"><?php echo esc_html( $category->name ); ?></span>
        <?php endforeach; ?>
    </div>
    <?php endif; ?>
    
    <?php if ( $is_password_protected ): ?>
    <div class="sdm_fancy5_password_notice">
        🔒 Passwort-geschützter Download
    </div>
    <?php endif; ?>
    
</div>

<style>
/* Custom Fancy 5 Styles - Kompakte Karten-Ansicht */
.custom-child-fancy-5 {
    border: 1px solid #e0e0e0;
    border-radius: 12px;
    background: linear-gradient(135deg, #ffffff 0%, #f8f9fa 100%);
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
    margin: 15px 0;
    padding: 20px;
    transition: all 0.3s ease;
    position: relative;
    overflow: hidden;
}

.custom-child-fancy-5::before {
    content: "FANCY 5";
    position: absolute;
    top: 5px;
    right: 10px;
    background: #9C27B0;
    color: white;
    padding: 2px 6px;
    font-size: 9px;
    border-radius: 3px;
    font-weight: bold;
}

.custom-child-fancy-5:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 20px rgba(0,0,0,0.15);
    border-color: #9C27B0;
}

.sdm_fancy5_header {
    display: flex;
    align-items: center;
    gap: 15px;
    margin-bottom: 15px;
}

.sdm_fancy5_icon {
    flex-shrink: 0;
    width: 60px;
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(45deg, #9C27B0, #E91E63);
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(156, 39, 176, 0.3);
}

.sdm_fancy5_thumbnail {
    width: 50px;
    height: 50px;
    object-fit: cover;
    border-radius: 8px;
}

.sdm_fancy5_default_icon {
    font-size: 24px;
    color: white;
}

.sdm_fancy5_title_section {
    flex-grow: 1;
}

.sdm_fancy5_title {
    margin: 0 0 8px 0;
    font-size: 18px;
    font-weight: 600;
    color: #333;
    line-height: 1.3;
}

.sdm_fancy5_quick_info {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    font-size: 12px;
    color: #666;
}

.sdm_fancy5_quick_info span {
    background: rgba(156, 39, 176, 0.1);
    padding: 3px 8px;
    border-radius: 12px;
    border: 1px solid rgba(156, 39, 176, 0.2);
}

.sdm_fancy5_button_section {
    flex-shrink: 0;
}

.sdm_fancy5_button_section .sdm-fancy5-btn {
    display: inline-block;
    background: linear-gradient(45deg, #9C27B0, #E91E63);
    color: white !important;
    padding: 12px 20px;
    border-radius: 25px;
    text-decoration: none;
    font-weight: 600;
    font-size: 14px;
    transition: all 0.3s ease;
    box-shadow: 0 3px 12px rgba(156, 39, 176, 0.3);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.sdm_fancy5_button_section .sdm-fancy5-btn:hover {
    background: linear-gradient(45deg, #7B1FA2, #C2185B);
    transform: translateY(-1px);
    box-shadow: 0 5px 15px rgba(156, 39, 176, 0.4);
}

.sdm_fancy5_description {
    background: rgba(158, 158, 158, 0.05);
    padding: 12px;
    border-radius: 8px;
    border-left: 4px solid #9C27B0;
    margin-bottom: 12px;
    font-size: 14px;
    line-height: 1.5;
    color: #555;
}

.sdm_fancy5_categories {
    margin-bottom: 10px;
}

.sdm_fancy5_category_tag {
    display: inline-block;
    background: linear-gradient(45deg, #FF9800, #FF5722);
    color: white;
    padding: 4px 10px;
    border-radius: 15px;
    font-size: 11px;
    font-weight: 600;
    margin-right: 6px;
    margin-bottom: 4px;
}

.sdm_fancy5_password_notice {
    background: linear-gradient(45deg, #FFC107, #FF9800);
    color: #333;
    padding: 8px 12px;
    border-radius: 6px;
    font-size: 13px;
    font-weight: 600;
    text-align: center;
}

/* Responsive Design */
@media (max-width: 768px) {
    .sdm_fancy5_header {
        flex-direction: column;
        text-align: center;
        gap: 10px;
    }
    
    .sdm_fancy5_quick_info {
        justify-content: center;
    }
    
    .sdm_fancy5_button_section {
        width: 100%;
    }
    
    .sdm_fancy5_button_section .sdm-fancy5-btn {
        display: block;
        text-align: center;
        width: 100%;
    }
}
</style>

Vorteile

Update-sicher: Plugin-Updates überschreiben Ihre Templates nicht
Flexibel: Beliebige Fancy-Nummern unterstützt
Einfach: Nur HTML/CSS/PHP Templates, keine komplexe Programmierung
Kompatibel: Funktioniert mit allen Simple Download Monitor Features
Performant: Templates werden nur geladen wenn benötigt

Lizenz & Support

Diese Lösung ist kostenlos nutzbar. Bei Problemen prüfen Sie:

  1. WordPress-Debug-Log
  2. Template-Dateinamen und -pfade
  3. Plugin-Kompatibilität
  4. PHP-Syntax in Template-Dateien

Plugin: Simple Download Monitor
GitHub: https://github.com/Arsenal21/simple-download-monitor
Template-System: Child-Theme Override Solution

Simple Download Monitor – Child Theme Template Override