Custom function not passing Global Variables to other functions
-
I’m trying to pass Custom CSS in global variable to use it in another function then to pass it to
wp_enqueue_scripts
viawp_add_inline_style
but the global variable whenever I use it as global it doesn’t return anything only if i useadd_action('wp_footer', 'my_function')
below i’ll show you my code and functionsI’ve tried many methods but still the same problem!
I use
cru_generate_custom_style
in many files to generate custom css for custom elements and based on WordPress you can’t runwp_add_inline_style
anywhere else exceptwp_enqueue_scripts
this is to eliminate errors on W3C and add inline style towp_head
global $cru_bottom_styles; $cru_bottom_styles = array(); function cru_generate_custom_style( $selector = '', $props = '', $media = '', $footer = true ) { global $cru_bottom_styles; $css = ''; // Selector Start $css .= $selector . ' {' . PHP_EOL; // Selector Properties $css .= str_replace( ';', ';' . PHP_EOL, $props ); $css .= PHP_EOL . '}'; // Selector End // Media Wrap if ( trim( $media ) ) { $css = "@media {$media} { {$css} }"; } if ( ! $footer || defined( 'DOING_AJAX' ) ) { echo "<style>{$css}</style>"; return; } $cru_bottom_styles[] = $css; } function cru_parse_header_styles() { global $cru_bottom_styles; print_r($cru_bottom_styles); wp_add_inline_style( 'main', implode( PHP_EOL . PHP_EOL, $cru_bottom_styles ) ); } add_action( 'wp_enqueue_scripts', 'cru_parse_header_styles' );
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Custom function not passing Global Variables to other functions’ is closed to new replies.