• Resolved Carlos A.

    (@carlos-a)


    Hi, i’m new in wordpress, i made a custom theme and i have a problem with javascript on my wordpress site, i used the same html and js files that i used on a joomla site and the js files are not working, i try to make a html with the js files and it works fine, the problem is when i add it to my wordpress site.

    On the footer.php i have this

    <script type=”text/javascript” src=”https://external/assets/scripts/jquery.min.js”></script&gt;
    <script type=”text/javascript” src=”<?php echo get_stylesheet_directory_uri(); ?>/js/portal.min.js”></script>

    </body>
    </html>

    if i saw the code of the site it shows that it have the js files but its not working, someone please help me with this, i’m stuck here

Viewing 8 replies - 1 through 8 (of 8 total)
  • URL?

    I assume this is not the actual URL/code on the site?

    https://external/assets/scripts/jquery.min.js&#8221;

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Thread Starter Carlos A.

    (@carlos-a)

    Yes Colin, thats not the actual, is an ip address, for security reason don’t post the ip, i’ve read a lot about adding jquery to wordpress, i saw the complete code that generate the wordpress and i only see my js files, so i don’t know what is the problem

    Thread Starter Carlos A.

    (@carlos-a)

    And another question how do i add the functions.php to my theme?
    just use the include(get_template_directory_uri().’/js/myscript’);
    and where i should put that?

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    When you say custom theme, did you build it from scratch or are you working from an existing theme?

    Thread Starter Carlos A.

    (@carlos-a)

    Hi Andrew, i build it from scratch.

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    You can create your functions.php file in the Notepad software if you’re using Windows. Just save the file with a ‘.php‘ extension and it will be a PHP file automatically. Then using which ever file transfer protocol your hosting providers give you, upload that functions.php file to your main theme folder. E.g.:

    /wp-content/themes/your-theme/functions.php

    You don’t need to do this bit:

    <script type="text/javascript" src="https://external/assets/scripts/jquery.min.js"></script>

    You can instead just call the jQuery library that WordPress is built on. This is already packed into your WordPress installation. To use it you need only put this into your functions.php file:

    <?php 
    
    /**
     * Proper way to enqueue scripts
     */
    function my_scripts() {
    	wp_enqueue_script('jquery');
    }
    
    add_action( 'wp_enqueue_scripts', 'my_scripts' );
    
    ?>

    The jQuery library will then be automatically loaded in at the top of the page.

    Then you can load your custom script that relies on the jQuery library, like this:

    wp_enqueue_script( 'general', get_template_directory_uri() . '/js/myscript.js', array( 'jquery' ));

    All together your functions.php code would be:

    <?php 
    
    /**
     * Proper way to enqueue scripts and styles
     */
    function my_scripts() {
        wp_enqueue_script('jquery');
        wp_enqueue_script( 'general', get_template_directory_uri() . '/js/myscript.js', array( 'jquery' ));
    }
    
    add_action( 'wp_enqueue_scripts', 'my_scripts' );
    
    ?>

    Thread Starter Carlos A.

    (@carlos-a)

    Thank you so much Andrew for the help, it really help me

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Javascript in Custom Theme’ is closed to new replies.