Skip to content
This repository has been archived by the owner on Jul 25, 2023. It is now read-only.

Don't enqueue scripts #44

Open
kilianso opened this issue Mar 4, 2018 · 2 comments
Open

Don't enqueue scripts #44

kilianso opened this issue Mar 4, 2018 · 2 comments

Comments

@kilianso
Copy link

kilianso commented Mar 4, 2018

Hey there

Great code! Just a question: How can we use this without enqueue the script in the functions.php?

function sl_enqueue_scripts() {
	wp_enqueue_script( 'simple-likes-public-js', get_template_directory_uri() . '/js/simple-likes-public.js', array( 'jquery' ), '0.5', false );
	wp_localize_script( 'simple-likes-public-js', 'simpleLikes', array(
		'ajaxurl' => admin_url( 'admin-ajax.php' ),
		'like' => __( 'Like', $textdomain ),
		'unlike' => __( 'Unlike', $textdomain )
	) );
}

I'm using Sage which bundles all the scripts so i don't want separate scripts in my functions.php, how can i achieve this? Or would it be possible to just place all of this in a plugin?

@rdimascio
Copy link

Hey, I'm relatively new to Sage development, but I'm stealing this from the PDF - Theme Development with Sage 2nd Edition. Maybe @JonMasterson can chime in if off base here.

First, you'll need to add simple-likes-public.js to assets/scripts

Then to the JSON manifest at assets/manifest.json

{ 
    "dependencies": { 
        "simple-likes-public.js": { 
            "files": [ 
                "scripts/simple-likes-public.js" 
            ] 
        },

Then you can enqueue plugin scripts in Sage at the bottom of lib/setup.php

wp_enqueue_script('theme_homepage', asset_path('scripts/simple-likes-public.js'), ['jquery'], null, true);

You should do the same thing for the stylesheet and you should also make sure to dequeue the plugin assets in lib/setup.php

@kilianso
Copy link
Author

Hi!

Well the problem is the wp_localize_script. If I don't enqueue the script first, the lines below won't work. So how can I add the ajaxurl, like and unlike functionality if the main.js contains all the simpleLikes code and is enqueued somewhere else?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants