Scripte in den Footer verschieben und einzelne Scripte ausschließen

  • Hallo liebe Community,

    ich hab mit folgenden Code

    PHP
    // Javascript to footer --> läd alles im footer! 
    
    
    remove_action('wp_head', 'wp_print_scripts');
    remove_action('wp_head', 'wp_print_head_scripts', 9);
    remove_action('wp_head', 'wp_enqueue_scripts', 1);
    add_action('wp_footer', 'wp_print_scripts', 5);
    add_action('wp_footer', 'wp_enqueue_scripts', 5);
    add_action('wp_footer', 'wp_print_head_scripts', 5);

    alle Scripte in den Footer verschoben. Nun funktioniert aber meine Google-maps-Karte auf der Seite nicht mehr. Weiß Jemand wie man ein Script von der Verschiebung aus dem Head in den Footer ausschließen kann? Bzw. gibt es eine andere alternative, sodass die Google-Maps-Karte trotzdem noch funktioniert?

    Ich freue mich über Antworten!

    Beste Grüße

    • Anzeige

    Hallo!

    Wenn du gerade an deiner Website arbeitest oder dein aktuelles Hosting überdenkst: Wir betreiben mit NetzLiving eine Hosting-Plattform, die speziell auf Performance, Sicherheit und einfache Verwaltung ausgelegt ist.

    • ✔️ Schnelle Ladezeiten (optimiert für WordPress & Co.)
    • ✔️ Deutsche Server & DSGVO-konform
    • ✔️ Persönlicher Support (kein 0815-Ticket-System)

    Mehr erfahren

  • lass remove_action('wp_head', 'wp_enqueue_scripts', 1); und add_action('wp_footer', 'wp_enqueue_scripts', 5); einfach weg. Stattdessen könntest du über die functions.php lieber die Scripte direkt in den Footer laden, bsp. jquery:

    PHP
    function own_scripts_loader() {
          if(!is_admin()) {
            wp_deregister_script( 'jquery' );
            wp_register_script( 'jquery', '/wp-includes/js/jquery/jquery.js', false, null, true );
            wp_enqueue_script( 'jquery');
          }
        }
        add_action('wp_enqueue_scripts', 'own_scripts_loader', 1);

    Dazu schau dir den 5. Parameter ($in_footer) von wp_register_script() an: https://codex.wordpress.org/Function_Refer…register_script

    [size=10]Bitte immer zuerst die Forensuche nutzen und erst danach Deine Fragen hier im Forum stellen, denn sehr vieles wurde schon mehrfach beantwortet. Bitte dabei auch die Forenregeln beachten.
    Kein Support per PN - Fragen gehören ins Forum.[/SIZE]
    [size=8]Wenn du eine weise Antwort verlangst, musst du vernünftig fragen. (Johann Wolfgang von Goethe)[/SIZE]

  • Danke für deine Antwort!

    Leider beherrsche ich kein php. Aber das Buch ist unterwegs ;-)

    Also könnte ich mit deiner Variante die javascript Bibliotheken aus dem Ordner /wp-includes in den Footer laden, oder? Jetzt habe ich viele .js Scripte im Ordner /Plugins z.B, ( /wp/wp-content/plugins/wc-shortcodes/includes/js/rsvp.js?ver=1.77
    Nach meinem Verständnis müsste ich jetzt jedes einzelne Script dann mit deiner Funktion behandeln also so:

    [COLOR=#0000BB][FONT=monospace] [/FONT][/COLOR][COLOR=#007700][FONT=monospace]function [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]own_scripts_loader[/FONT][/COLOR][COLOR=#007700][FONT=monospace]() {
    if(![/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]is_admin[/FONT][/COLOR][COLOR=#007700][FONT=monospace]()) {
    [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]wp_deregister_script[/FONT][/COLOR][COLOR=#007700][FONT=monospace]( [/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]'jquery' [/FONT][/COLOR][COLOR=#007700][FONT=monospace]);
    [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]wp_register_script[/FONT][/COLOR][COLOR=#007700][FONT=monospace]( [/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]'jquery'[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]'[/FONT][/COLOR]/wp/wp-content/plugins/wc-shortcodes/includes/js/rsvp.js?ver=1.77[COLOR=#DD0000][FONT=monospace]'[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]false[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]null[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]true [/FONT][/COLOR][COLOR=#007700][FONT=monospace]);
    [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]wp_enqueue_script[/FONT][/COLOR][COLOR=#007700][FONT=monospace]( [/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]'jquery'[/FONT][/COLOR][COLOR=#007700][FONT=monospace]);
    }
    }
    [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]add_action[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]'wp_enqueue_scripts'[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]'own_scripts_loader'[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1[/FONT][/COLOR][COLOR=#007700][FONT=monospace]);

    [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]
    [/FONT][/COLOR]Oder kann ich die auch alle nacheinander aufzählen?


    Wie würde Deine zweite Variante mit dem [COLOR=#333333]($in_footer) aussehen?

    [/COLOR][COLOR=#0000BB][FONT=monospace] [/FONT][/COLOR][COLOR=#007700][FONT=monospace]function [/FONT][/COLOR]wp_enqueue_script[COLOR=#007700][FONT=monospace]() { [/FONT][/COLOR]

    [COLOR=#0000BB][FONT=Consolas]<?php wp_register_script[/FONT][/COLOR][COLOR=#007700][FONT=Consolas]([/FONT][/COLOR][COLOR=#007700][FONT=Consolas] [/FONT][/COLOR][COLOR=#0000BB][FONT=Consolas]$in_footer [/FONT][/COLOR][COLOR=#007700][FONT=Consolas]); [/FONT][/COLOR][COLOR=#0000BB][FONT=Consolas]?>
    [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]wp_register_script[/FONT][/COLOR][COLOR=#007700][FONT=monospace]( [/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]'jquery'[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]'[/FONT][/COLOR]/wp/wp-content/plugins/wc-shortcodes/includes/js/rsvp.js?ver=1.77[COLOR=#DD0000][FONT=monospace]'[/FONT][/COLOR][COLOR=#007700][FONT=monospace],[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]true [/FONT][/COLOR][COLOR=#007700][FONT=monospace]);

    }

    [/FONT][/COLOR]Sicherlich völlig falsch, oder?

Jetzt mitmachen!

Sie haben noch kein Benutzerkonto auf unserer Seite? Registrieren Sie sich kostenlos und nehmen Sie an unserer Community teil!