How to add code to just before closing body tag

How would I put the following code, for a certain shopping cart, just before the closing body tag in my WordPress site? In the footer? It seems to me that I would only want to call startcart() for product pages. Thanks very much.

<script language="javascript" type="text/javascript">
startcart()
</script>

Related posts

Leave a Reply

1 comment

  1. If you’re just using a small script or other markup, you can hook a function to the wp_footer filter, which should be included in all properly-coded themes:

    add_action( 'wp_footer', function () { ?>
    
        <script language="javascript" type="text/javascript">
        startcart()
        </script>
    
    <?php } );
    

    However, if your JavaScript code is more substantial, or you wish to use built-in libraries such as jQuery, you should put the code in an external file and enqueue it properly using the wp_enqueue_script() function:

    wp_enqueue_script(
        'myscript', // lowercase name of script
        get_template_directory_uri() . '/js/script.js', // url to script
        array( 'jquery' ), // libraries to use
        false, // version of script (false is WP version)
        true // load in footer (true) or head (false)? 
    );
    

    You can read more about how to use the wp_enqueue_script() function in the Codex


    If you need more help, please post a comment below.