Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Admin Styles and Scripts not loading due to wrong path #432

Closed
bitstarr opened this issue Aug 18, 2015 · 22 comments
Closed

Admin Styles and Scripts not loading due to wrong path #432

bitstarr opened this issue Aug 18, 2015 · 22 comments

Comments

@bitstarr
Copy link

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.

@jtsternberg
Copy link
Member

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.

@gyrus
Copy link
Contributor

gyrus commented Aug 18, 2015

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?

@jtsternberg
Copy link
Member

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?

@gyrus
Copy link
Contributor

gyrus commented Aug 18, 2015

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.

@gyrus
Copy link
Contributor

gyrus commented Aug 18, 2015

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/

@MickeyKay
Copy link

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?

@jtsternberg
Copy link
Member

@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' );

jtsternberg added a commit that referenced this issue Apr 20, 2016
…bizarre cases. Also include normalize_path wrapper for back-compat. Fixes #27, Fixes #432
@jtsternberg
Copy link
Member

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.

@pyronaur
Copy link

pyronaur commented May 17, 2016

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 ?

@jtsternberg
Copy link
Member

@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.

@pyronaur
Copy link

pyronaur commented Sep 30, 2016

@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)

@tw2113
Copy link
Contributor

tw2113 commented Sep 30, 2016

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 );

@jtsternberg
Copy link
Member

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.

@tw2113
Copy link
Contributor

tw2113 commented Sep 30, 2016

fair enough.

@pyronaur
Copy link

pyronaur commented Oct 4, 2016

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.

@JiveDig
Copy link

JiveDig commented Apr 28, 2017

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?

@tw2113
Copy link
Contributor

tw2113 commented Apr 28, 2017

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.

@JiveDig
Copy link

JiveDig commented Apr 28, 2017

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.

@tw2113
Copy link
Contributor

tw2113 commented Apr 28, 2017

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

@jtsternberg
Copy link
Member

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.

@taimurahmad971
Copy link

taimurahmad971 commented Mar 25, 2018

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

@tw2113
Copy link
Contributor

tw2113 commented Mar 25, 2018

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.

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

No branches or pull requests

8 participants