url_to_postid returns 0

when you are at the post URL, you don’t need to work with referer (which is not reliable moreover) but with $GLOBALS[“post”] because you are in the PHP code which generate the page then you can have the post ID with that : $post_id = $GLOBALS[“post”]->ID;

Add crossorigin to SCRIPT tag

script_loader_tag or script_loader_src filters are there to let you tweak the HTML of the script easily so you can add custom attributes: add_filter(‘script_loader_tag’, function($tag, $handle){ switch ( $handle ) { case ‘foo’: $tag = preg_replace( ‘/src=[\’|”|]/i’, ‘crossorigin $0’, $tag ); break; } return $tag; }, 10, 2); To avoid conflicting with other plugins, pass unique … Read more

Enqueue a file that’s not js or css

You could also look to create a special CSS file that you setup an htaccess rewrite rule for. For example wp-content/yourtheme/custom.css And then add a rule in your htaccess: RewriteEngine On RewriteRule ^/wp-content/yourtheme/custom.css$ /wp-content/yourtheme/styles.php [L] You can then call wp-load inside this styles.php file, do any sort of logic required, before outputting your custom css … Read more

Javascript asset not enqueuing with the rest

Your second wp_enqueue_script() contains the same handle (the same ID) as the first wp_enqueue_script(). The handle is the ID by which a registered script is known to WordPress internally. If you use this ID a second time, the second script won’t be registered. A working example could be this one: <?php add_action( ‘wp_enqueue_scripts’, ‘theme_enqueue_styles_and_scripts’ ); … Read more

Theme JS is available but theme CSS isn’t

It seems to me best to separate styles, from scripts and other assets. This worked: function my_theme_enqueue_styles() { wp_enqueue_style( ‘parent-style’, get_parent_theme_file_uri() . ‘style.css’ ); } add_action( ‘my_theme_enqueue_styles’, ‘wp_enqueue_styles’ ); function my_theme_enqueue_assets() { wp_enqueue_script( ‘behavior’, get_theme_file_uri( ‘behavior.js’ ), array(), null, true ); } add_action( ‘wp_enqueue_scripts’, ‘my_theme_enqueue_assets’ );

Using function from enqueued .js file in theme in plugin?

Yes. Dependencies work across all of WP, it doesn’t matter where you enqueue your script. All that matters is that you define your dependencies correctly, and you’re doing that. If, however, you plan to publish both the theme and the plugin as standalone versions, you shouldn’t rely on your plugin being used with your theme, … Read more

add WordPress script to page conditionally by category

is_single() doesn’t work for the page post type, use is_page() instead. You’re also missing the first argument for wp_enqueue_script, the script handle. Source should be the second argument. function my_conditional_enqueue_script1() { if ( is_page() && in_category( 19 ) ) { wp_enqueue_script( ‘my-cart-script’, ‘/members/application/cart/views/public/js/cart.js’ ); } } add_action(‘wp_enqueue_scripts’, ‘my_conditional_enqueue_script1’);