Cmb2: Admin Styles and Scripts not loading due to wrong path

Created on 18 Aug 2015  路  22Comments  路  Source: CMB2/CMB2

As said in title, CSS and JS get the wrong path (absolute file system path) on registration.

<link rel='stylesheet' id='cmb2-styles-css'  href='http://developer.dev/home/dev/web/wordpress/wp-content/plugins/cmb2/css/cmb2.min.css?ver=4.2.4' type='text/css' media='all' />

The problem is, that the url function in CMB2_Utils class utilizes cmb2_dir() and so the CMB2_DIR constant which is constructed for for includes.

I replaced the line

$this->url = trailingslashit( apply_filters( 'cmb2_meta_box_url', set_url_scheme( $cmb2_url ), CMB2_VERSION ) );

with

$this->url = plugins_url( '', __FILE__ ) . '/../';

as a workaround.

Please find a better solution.

Most helpful comment

This works perfectly for me:

if ( defined( "WP_DEBUG" ) && WP_DEBUG ) {
    function cm_update_cmb2_meta_box_url( $url ) {

        return plugin_dir_url( __FILE__ ) . '/vendor/webdevstudios/cmb2';
    }

    add_filter( 'cmb2_meta_box_url', 'cm_update_cmb2_meta_box_url' );
}

So I'm not really looking for a solution anymore, but thanks anyway 馃憤 . I'll probably remove that function when building for production.

All 22 comments

Please don't hack the plugin as you could break it for other plugins/themes depending on it. There is documentation for updating that url if it is not working properly: https://github.com/WebDevStudios/CMB2/wiki/Troubleshooting

TL;DR: there is a filter in place so hacking the plugin is not necessary.

I'm having the issue with CMB2 symlinked locally in my dev environment. My workaround:

add_filter( 'cmb2_meta_box_url', 'pilau_cmb2_meta_box_url' );
function pilau_cmb2_meta_box_url( $url ) {
    if ( defined( 'WP_LOCAL_DEV' ) && WP_LOCAL_DEV ) {
        $url = plugins_url( 'cmb2/' );
    }
    return $url;
}

I wonder why plugins_url() isn't used in the first place?

Because CMB2 can be loaded as a plugin, from a plugin, from mu-plugins, from a theme, etc.

I would like to solve this a bit better though. Can you provide more insight into your file structure and where CMB2 is loaded?

Ah, yes, forgot it could be packaged with a theme.

I have all my own plugins, and any I may contribute to, in a local folder outside the web root, /www-projects/, cloned from GitHub. I then just symlink those plugins locally. I can see there's a problem if you have to cater for the different locations CMB2 may be in - and maybe the filter is the best or only option? That's working for me for now, so no urgency, but interested if you find a solution that covers all bases.

I didn't realise this had happened, I guess I only started symlinking plugins after 3.9. But maybe of use:

https://make.wordpress.org/core/2014/04/14/symlinked-plugins-in-wordpress-3-9/

Similar issue here. We're including a bunch of composer dependencies in our WP root, and when I require init.php from our theme's functions.php file with something like this:
require( ABSPATH . '/vendor/webdevstudios/cmb2/init.php' );

. . . I end up with a console error with CMB2 attempting to pull in the following file (notice the bogus path):
{our_root_url}/srv/{our_root_path}/vendor/webdevstudios/cmb2/js/cmb2.min.js?ver=2.2.0

I know CMB2 is meant to be used as a standalone library, however it seems like it is required to live within wp-content for any of this to work, which ends up not working so well for the use case I'm describing.

Any ideas on this one?

@MickeyKay Yep, just need to filter that URL. https://github.com/WebDevStudios/CMB2/wiki/Troubleshooting#cmb2-urls-issues (like mentioned above).

function update_cmb2_meta_box_url( $url ) {
    return site_url( '/vendor/webdevstudios/cmb2' );
}
add_filter( 'cmb2_meta_box_url', 'update_cmb2_meta_box_url' );

This issue should be solved for almost all use-cases now (without need to filter). If any of you are able to test with the trunk branch, please do so.

Tried trunk, doesn't help on my local development environment when bundling CMB2 with a symlinked plugin.
That filter works though - is it production safe ?

@justnorris yes, the filter is there for the scenarios where CMB2 is not smart enough to figure it out. I've updated the documentation though, as there are some caveats to consider when using the filter (namely, try not to break CMB2 for other implementors): https://github.com/WebDevStudios/CMB2/wiki/Troubleshooting#cmb2-urls-issues

Can you provide me the broken URL vs the correct URL for your install? I want to see if there's ways to smarten CMB2's logic a bit more.

@jtsternberg Missed the github notification, sorry for the late response.

I'm running a local MAMP, with an actual domain, pointing to 10.0.0.10 ( my local IP assigned to my computer by the router ).

So I get 404 that looks like this:

http://bird.localbox.in/Users/N/Projects/Colormelon/Plugins/portfolio-plugin/vendor/webdevstudios/cmb2/css/cmb2.css?ver=4.6.1 Failed to load resource: the server responded with a status of 404 (Not Found)

And it should be something like this:

http://bird.localbox.in/wp-content/plugins/portfolio-plugin/vendor/webdevstudios/cmb2/css/cmb2.css?ver=4.6.1 Failed to load resource: the server responded with a status of 404 (Not Found)

Honestly, URLs for these types of resources should probably utilize the appropriate functions to get browser friendly URLs, and not server path versions.

In the meatime, @justnorris could use https://github.com/WebDevStudios/CMB2/wiki/Troubleshooting#cmb2-urls-issues

and have it return the following, if I'm seeing things correctly.

return str_replace( '/Users/N/Projects/Colormelon/Plugins/', '/wp-content/plugins/', $url );

Honestly, URLs for these types of resources should probably utilize the appropriate functions to get browser friendly URLs, and not server path versions.

This is not possible since CMB2 can be bundled/included in many places. Believe me, it's been discussed/hashed out before, including the rest of this thread.

fair enough.

This works perfectly for me:

if ( defined( "WP_DEBUG" ) && WP_DEBUG ) {
    function cm_update_cmb2_meta_box_url( $url ) {

        return plugin_dir_url( __FILE__ ) . '/vendor/webdevstudios/cmb2';
    }

    add_filter( 'cmb2_meta_box_url', 'cm_update_cmb2_meta_box_url' );
}

So I'm not really looking for a solution anymore, but thanks anyway 馃憤 . I'll probably remove that function when building for production.

Just hit this issue with a (locally symlinked) plugin that bundles CMB2. Just thinking about it... if I run the filter in my plugin, but another plugin bundling CMB2 gets loaded first, won't my filter still run and screw things up?

Logically, it would, but at the same time, the file you're referring to with the filter would still exist and load. At that point, the primary potential errors would be js selectors not finding things like expected, or some styles mismatching.

Interesting note, it only fails on my term metabox. Not on my post/page metabox. The filter fixed it, though i'm leery to use it.

Perhaps conditionally add the filter? Limit the scope of where it gets added.

The trick (as identified in the docs for that filter) is to do a string_replace on the url to only replace the bad bits with good bits, vs explicitly setting the URL to our local version. In my example on the wiki page, it's removing the incorrect bits of the symlink path, but this will not break things if CMB2 is loaded from another plugin/theme/etc. Hopefully that makes sense.

problem solved by just commenting these lines in init.php
// Kick the whole thing off. //require_once( cmb2_dir( 'bootstrap.php' ) ); //cmb2_bootstrap();

Given that those two lines are more or less essential to CMB2 running as a whole, it's definitely not a solution by any means.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

noquierouser picture noquierouser  路  8Comments

DevinWalker picture DevinWalker  路  4Comments

jflagarde picture jflagarde  路  6Comments

gorirrajoe picture gorirrajoe  路  4Comments

jquimera picture jquimera  路  8Comments