1

I added some shortcode after the content using filter, like this:

add_filter( 'the_content', 'image_posts' );

but now that I run has_shortcode in another function, this returns false even though the shortcode is there (which was added using the above filter.

add_action( 'wp_head', 'gallery_style');
function gallery_style(){
  if ( has_shortcode( $content, 'gallery' ) ) {
     // enqueue style
     // it fails for posts which add gallery using the_content
  }

how can I test the has_shortcode for contents added using add_filter?

  • Can you post the full function/code where your has_content conditional check exists? Is it in another the_content filter? – Ian Apr 5 '17 at 1:40
  • no it is a add_action( 'wp_head', I want to add the style where post has gallery shortcode. – Tammy Apr 5 '17 at 1:44
  • wp_head action hook runs before the_content filter hook. – Fayaz Apr 5 '17 at 1:47
  • @Fayaz can the order be changed? – Tammy Apr 5 '17 at 1:50
  • Nope. wp_head loads the HTML content within <head> part of HTML, and the_content filters content within <body>, so that can't be changed. However, if you edit your question and provide the full CODE of those functions with explanation of what you want to do, we may suggest a different approach that works. – Fayaz Apr 5 '17 at 1:53
0

You can actually enqueue styles and scripts from any hook. If wp_head() had already fired, then the styles and/or scripts will be enqueued into the footer.

//* Add filter to the_content
add_filter( 'the_content', 'wpse_262484_the_content' );
function wpse_262484_the_content( $content ) {

  //* Maybe do something useful with $content

  //* Enqueue style if the content has gallery shortcode
  //* Since wp_head() has already fired, this will be output in the footer
  if ( has_shortcode( $content, 'gallery' ) ) {
    wp_enqueue_style( 'wpse-262484', PATH_TO . 'style.css' );
  }
  return $content;
}
  • the statement inside has_shortcode never gets executed. I added a shortcode to the content using the_content filter and now no matter where I run has_shortcode it fails – Tammy Apr 5 '17 at 2:35
  • Then the content doesn't have the gallery shortcode. Make sure this filter is run after the one inserting the shortcode. – Nathan Johnson Apr 5 '17 at 4:48
0

For this, you'll actually want to utilize the wp_enqueue_scripts action instead of wp_head.

wp_enqueue_scripts is technically the proper way to add scripts and styles to your site. This action allows us to both register scripts and styles for later use (which is what we want here) and allows us to output scripts and styles to our page without hard coding. Also this matters because wp_head runs before the_content which means that you won't be able to modify the head once your code gets to the_content.

First let's register our style using the wp_enqueue_scripts action.

/**
 * Register the gallery stylesheet for later use if our content contains the gallery shortcode.
 */
function enqueue_gallery_style(){
        wp_register_style('gallery_style', get_stylesheet_directory_uri() . '/assets/css/gallery.css');
}
add_action('wp_enqueue_scripts', 'enqueue_gallery_style'); // register with the wp_enqueue_scripts action

We register the script using the wp_register_style function. In this case we'll make use of the first two parameters. The first is the name we want to give to the style so we can use it later. You can name it whatever you want. Here I've named it "gallery_style". The second parameter is the url path to the stylesheet in your theme or plugin (make sure to update based on your specific path). Here are more details on wp_register_style

Now we can add another the_content filter in addition to your image_posts filter to perform the shortcode conditional check. Inside the check, if we find it, then we run the wp_enqueue_style function to enqueue our gallery style and add it to that specific page.

/**
 * This function is used to check the_content to see if it contains any shortcodes
 *
 * @param $content
 *
 * @return string
 */

function shortcode_check( $content ) {

    if ( has_shortcode( $content, 'gallery' ) ) {
        wp_enqueue_style( 'gallery_style' ); // this is the same name we gave our gallery style when we registered it.
    }

    return $content; // Make sure you still return your content or else nothing will display.
}
add_filter( 'the_content', 'shortcode_check' );

More info on wp_enqueue_style

Method if you are trying to add admin-generated styles (eg. from customizer or other settings) that won't have a physical file.

If you need to output styles that are generated via the admin (ie, colors) you can utilize the wp_add_inline_style function.

This function will allow you to add on styles to a registered stylesheet. So in the case of our already registered gallery.css file above, we can add wp_add_inline_style( 'gallery_style', $user_css ); and it will add that as an inline style immediately after registered stylesheet.

Here $user_css would be the styles as a string without the <style> wrapper.

So in your case, you could register a stylesheet for gallery with some base styles and then use this function to add styles that would override those base styles.

function shortcode_check( $content ) {
    if ( has_shortcode( $content, 'gallery' ) ) {
        wp_enqueue_style('gallery_style');
        wp_add_inline_style( 'gallery_style', $user_css ); // this will add our inline styles. Make sure to sanitize!!!
    }

    return $content; // Make sure you still return your content or else nothing will display.
}

More on wp_add_inline_style

  • thanks for in depth answer, I have some constraints and must use wp_head to insert the style – Tammy Apr 5 '17 at 2:16
  • Can you elaborate on that? Why exactly do you have to use wp_head? There may be another way around the constraints ;) – Ian Apr 5 '17 at 2:17
  • user sets custom colors – Tammy Apr 5 '17 at 2:18
  • Ok, so then the user-set styles are actually dynamic and not in a file, is that correct? – Ian Apr 5 '17 at 2:21
  • yes, that is correct. I tried this method and I had some issues so I had to go with wp_head option – Tammy Apr 5 '17 at 2:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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