When dealing with a widget that is a custom post type, is there a way to detect when that widget is displayed, then check its post meta for any custom css and enqueue it in the head of the site?

This may sound a bit confusing so I will explain a little further.

I have a plugin that has custom post type 'widget'.

When I create a post from that custom post type widget, I can then load that post content from its widget, selecting the post content to display.

If my post has custom css that is stored in the post meta, the widget will display the css, but it is enqueued in the footer.

Is there a way to check for that widget, then check for it's post meta custom css, then enqueue that css in the head section of the website?

This is specifically for a Visual Composer plugin, but I see every author that enables Visual Composer post content to be displayed via a widget to enqueue the css in the footer. This gives errors in accordance to WC3 standards.

So I figured I would ask for for some expert help because obviously the authors collectively have not found a way to do so, but I just believe this has to be possible.

I believe it is possible for a couple reasons.

  1. I see caching plugins grab any plugins css, in the footer, inline or enqueued and then enqueue it in the head of the site. So I know that such methods can be done maybe in the shutdown priority, etc. ... somewhere detecting right before page render if there is custom css enqueued in the footer, then grabbing that and forcing it to display in the head.
  2. This plugin Google Fonts Optimizer does just that, in a way, but I'm not sure how to tweak is methodology to do this for my plugin. I'm knew to the methods used by this plugin and php classes. But Google Fonts Optimizer will check for all the enqueued Google Fonts, then optimize them all into one request and strip it and force it to show in the head. @ https://github.com/sigginet/google-webfont-optimizer/blob/master/google-webfont-optimizer.php

Here's a gist of the css file I am trying to get to do this, but it might give the total look needed into the file @ https://gist.github.com/NoahjChampion/9329f7301eef9f2619b4

  • Your question starts bad because "widget" it's something that aleready exists in WordPress. It's objects those are place in sidebar. Maybe you can edit the question in writing "widget-CPT" when you speak about your custom type and "widget-WP" when it's the one which goes in a sidebar
    – mmm
    Commented Mar 24, 2016 at 8:20

1 Answer 1


I think this is what you are looking for

You need to get the custom post meta and add it to the wp_add_inline_style( $handle, $data );, I advise using the "style" default handle. The position of your custom css will depend on the hook of your stylesheet.

  • Sorry for the delay here. I tried this out and it doesn't work, but I think it is probably do to the stylesheet hook, as you said. But I will bookmark this in case when I get a developer to take a closer look this help become a solution. Commented Apr 9, 2016 at 13:11
  • By default stylesheets are placed inside the header, as its css this should work in any case. I advise you to get a professional to look over it as the reason for this could be pretty much anything.
    – db306
    Commented Apr 13, 2016 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.