How to remove Cornerstone’s styles from the head?

This is the reopening of the topic: How to remove Cornerstone's styles from the head?


How can I remove Cornerstone’s styles from the head of html?
I’m running Standalone Cornerstone ver.: 7.1.8.

I added filters in functions.php as stated in https://theme.co/docs/standalone-cornerstone#how-to-integrate-cornerstone-into-a-third-party-theme

Currently I have following filters without any success:
add_filter( ‘cornerstone_enqueue_styles’, ‘__return_false’ );
add_filter( ‘cornerstone_customizer_output’, ‘__return_false’ );
add_filter( ‘cornerstone_use_customizer’, ‘__return_false’ );


Kind regards.

Hi @dasbios,

Thanks for reaching out.
This issue was already reported by my colleague Christian, but unfortunately, there is no news till now. I will check with our development team about this and also add your thread so they will be aware of multiple reports.

Thanks

Hi @tristup,
Thank you for the information.

What is the timeframe of this fix?
My first report was 9 months ago.

Kind regards.

Hi @dasbios,

Unfortunately, we don’t have any news on ETA till now.

Thanks

I’ll start bringing back some of these filters in the next couple of releases. I’m not sure the optimizer one still is valid here, but cornerstone_enqueue_styles sounds useful. I was not aware this was feature in the past so I did rename, but I will also support the other name. If you have time, the below filter should remove a good portion of the generated CSS for Cornerstone Standalone and It would benefit me to know if there are other spots where this needs to be added. Let us know if you have any other questions too and have a great day.

add_filter("cs_use_standalone_theme_options", '__return_false');

I just tried. It did nothing.

Can I try something else?

If you haven’t tried the below line, this might help. But it might output other things instead. I see where it is also outputting and in a release or two cornerstone_enqueue_styles will work as it was before. Thanks for trying it out for me, at least it narrowed it down for me.

add_theme_support("cornerstone");

Hi, I tried. And no difference.

What should add_theme_support("cornerstone"); do?

It tells cornerstone that X or Pro is running essentially and doesn’t output it’s special styles. Or any theme that is handling these themselves, like your theme. It handles assignments differently and theme options too. If it didn’t help I’d leave it off unless you plan on getting really custom in the theme options. I’ll give you an update on the actual filter sometime this week. Probably a next week release thing.

I think I figured out why it was removed and or not included. There was a timing issue with hooks, but the following filter will remove the theme options inline css in the next release or Beta. Have a great day and let us know how we can help you in your custom theme.

add_filter( ‘cornerstone_enqueue_styles’, ‘__return_false’ );

This is working now.
It is nice that settings that are set directly on elements/components do get outputted.

There are still default CSS and JS that are outputted:

  • cs.ed2523f.css
  • cs-classic.ed2523f.js

How can I remove them if I need to?

Especially CSS is with 337 kB quite heavy and has a lot of styling that is really messing with our custom styling. Overwriting all those styles in our CSS is in a lot of cases not practical. It’s also a difficult and lengthy process, and it is just adding complexity.

If I dequeue/deregister the ‘CS’ style then linked CSS and inline CSS are removed.

I would like to keep the inline CSS so the styling controls on the Elements are still functional.

function stf_cornerstone(){
	wp_dequeue_style('cs');
	wp_deregister_style('cs');
}
add_action('wp_enqueue_scripts', 'stf_cornerstone', 9999);

Our current workaround solution to remove Corerstone’s linked CSS and, at the same time, keep Cornerstone inline styles :

function stb_cornerstone_remove_styles() {
	add_filter( 'cornerstone_enqueue_styles', '__return_false' );
}
add_filter( 'after_setup_theme', 'stb_cornerstone_remove_styles' );


function stb_enqueue_styles_and_scripts() {
	global $wp_styles;

	// Copy and insert inline overrun CSS from Cornerstone
	// Check if Cornerstone Style Exists
	if ( !empty( $wp_styles->registered[ 'cs' ] ) ) {
		// Check if Cornerstone Inline Styles Exists
		if ( !empty( $wp_styles->registered[ 'cs' ]->extra ) ) {
			// Create dummy handle
			wp_register_style( 'stb-cs', false );
			wp_enqueue_style( 'stb-cs' );

			// Copy Cornerstone Inline CSS over to the "dummy" stb-cs
			$wp_styles->registered[ 'stb-cs' ]->extra = $wp_styles->registered[ 'cs' ]->extra;
			
			// Let's make sure original Cornerstone linked CSS does not get outputted and,
			// at the same time CS Back-End editor WILL show real-time/WYSIWYG changes made in the editor
			$wp_styles->registered[ 'cs' ]->src = false;
		}

		// Remove URI so, linked CSS will not be inserted in Front-End and, at the same time
		// realtime/WYSIWYG styling in the Back-End CS Editor will still work
		// (its using inlined styles attached to 'cs' style which should not be removed)
		$wp_styles->registered[ 'cs' ]->src = false;
	}
}
add_action( 'wp_enqueue_scripts', 'stb_enqueue_styles_and_scripts', 9999 );


function stb_cornerstone_remove_css_scripts() {
	if ( !function_exists( 'CS' ) ) return;

	// Remove Cornerstone linked CSS
	wp_dequeue_style( 'cs' );
	//wp_deregister_style('cs');      //NOTE: If 'cs' is deregistered Cornerstone Back-End editor will NOT show changes made in the editor
}
add_action( 'wp_enqueue_scripts', 'stb_cornerstone_remove_css_scripts', 99999 );

It feels hackish and error-prone since one day you will probably change something that will break this, so it would be great and developer friendly if there would be separated hooks with which, we could control which CSS (linked and/or inlined) we would like to remove.
E.g.:

  • 'cornerstone_enqueue_styles’ / 'cornerstone_enqueue_styles_linked’
  • 'cornerstone_enqueue_styles_inline’

Do you have a hook like that? Could you provide it?

Kind regards.

PS: The same goes for Cornerstone scripts.

2 Likes

I made some modifications in my code above.

Hi @dasbios,

Glad that you shared the solution with others.

Thanks

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.