block-lab icon indicating copy to clipboard operation
block-lab copied to clipboard

block css not loading (wrong path) in roots bedrock environment (fix included)

Open lemon3 opened this issue 5 years ago • 8 comments

Feature description

can you please update the function get_url_from_path() in class-util.php

Use case

roots bedrock environment (or when the WP_HOME !== WP_SITEURL)

Additional information

here is my „new“ get_url_from_path() function in class-util.php

public function get_url_from_path( $path ) {
		// uri to current stylesheet directory
		$uri = get_stylesheet_directory_uri();
		// get the current (child-)theme name
		$theme_name = substr($uri, strrpos($uri, '/')+1);
		// relative path to the style file
		$rel_path = substr($path, strrpos($path, $theme_name) + strlen($theme_name));
		// stylesheet url
		return $uri . $rel_path;
}

lemon3 avatar Apr 13 '20 20:04 lemon3

Hi @lemon3, Thanks for bringing this up, I'll look at this.

kienstra avatar Apr 13 '20 20:04 kienstra

This fix does not work with Oxygen.

I stumbled upon this problem with Oxygen where you can not use a custom CSS file because it points to the wrong location (missing trailing slash on domain). I thought it was Oxygen being a problem.

See here: https://github.com/soflyy/oxygen-bugs-and-features/issues/1380

Zyles avatar Jul 29 '20 09:07 Zyles

This fix worked for me:

public function get_url_from_path( $path ) {
                $abspath = ABSPATH;

                // Workaround for weird hosting situations.
                if ( trailingslashit( ABSPATH ) . 'wp-content' !== WP_CONTENT_DIR && isset( $_SERVER['DOCUMENT_ROOT'] ) ) {
                        $abspath = sanitize_text_field( wp_unslash( $_SERVER['DOCUMENT_ROOT'] ) );
                }

                $stylesheet_url = str_replace( untrailingslashit( $abspath ), '', $path);

                return get_site_url() . '/' . $stylesheet_url;
        }

Zyles avatar Jul 29 '20 09:07 Zyles

Hi @Zyles, Thanks a lot for your snippet!

kienstra avatar Jul 29 '20 15:07 kienstra

@Zyles, i have tested your code in my environment, but it doesn't work ...

lemon3 avatar Jul 29 '20 16:07 lemon3

Why not?

Zyles avatar Jul 29 '20 16:07 Zyles

as „wordpress“ is in it's own subdir see here: https://roots.io/bedrock/

lemon3 avatar Jul 29 '20 17:07 lemon3

I use Oxygen not bedrock. I think it depends on your webserver config too.

Zyles avatar Jul 30 '20 06:07 Zyles