• I’m trying to pass Custom CSS in global variable to use it in another function then to pass it to wp_enqueue_scripts via wp_add_inline_style but the global variable whenever I use it as global it doesn’t return anything only if i use add_action('wp_footer', 'my_function') below i’ll show you my code and functions

    I’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 run wp_add_inline_style anywhere else except wp_enqueue_scripts this is to eliminate errors on W3C and add inline style to wp_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)
  • Are you saying that the print_r showed an empty array or that your styles were not output by the wp_add_inline_style call?

    It’s not true that you can only call wp_add_inline_style in wp_enqueue_scripts. As it says on the Code Reference
    “Styles will only be added if the stylesheet is already in the queue.”

    If you want to output styles in the head section put your function hook on wp_head.
    If you want to make sure that an inline style is after your main stylesheet, use wp_add_inline_style withe the handle of the that stylesheet, after it is enqueued.
    You don’t need a global variable. You can call the function at the time of the enqueue.
    Your function is sometimes echoing and sometimes putting it into the global. Make it one or the other and put it on the appropriate action hook.

    Thread Starter Berat Gashi

    (@beratzgashi)

    Hi @joyously,

    Thanks for your reply.

    My function cru_generate_custom_style is called in post content and everywhere else and the only one that loads after this function is wp_footer so that’s why it doesn’t load my global variables in other hook, so when i call wp_enqueue_scripts it actually gets an empty array because the global variables are set way after the wp_enqueue_scripts or wp_head so i don’t know another way to set my custom css from everywhere to an array then to print it via wp_add_inline_style

    • This reply was modified 5 years, 3 months ago by Berat Gashi.
    Moderator bcworkz

    (@bcworkz)

    You can echo out an inline style block almost anywhere you want. In the middle of content if you must, though doing so could slow down page loads. What you cannot do is output a style block before code knows what it is ?? If your code decides what CSS is required during content output, you cannot use actions that have already fired in “wp_head” or earlier. As you noted, “wp_footer” is about the only reliable hook after content output.

    But “wp_footer” fires too late to use wp_add_inline_style(). If you want a style block output at “wp_footer”, just echo out the requisite style block.

    Sounds like you are going about styles backward. The content should have a class applied, and the class is defined in the head section. The content should not determine the styles.

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.