Make WordPress Core

Opened 10 years ago

Last modified 7 years ago

#31093 new enhancement

Make $tag argument optional for has_shortcode()

Reported by: danielbachhuber's profile danielbachhuber Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Shortcodes Keywords: has-patch dev-feedback has-unit-tests
Focuses: Cc:

Description

Use case: I'd like to see if my string has any shortcodes.

Attachments (5)

31093.patch (1.7 KB) - added by tyxla 10 years ago.
trac_31093_modified_has_shortocde.diff (1.6 KB) - added by ashfame 10 years ago.
Make has_shortcode() work without a specific shortcode to search for
trac_31093_has_shortcode_array_support.diff (2.5 KB) - added by ashfame 10 years ago.
make has_shortcode() support checking against array of shortcode tags along with an AND/OR relation parameter
31093.4.diff (7.0 KB) - added by birgire 7 years ago.
31093.5.diff (2.3 KB) - added by birgire 7 years ago.

Download all attachments as: .zip

Change History (12)

#1 follow-up: @danielbachhuber
10 years ago

And / or ability to supply an array of shortcodes to check

@tyxla
10 years ago

#2 @tyxla
10 years ago

  • Keywords has-patch dev-feedback 2nd-opinion added; needs-patch removed

Not sure if that makes sense, or if it is necessary at all.

My point is, if a function would check for any shortcodes within the content, it would make more sense if a new function contains_shortcodes() is created for that purpose. Mainly because has_shortcode() would do totally different things in the two cases (with and without a parameter). And the function name would make more sense IMHO.

Also, I believe a function for checking whether there are any shortcodes is not necessary. It is quick enough to check the original string against the result of strip_shortcodes() over the string. Something like this:

<?php
$original_content = 'some content that might have [shortcodes]';
$stripped_content = strip_shortcodes($original_content);

if ( $original_content === $stripped_content ) {
    echo 'There are no shortcodes within the content.';
} else {
    echo 'There are one or more shortcodes within the content.';
}
?>

Of course, a built-in function for this can turn out handy for developers. Attaching a patch here ( https://core.trac.wordpress.org/attachment/ticket/31093/31093.patch ) in case the core committer team decide to include it. Tests included.

#3 in reply to: ↑ 1 @tyxla
10 years ago

Replying to danielbachhuber:

And / or ability to supply an array of shortcodes to check

A little in doubt here - how would you expect this to work? If you pass an array with 2 shortcodes, and one of them is found, would it return true or false? Perhaps a third parameter to specify whether to use OR or AND condition?

#4 @ashfame
10 years ago

IMO, has_shortcode() should be able to check whether string has a shortcode in it or not. Checking for a specific one is more of an add-on to it. Adding my patch - trac_31093_modified_has_shortocde.diff

Not sure if that function should support an array along with a relation to compare, because one can do that with multiple calls to it, but I believe the concern would be not to do a regex search multiple times. Thoughts?

@ashfame
10 years ago

Make has_shortcode() work without a specific shortcode to search for

@ashfame
10 years ago

make has_shortcode() support checking against array of shortcode tags along with an AND/OR relation parameter

#5 @danielbachhuber
10 years ago

It is quick enough to check the original string against the result of strip_shortcodes() over the string.

Clever suggestion — didn't think of that. I pulled and am using the regex from has_shortcode()

A little in doubt here - how would you expect this to work? If you pass an array with 2 shortcodes, and one of them is found, would it return true or false?

Return true (aka OR condition)

Not sure if that function should support an array along with a relation to compare, because one can do that with multiple calls to it, but I believe the concern would be not to do a regex search multiple times.

Yes, my use case for checking multiple shortcodes at once is so I have one regex call instead of many.

Regardless, any of these patches would need unit tests.

#6 @miqrogroove
9 years ago

  • Keywords needs-unit-tests added; 2nd-opinion removed

@birgire
7 years ago

@birgire
7 years ago

#7 @birgire
7 years ago

  • Keywords has-unit-tests added; needs-unit-tests removed

I remember assuming this would work:

has_shortcode( $content )

to check for any shortcode in a content, so I support this feature.

I would prefer to use has_shortcode() though, instead of a new function.

31093.5.diff adds a support to check if there's any shortcode in a text, by using the clever idea from @tyxla, where I simplified it with:

if ( empty( $tag ) ) {
    return strip_shortcodes( $content ) !== $content;
}

Here we're talking about this kind of usage:

has_shortcode( $content );
has_shortcode( $content, 'gallery' );

In 31093.4.diff I updated the patch from @ashfame, in addition with the update from 31093.4.diff, with various tests. Here we're talking about this kind of usage:

has_shortcode( $content );
has_shortcode( $content, 'gallery' );
has_shortcode( $content, array( 'gallery', 'playlist' ) );  // default OR
has_shortcode( $content, array( 'gallery', 'playlist' ), 'OR' );
has_shortcode( $content, array( 'gallery', 'playlist' ), 'AND' );

Note: See TracTickets for help on using tickets.