• I’d like to add a little bit of JavaScript to the footer of my homepage and for it to be inline – i.e. published on the page since I believe that this is best for loadings peed.

    Here’s my code:

    I believe that this might be the better approach:

    function add_inline_script() {
    echo “<script>/* do awesome things */</script>\n”;
    }
    add_action( ‘wp_footer’, ‘add_inline_script’, 0 );

    Is this the best approach for OceanWP? Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Do you want the code only to the homepage? If yes, then use the below example code –

    function add_inline_script() {
       if( is_home() || is_front_page() ) {
          echo "<script>/* do awesome things */</script>\n";
       }
    }
    add_action( 'wp_footer', 'add_inline_script', 0 );
    Thread Starter conciseac

    (@conciseac)

    That’s great thank you!

    Thread Starter conciseac

    (@conciseac)

    If I wanted to do exactly the same for CSS (only on the homepage) and insert inline into the header would I do this:

    function add_inline_style() {
       if( is_home() || is_front_page() ) {
          echo "<script>/* do awesome things */</script>\n";
       }
    }
    add_action( 'wp_header', 'add_inline_script', 0 );

    Thanks!

    Yes, you can use this code for adding the CSS.

    function add_inline_style() {
       if( is_home() || is_front_page() ) {
          echo "<style>/* do awesome things */</style>\n";
       }
    }
    add_action( 'wp_header', 'add_inline_style', 0 );
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Add custom JavaScript ONLY to the homepage and the footer?’ is closed to new replies.