• I am trying to enqueue some files. I have seen many ways to do it and none are working. Can someone tell me how to load a file from my theme folder?

    I have this on my page:
    <script>
    $( document ).ready( wpb_adding_scripts );
    
    $( window ).load( wpb_adding_scripts );
    </script>
    
    In my functions file I have:
    function wpb_adding_scripts() {
    
    wp_register_script('thickbox', get_stylesheet_directory_uri().'/_js/thickbox.js',array( 'jquery'));
    wp_register_script('jqFancyTransitions', get_stylesheet_directory_uri(__FILE__ ) . '/_js/jqFancyTransitions.1.8.min.js', array('jquery'),'1.8', true);
    wp_register_script('functions',  get_stylesheet_directory_uri(). '/_js/functions.js', array('jquery'),'1.8', true) ;
    }
    
    add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' );

Viewing 15 replies - 1 through 15 (of 19 total)
  • Depends what kind of file? (js only?)

    Thread Starter Bloke

    (@bloke)

    Yes only .js files

    Hey Bloke, then 2 ways to do it:

    1) adding it like you show in wp_register (get_stylesheet_directory)
    2) in header.php, a direct call like

    after <head> html tag

    <script src="http://my.js" type="text/javascript"></script>

    and between </head> html tag

    3) A little addon

    For a local js, located for example in mytheme/jsfiles/supercarousel.js

    Still in header.php, add something like:

    <script src="<?php (__FILE__); ?>"/jsfiles/supercarousel.js type="text/javascript"></script>

    Thread Starter Bloke

    (@bloke)

    Yes I want to clean up some old files that were placed in the header. WordPress says its best to enqueue them to the header. https://codex.wordpress.org/Function_Reference/wp_enqueue_script I have done it just like have it. Can’t get them to load. I looked at other plugins I have installed to and tried a similar path.

    Hi again Bloke,

    Can’t get to load other js files like i showed ?

    By the way, wp_enqueue is recommended especially to avoid Jquery conflicts. Some years before, it was recommended to enqueue for that purpose. Now, I kinda disagree – I saw too many themes bloated by wp_enqueue… some loading over 200Kb of data before actually starting to load some page text.

    Still, I have no idea about the issue, please check errors like in Chrome/Safari/Firefox code inspector to investigate. Possible issue would be with an obsolete js library?

    Thread Starter Bloke

    (@bloke)

    I echo get_stylesheet_directory_uri();
    and I get http://localhost/mysite/wp-content/themes/mytheme&gt;

    shouldn’t it be http://localhost/mysite/wp-content/themes/mytheme

    Yes it should! Your page is probably not in utf-8.. while theme is..

    I suggest you try the <?php __FILE__ ?>/locationofjsfileinsidethemedirectory/file.js as link to check. or simply hard code it in header.php file.

    Thread Starter Bloke

    (@bloke)

    Correct there were hard coded and I am removing that to clean it up. The character for the greater than symbol or a right closing tag is what showing when I post the code here. If vardump() it I get the correct string. Its a right bracket that is showing.
    I deactivated my plugins that use other jQury. I checked firebug for any errors. I made sure my Doctype is ok.
    http://localhost/mysite/wp-content/themes/mytheme >

    MMM very strange, but if Firebug doesn’t say it’s an error, no issue.
    Again, I repeat localhost is not the best way to test a WP theme:)

    It looks like there is a “>” in functions.php or a small file that is called all over the template.. could take ages to find exactly where, I had that kind of error a couple of times, and it’s better to start a fresh template in your case (faster, too). If you setup a new template, you’ll have no error I’m almost 100% sure 😀

    Thread Starter Bloke

    (@bloke)

    I used to think local development of WP sites was not possible. But with Dreamweaver CC its great. I even can design mobile with it.
    I put this back and the files load ok.

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js" ></script>
    <script type="text/javascript" src="<?php echo get_bloginfo('template_url');?>/_js/thickbox.js" ></script>
    <script type="text/javascript" src="<?php echo get_bloginfo('template_url');?>/_js/jqFancyTransitions.1.8.min.js" ></script>
    <script type="text/javascript" src="<?php echo get_bloginfo('template_url');?>/_js/functions.js" ></script>

    Well, I think we know the issue now.

    Thread Starter Bloke

    (@bloke)

    What’s the issue I should be able to load it the other way.

    Get_bloginfo(‘template_url’) is nice too, it’s the same as __file__/mytheme by the way,

    Have fun,

    Thread Starter Bloke

    (@bloke)

    I have two versions below and still won’t work:

    function wpb_adding_scripts() {
    
    wp_enqueue_style( 'style-name', get_stylesheet_uri() );//works
    wp_register_script('thickbox', get_template_directory_uri() .'/_js/thickbox.js',  array( ), 3.1, false);
    wp_register_script('jqFancyTransitions', get_template_directory_uri().'/_js/jqFancyTransitions.1.8.min.js',  array(), 1.8, false);
    wp_register_script('functions', get_template_directory_uri().'/_js/functions.js', array( ), 1.1, false);
    wp_enqueue_script( "scroll", plugin_dir_url( __FILE__ ) . '/_js/scroll.js', array( 'jquery' ) );//works copied form a plugin. Note the double quotes
    
    Version 2:
    wp_enqueue_style( 'style-name', get_stylesheet_uri() );//works
    wp_register_script('thickbox', get_template_directory_uri( __FILE__).'/_js/thickbox.js', array( 'jquery' ) );
    wp_register_script('jqFancyTransitions', get_template_directory_uri( __FILE__).'/_js/jqFancyTransitions.1.8.min.js', array( 'jquery' ) );
    wp_register_script("functions", get_template_directory_uri( __FILE__).'/_js/functions.js', array( 'jquery' ) );
    wp_enqueue_script( "scroll", plugin_dir_url( __FILE__ ) . '/_js/scroll.js', array( 'jquery' ) );//works copied form a plugin. Note the double quotes
    
    wp_enqueue_media ();//with and with out
    
    }
Viewing 15 replies - 1 through 15 (of 19 total)

The topic ‘Enqueue files from theme’ is closed to new replies.