I know this sounds complicated, so bear with me.
I'm trying to add certain custom css styles based on the shortcodes(if any) in the current post/page.
function load_shortcode_styles($posts){
//function to check short codes here
//regex to get id from shortcode(e.g [item id=""] )
//get custom post meta from the id that has my custom css
add_action('wp_head', create_function('', 'echo "\n<!-- my style -->\n<style>' . $custom_css . '</style>\n";') );
}
add_filter('the_posts', 'load_shortcode_styles' );
After I open my post pages, the source shows my style being printed twice like:
<!-- my style -->
<style>
</style>
<!-- my style -->
<style>
</style>
For testing purposes I used a hardcoded function to echo a certain string, and it works fine.
e.g. :
function test_print(){
echo "\n<!-- my style -->\n<style>foo bar</style>\n";
}
and replace above action hook with this
add_action('wp_head', 'test_print' );
Somehow, this will print, which is the correct one!
<!-- my style -->
<style>
foo bar
</style>
Does anyone know about this issue? How can I solve this?