• Resolved eliant

    (@eliant)


    This works correctly:

    <?php
    $plugin_path = "wp-content/plugins/my_plugin";
    wp_enqueue_style("my_style",	$plugin_path."/CSS/style.css", false, "1.0", "all");	
    wp_enqueue_script("my_js",	"/".$plugin_path."/JS/scripts.js", false, "1.0"); 	
    ?>

    Notice I had to add a slash when calling “enqueue_script”

    Both functions have identical descriptions of $src in the documentation:
    “Full URL of the stylesheet, or path of the stylesheet/script relative to the WordPress root directory.”

    Before adding the slash, the scripts failed. When I examined the source code of my site and saw this:
    <script src='https://mysite.comwp-content/plugins/my_plugin/JS/scripts.js?ver=1.0' id='myplugin-js'></script>

    I think this is a bug, or there needs to be a correction in the documentation.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Anonymous User 14808221

    (@anonymized-14808221)

    You shouldn’t use the path hardcoded like that.

    It is better to use it like this (for themes)

    wp_enqueue_script(
    		'handle',
    		get_template_directory_uri() . '/path/to/file.js',
    		array(),
    		'1234', true
    	);
    
    
    
    wp_enqueue_style(
    		'handle',
    		get_template_directory_uri() . '/path/to/file.css',
    		array(),
    		'1234'
    	);
    

    If you use it in a plugin, use plugin_dir_url( )

    This will nicely load the relative path to the file, and work always, as opposed to hardcoded paths, which might not work when you rename plugin name for example (which is a part of a simple debug procedure)

    • This reply was modified 4 years, 11 months ago by Anonymous User 14808221.
    • This reply was modified 4 years, 11 months ago by Anonymous User 14808221.
    Thread Starter eliant

    (@eliant)

    Using a URI to enqueue forces a DNS look-up, which is a waste of resources and slows page load. I’d like to avoid that by using the absolute filepath for this call, but I can’t without the hard-coding (at least not while enqueue expects the second argument to be everything “to the right of WordPress root” in the filepath to be hardcoded.

    And anyway, this is all beside the point. The point is…

    enqueue_style and enqueue_script have different behaviors, even though documentation claims they behave the same.

    DEVELOPERS – Pick one of the enqueue functions and make it work like the other, or correct the documentation.

    • This reply was modified 4 years, 11 months ago by eliant.
    Anonymous User 14808221

    (@anonymized-14808221)

    I do not think this is the case, not only because I am a developer and do read the core code of those functions when I use them, and there is nothing that will remove or add your slashes if and wether you hardcode or not, but I also took the time to confirm this in a local install.

    Your suggested code will throw an error on the first line (the CSS) because missing the / at the path start of the relative path.

    If you really want to hardcode the path, this is what works:

    
    $plugin_path = "/wp-content/plugins/your-plugin-name";
    wp_enqueue_style("my_style", $plugin_path."/public/css/style.css", array(), false, "all");	
    wp_enqueue_script("my_js", $plugin_path."/public/js/script.js", array(), false, true);
    

    Note that I think you also missed the dependency in your code, I added it in above example for completeness.

    Now, if you think you have found a bug in this behaviour and have clear steps to replicate it the right place to report for fix would be the Trac platform here https://core.trac.wordpress.org
    The documentation is generated from the Code, so what is stated in the documentation generally is not human work, but generated by a program from the code, hence reflecting by the letter what is commented in code. There can hence not be any "mistake" in this sense in the Documentation if not also present in the code.

    However, as I tested and deduced from reading the code the functions work just fine.
    Even if your path would change, that wouldn't be due to the enqueue functions, since those cannot change the path you pass hardcoded.

    Note again that with this hardcoded approach if someone renames wp-content or renames your plugin main folder the plugin will fail.

    I hope this helps

    Thread Starter eliant

    (@eliant)

    I must apologize. During my initial investigation of the problem, I looked at the wrong line in my page’s source code. Turns out both the style and the script were missing the slash. So there was no error.

    Now, about the path hardcoding, I get it. Always did. But since I’m the website developer and administrator on my own dedicated server, Renaming a path is not something I worry about. But like I said, I get it. So how about this…

    $r = dirname(__FILE__);
    $ABS = strlen(ABSPATH);
    $plugin_path = substr($r, $ABS-1);
    
    wp_enqueue_style("my_style", $plugin_path."/CSS/style.css", array(), false, "2.0", "all");	
    wp_enqueue_script("my_js", $plugin_path."/JS/script.js", array(), false, "2.0");

    ON A PERSONAL NOTE: I’m building this site for my neighborhood homeowners’ association. A resident is telling his neighbors, “I’m a professional web developer, and WordPress is the worst-written CMS with terrible security problems.” So saying “I’m a developer” doesn’t add much weight to your response (but I really do appreciate your response) and can be off-putting to folks who have been in this business for many years but incorrectly think they’ve stumbled onto an issue.

    Anonymous User 14808221

    (@anonymized-14808221)

    I am happy your question has been answered, we would love if you would mark this topic as resolved in the sidebar.

    This helps our volunteers find the topics that still need attention and more people will get helped, possibly like you did.

    Cheers.

    Thread Starter eliant

    (@eliant)

    LAST NOTE, before I close this thread, and I think it’s important…

    My attempt to avoid DNS look-up was futile. Enqueue translated my absolute filepath into a URI before generating the HTML that gets sent to my browser.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘ERROR: Trailing slash when enqueuing styles vs. scripts’ is closed to new replies.