• Resolved Gyurmey

    (@gyurmey)


    Hi,

    I have this code: echo wp_get_global_styles(array('color', 'background')); and I am trying to get data from global styles, but instead of var(–wp–preset–color–background) I get var:preset|color|background. How could I get the correct variables?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator Kathryn Presner

    (@zoonini)

    Hi @gyurmey – I asked @greenshady to take a look at this, and he shared that this function:

    add_action( 'init', function() {
    	var_dump( wp_get_global_styles(array('color', 'background')) );
    }, 99999 );
    

    Gives this output:

    string(32) "var( --wp--preset--color--base )"

    If that doesn’t help to resolve your issue, it would be helpful if you could provide more context, such as a bigger piece of the relevant code. Thanks!

    Thread Starter Gyurmey

    (@gyurmey)

    Hi @zoonini

    Thank you for the response. However, this function doesn’t work.
    As for the context, I’m just trying to get data from global styles when I change color in settings (screenshot).

    Moderator Kathryn Presner

    (@zoonini)

    Hi @gyurmey could you please copy a larger chunk of your code (including the function provided above) into a Pastebin or between code tags here in the forum? That way I can ask Justin to have a look to try to see why the code may not be working, since he tested it several times on his end and it worked well.

    Could you also please let me know:

    – What version of WordPress are you using?
    – Does the issue occur when all plugins are deactivated?
    – Is the issue present with Twenty Twenty-Two?
    – Is the standalone Gutenberg plugin active, and if so, what version is it?

    Thread Starter Gyurmey

    (@gyurmey)

    @zoonini, sorry for the delay.

    This is my code snippet.

    Input:

    <style>
          .content {
            background-color: <?php echo (wp_get_global_styles(array('color', 'background'))); ?>;
          }
    
          <?php echo var_dump(wp_get_global_styles(array('color', 'background'))); ?>
    </style>

    Output:

    <style>
          .content {
            background-color: var:preset|color|luminous-vivid-amber;
          }
    
          string(37) "var:preset|color|luminous-vivid-amber"
    </style>

    The problem occurs when the colors are based on CSS variables. When it gets a color from the colors picker, that’s fine.

    – What version of WordPress are you using?
    6.0.2 and 6.1-alpha-54226

    – Does the issue occur when all plugins are deactivated?
    Yes.

    – Is the issue present with Twenty Twenty-Two?
    Yes, and any other block theme.

    – Is the standalone Gutenberg plugin active, and if so, what version is it?
    Yes (14.1.1 and Gutenberg Nightly).

    • This reply was modified 2 years, 2 months ago by Gyurmey.
    • This reply was modified 2 years, 2 months ago by Gyurmey.
    Moderator Kathryn Presner

    (@zoonini)

    Hi @gyurmey – thanks for the larger piece of code as well as your testing specs.

    Justin took a look and created this function for you. Let us know if this does the trick!

    https://gist.github.com/justintadlock/ff27b00de0b28726b3336cde8d01806b

    Thread Starter Gyurmey

    (@gyurmey)

    @zoonini, it works! Thank you very much for help.

    Moderator Kathryn Presner

    (@zoonini)

    @gyurmey Awesome! You’re very welcome, and I’ve passed along your thanks to Justin as well. ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Access to global styles’ is closed to new replies.