Using js and css with a wordpress plugin

I am trying to use css and js in a wordpress plugin I developed. I looked into the codex and I know I have to use wp_enqueue_script to add js, and wp_enqueue_style to add css with plugins_url to output the correct path, but I can’t figure out how to do it. I have a file structure so that it’s the plugin then plugin > css and plugin > js where css and js are folders that contain their respective files.

I tried doing:

Read More
function my_scripts_method() {
    wp_enqueue_script('js/tabpane.js');
    wp_enqueue_script('local/helptip.js');
    wp_enqueue_script('local/webfxapi.js');
    wp_enqueue_script('local/webfxlayout.js');
}

add_action('wp_enqueue_scripts', 'my_scripts_method'); // For use on the Front end (ie. Theme)

but it’s not working at all, and the codex isn’t of much more help. Could anyone let me know what I’m missing?

Related posts

Leave a Reply

1 comment

  1. When adding files to backend plugins you need to do a few things within your code:

    add_action('init', 'add_main_javascript');
    
    function add_main_javascript(){
        $slug = "main";
        $file = "inc/js/$slug.js";
        include_js_files($file, $slug);
    }
    
    function include_js_files($file, $slug, $in_footer = NULL){
        if(!$file)
        $file = "inc/js/filename.js";
        $your_plugin_dir = plugin_dir_path(__FILE__);
        $your_plugin_url = plugin_dir_url(__FILE__);
        if(file_exists($your_plugin_dir.$file))
        // array('jquery') is the scope of the javascript
        // Set $in_footer to true if you need the js to be in the footer
        wp_enqueue_script("filename.js", $your_plugin_url.$file, array ( 'jquery' ), "", $in_footer);
    }
    

    Or you could use just the add_action(‘init’, ‘your_code_for_enqueue’);