Skip to main content
added 489 characters in body; added 10 characters in body
Source Link
TheDeadMedic
  • 36.6k
  • 9
  • 68
  • 102

Based on my own experience, I've used a combination of method 1 & 2 - the architecture and footer scripts of 1, and the 'look-ahead' technique of 2.

For the look-ahead though, I use regex in place of stripos; personal preference, faster, and can check for 'malformed' shortcode;

preg_match( '#\[ *shortcode([^\]])*\]#i', $content );

If you're concerned about authors using do_shortcode manually, I would opt to instruct them to use an action calluse an action call enqueue your pre-registered style manually.

UPDATE: For the lazy author who never RTFM, which they can use inoutput a template beforemessage to highlight the get_header, something like;error of their ways ;)

function my_shortcode_stylemy_shortcode()
{
    wp_enqueue_stylestatic $enqueued;
    if (... ! isset( $enqueued );
} )
add_action        $enqueued = wp_style_is( 'my_shortcode_style''my_style', 'my_shortcode_style''done' ); // cache it so we don't repeat if called over and over
    
    // do shortcode
    $output = '';
    
    if ( ! $enqueued )
        // you can output the message on first occurence only by wrapping it in the previous if
        $output .= <<<HTML
<p>Attention! You must enqueue the shortcode stylesheet yourself if calling <code>do_shortcode()</code> directly!</p>
<p>Use <code>wp_enqueue_style( 'my_style' );</code> before your <code>get_header()</code> call inside your template.</p>
HTML;

    return $output;
}

Using actions over a direct function call is the WordPress way, and you won't need to be grepping the hell out of output buffers to look for shortcode calls ;)

Based on my own experience, I've used a combination of method 1 & 2 - the architecture and footer scripts of 1, and the 'look-ahead' technique of 2.

For the look-ahead though, I use regex in place of stripos; personal preference, faster, and can check for 'malformed' shortcode;

preg_match( '#\[ *shortcode([^\]])*\]#i', $content );

If you're concerned about authors using do_shortcode manually, I would opt to instruct them to use an action call, which they can use in a template before get_header, something like;

function my_shortcode_style()
{
    wp_enqueue_style(...);
}
add_action( 'my_shortcode_style', 'my_shortcode_style' );

Using actions over a direct function call is the WordPress way, and you won't need to be grepping the hell out of output buffers to look for shortcode calls ;)

Based on my own experience, I've used a combination of method 1 & 2 - the architecture and footer scripts of 1, and the 'look-ahead' technique of 2.

For the look-ahead though, I use regex in place of stripos; personal preference, faster, and can check for 'malformed' shortcode;

preg_match( '#\[ *shortcode([^\]])*\]#i', $content );

If you're concerned about authors using do_shortcode manually, I would opt to instruct them to use an action call enqueue your pre-registered style manually.

UPDATE: For the lazy author who never RTFM, output a message to highlight the error of their ways ;)

function my_shortcode()
{
    static $enqueued;
    if ( ! isset( $enqueued ) )
        $enqueued = wp_style_is( 'my_style', 'done' ); // cache it so we don't repeat if called over and over
    
    // do shortcode
    $output = '';
    
    if ( ! $enqueued )
        // you can output the message on first occurence only by wrapping it in the previous if
        $output .= <<<HTML
<p>Attention! You must enqueue the shortcode stylesheet yourself if calling <code>do_shortcode()</code> directly!</p>
<p>Use <code>wp_enqueue_style( 'my_style' );</code> before your <code>get_header()</code> call inside your template.</p>
HTML;

    return $output;
}
Source Link
TheDeadMedic
  • 36.6k
  • 9
  • 68
  • 102

Based on my own experience, I've used a combination of method 1 & 2 - the architecture and footer scripts of 1, and the 'look-ahead' technique of 2.

For the look-ahead though, I use regex in place of stripos; personal preference, faster, and can check for 'malformed' shortcode;

preg_match( '#\[ *shortcode([^\]])*\]#i', $content );

If you're concerned about authors using do_shortcode manually, I would opt to instruct them to use an action call, which they can use in a template before get_header, something like;

function my_shortcode_style()
{
    wp_enqueue_style(...);
}
add_action( 'my_shortcode_style', 'my_shortcode_style' );

Using actions over a direct function call is the WordPress way, and you won't need to be grepping the hell out of output buffers to look for shortcode calls ;)