Skip to content

Commit 457c675

Browse files
committed
fix css styles displaying on shop page
1 parent 81f1002 commit 457c675

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/css-optimize.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,19 @@ public static function parse_block_style( $block, &$style_arr ) {
209209
* @return void
210210
*/
211211
public function load_cached_css_for_post() {
212-
// DEV NOTE: If we'll also do this for wp_template and
212+
// DEV NOTE #1: If we'll also do this for wp_template and
213213
// wp_template_part then we might need to use the actions:
214214
// render_block_core_template_part_post and
215215
// render_block_core_template_part_file
216-
if ( is_singular() && ! is_preview() && ! is_attachment() ) {
217-
$post_id = get_the_ID();
216+
// DEV NOTE #2: Check for WooCommerce Shop Page as well
217+
if ( ( is_singular() && ! is_preview() && ! is_attachment() ) || ( function_exists('is_shop' ) && is_shop() ) ) {
218+
if ( function_exists('is_shop' ) && is_shop() ) {
219+
// use wc_get_page_id() instead of get_the_ID() because
220+
// the latter returns the product page ID instead of the shop page ID
221+
$post_id = wc_get_page_id( 'shop' );
222+
} else {
223+
$post_id = get_the_ID();
224+
}
218225
$this->optimized_css = get_post_meta( $post_id, 'stackable_optimized_css', true );
219226

220227
if ( ! empty( $this->optimized_css ) ) {

src/kses.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,29 @@
88
exit;
99
}
1010

11+
/**
12+
* In WooCommerce Shop page, <style> tags are stripped out and the CSS styles are displayed in the frontend.
13+
* This function removes the <style> tags and CSS styles before they are stripped out.
14+
*/
15+
if ( ! function_exists( 'stackable_pre_kses_woocomerce_shop' ) && function_exists( 'wc_get_page_id' ) ) {
16+
17+
function stackable_pre_kses_woocomerce_shop( $content, $allowed_html, $context ) {
18+
// Check if we are on the WooCommerce Shop page
19+
if ( is_shop() ) {
20+
$optimized_css = get_post_meta( wc_get_page_id( 'shop' ), 'stackable_optimized_css', true );
21+
22+
// remove CSS before kses strips out <style> tags
23+
if ( ! empty( $optimized_css ) ) {
24+
$content = str_replace( '<style>' . $optimized_css . '</style>', '', $content );
25+
}
26+
27+
}
28+
return $content;
29+
}
30+
31+
add_filter('pre_kses', 'stackable_pre_kses_woocomerce_shop', 10, 3);
32+
}
33+
1134
if ( ! function_exists( 'stackable_allow_wp_kses_allowed_html' ) ) {
1235

1336
/**

0 commit comments

Comments
 (0)