WordPress.org

Forums

[resolved] Custom theme JavaScript not working (3 posts)

  1. sorenkalla
    Member
    Posted 11 months ago #

    I am attempting to design my own wordpress theme, but I'm having issues getting my JavaScript to work, and nothing I have researched seems to have the answer. If anyone can help me out I'd greatly appreciate it!

    Things I have done:

    Enqueue my javascript file in functions.php:

    function sorenTheme_scripts() {
        wp_enqueue_style('style', get_stylesheet_uri());
        wp_register_script( 'soren-script', get_template_directory_uri() . '/js/soren-script.js', array( 'jquery', 'jquery-ui-core' ), '1', true );
        wp_enqueue_script('soren-script');
    }
    add_action('wp_enqueue_scripts', 'sorenTheme_scripts');

    Create a simple javaScript file in the directory {template folder}/js named soren-script.js, which currently just contains a very simple alert test

    alert('Hello World');

    and that's it. When I tried to put the alert in the index.php file directly using the script tags, the alert came up as expected, but when I move it to the js file I get nothing. Is there something i need to add to the .php or .js files?

  2. stephencottontail
    Member
    Posted 11 months ago #

    Shot in the dark, but are you remembering to call wp_footer() in your theme? Because you put true for the final argument to wp_register_script(), WordPress will load the script at the bottom of the <body> tag, but you need to call wp_footer() for that to work.

  3. eco7eco
    Member
    Posted 3 weeks ago #

    Thank you x1,000,000, @stephencottontail! I was tearing my hair out trying to figure out why pretty much anything related to jQuery wouldn't work on my extremely stripped down custom theme, which I made to embed elsewhere. I recklessly removed the footer thinking that I wouldn't need it.

    This has been plaguing me for hours. After reading dozens of blog posts and help articles, I stumbled upon your comment. I restored get_footer(), and all is right again in my little corner of the world!

Reply

You must log in to post.

About this Topic