WordPress-Post-Like-System
WordPress-Post-Like-System copied to clipboard
Don't enqueue scripts
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?
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
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?