Über Web, Tech, Games, Art,
Musik, Code & Design

5. Oktober 2026

WordPress-Standard-Plugins automatisch laden

Es gibt eine Hand voll WordPress-Plug-ins, die ich auf nahezu jeder Website einsetze: LazyBlocks, Limit Login Attempts, Pods & Resize Image After Upload. Mit folgendem Code in der functions.php kannst du dein Set an Standard-Plug-ins bei Aktivierung der Themes automatisch laden und installieren:

/**
 * Standard-Plugins nach der Theme-Aktivierung installieren.
 *
 * - Aktuelle Versionen aus WordPress.org beziehen.
 * - Bereits installierte Plugins überspringen.
 * - Plugins nicht automatisch aktivieren.
 * - Installationsfehler im Backend anzeigen.
 */

// Installation nach der Theme-Aktivierung vormerken.
add_action( 'after_switch_theme', function () {
    update_option( 'fbn_standard_plugins_pending', 1, false );
} );

// Installation bei einem regulären Backend-Aufruf ausführen.
add_action( 'admin_init', function () {

    if (
        ! get_option( 'fbn_standard_plugins_pending' )
        || ! current_user_can( 'install_plugins' )
        || wp_doing_ajax()
        || wp_doing_cron()
    ) {
        return;
    }

    require_once ABSPATH . 'wp-admin/includes/plugin.php';
    require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
    require_once ABSPATH . 'wp-admin/includes/file.php';
    require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';

    // Keine automatischen FTP-/SSH-Zugangsdatenabfragen auslösen.
    if ( 'direct' !== get_filesystem_method() ) {
        delete_option( 'fbn_standard_plugins_pending' );

        update_option(
            'fbn_standard_plugins_result',
            array(
                'errors' => array(
                    'Die automatische Installation benötigt direkten '
                    . 'Schreibzugriff. Bitte die Plugins unter Plugins '
                    . '→ Installieren manuell installieren.',
                ),
            ),
            false
        );

        return;
    }

    // Parallele Installationen verhindern.
    if ( get_transient( 'fbn_standard_plugins_lock' ) ) {
        return;
    }

    set_transient( 'fbn_standard_plugins_lock', 1, 5 * MINUTE_IN_SECONDS );

    // Pro Theme-Aktivierung nur einen Installationsversuch starten.
    delete_option( 'fbn_standard_plugins_pending' );

    $plugins = array(
        'lazy-blocks'                    => 'Custom Block Builder – Lazy Blocks',
        'limit-login-attempts-reloaded'  => 'Limit Login Attempts Security',
        'pods'                          => 'Pods – Custom Content Types and Fields',
        'resize-image-after-upload'     => 'Resize Image After Upload',
    );

    $installed = get_plugins();
    $errors    = array();

    try {
        foreach ( $plugins as $slug => $name ) {

            // Vorhandene Plugins anhand ihres Verzeichnisses erkennen.
            $exists = false;

            foreach ( array_keys( $installed ) as $plugin_file ) {
                if (
                    strpos( $plugin_file, $slug . '/' ) === 0
                    || $plugin_file === $slug . '.php'
                ) {
                    $exists = true;
                    break;
                }
            }

            if ( $exists ) {
                continue;
            }

            // Aktuelle Download-Adresse von WordPress.org abfragen.
            $api = plugins_api(
                'plugin_information',
                array(
                    'slug'   => $slug,
                    'fields' => array(
                        'sections' => false,
                    ),
                )
            );

            if ( is_wp_error( $api ) ) {
                $errors[] = $name . ': ' . $api->get_error_message();
                continue;
            }

            if ( empty( $api->download_link ) ) {
                $errors[] = $name . ': Keine Download-Adresse gefunden.';
                continue;
            }

            // Anforderungen der aktuellen Plugin-Version prüfen.
            if (
                ! empty( $api->requires )
                && ! is_wp_version_compatible( $api->requires )
            ) {
                $errors[] = $name . ': Benötigt WordPress '
                    . $api->requires . ' oder neuer.';
                continue;
            }

            if (
                ! empty( $api->requires_php )
                && ! is_php_version_compatible( $api->requires_php )
            ) {
                $errors[] = $name . ': Benötigt PHP '
                    . $api->requires_php . ' oder neuer.';
                continue;
            }

            // Ohne direkte HTML-Ausgabe im Backend installieren.
            $skin     = new WP_Ajax_Upgrader_Skin();
            $upgrader = new Plugin_Upgrader( $skin );
            $result   = $upgrader->install( $api->download_link );

            if ( is_wp_error( $result ) ) {
                $errors[] = $name . ': ' . $result->get_error_message();
            } elseif ( true !== $result ) {
                $details = $skin->get_errors()->get_error_message();

                $errors[] = $name . ': '
                    . ( $details ?: 'Installation fehlgeschlagen.' );
            }
        }

        update_option(
            'fbn_standard_plugins_result',
            array( 'errors' => $errors ),
            false
        );
    } finally {
        delete_transient( 'fbn_standard_plugins_lock' );
    }
}, 20 );

// Ergebnis einmalig als Backend-Hinweis anzeigen.
add_action( 'admin_notices', function () {

    if ( ! current_user_can( 'install_plugins' ) ) {
        return;
    }

    $result = get_option( 'fbn_standard_plugins_result' );

    if ( ! is_array( $result ) ) {
        return;
    }

    delete_option( 'fbn_standard_plugins_result' );

    if ( empty( $result['errors'] ) ) {
        echo '<div class="notice notice-success is-dismissible"><p>';
        echo esc_html(
            'Alle vier Standard-Plugins sind installiert. '
            . 'Neu installierte Plugins kannst du unter Plugins aktivieren.'
        );
        echo '</p></div>';
        return;
    }

    echo '<div class="notice notice-warning is-dismissible"><p><strong>';
    echo esc_html( 'Standard-Plugins: Nicht alle Plugins konnten installiert werden.' );
    echo '</strong></p><ul>';

    foreach ( $result['errors'] as $error ) {
        echo '<li>' . esc_html( $error ) . '</li>';
    }

    echo '</ul><p>';
    echo esc_html( 'Fehlende Plugins bitte unter Plugins → Installieren nachinstallieren.' );
    echo '</p></div>';
} );Code-Sprache: PHP (php)

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

FRTG - Traces
Jetzt bei Apple Music & Spotify.