WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#11948 closed defect (bug) (invalid)

Shortcode parser doesn't support hyphen in shortcode name

Reported by: ukdmbfan Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.9.1
Component: Shortcodes Keywords: reporter-feedback
Focuses: Cc:

Description

I tried creating a Shortcode with a hyphen (-) in the name, namely "contact-us", but the parser was not recognising the Shortcode. Removing the hyphen from the name made it work.

It may be that the parser is not supposed to support hyphens in Shortcode names, however the documentation suggests that it does by using examples with hyphens in them ("my-shortcode", for example).

You should be able to easily reproduce this by creating a Shortcode with a hyphen in the name. Code I was using is below, removing the hyphen from the Shortcode name in the first parameter of the add_shortcode hook (and subsequently in the associated call in the blog post) returned the correct result. When the hyphen was present, the Shortcode tag was parsed and removed from the post, but was not replaced with the correct output and the associated function itself wasn't called at all.

Using WPMU 2.9.1.1.

function contact_us_func($atts, $content = null) {
    return '123';
}

/* NOT WORKING */

add_shortcode('contact-us', 'contact_us_func');
// [contact-us]

/* WORKING */

add_shortcode('contactus', 'contact_us_func');
// [contactus]

Change History (7)

comment:1 scribu4 years ago

  • Component changed from General to Shortcodes
  • Keywords needs-patch added; shortcode api wpmu removed
  • Milestone changed from Unassigned to Future Release

comment:2 greenshady4 years ago

Works for me on regular WordPress. I don't have an MU install set up to test if it's different there.

comment:3 TobiasBg4 years ago

  • Keywords hypen dash removed
  • Priority changed from low to normal
  • Severity changed from minor to normal

I use a shortcode with a hyphen in my plugin and it works just fine.

The only thing that breaks it, if I register a shortcode that is just the part until the hyphen before registering the hyphened shortcode.

In my plugin:
add_shortcode( 'table-info', ...)
add_shortcode( 'table', ...)
works fine, while
add_shortcode( 'table', ...)
add_shortcode( 'table-info', ...)
does not (well I admit I haven't tested again with WP 2.9 which has a new regex, if I remember correctly.

So, could you check that in your case you (nor any other plugin) is registering a shortcode [contact /]?

comment:4 wpmuguru4 years ago

This worked for me in MU 2.9.1:

function ra_shortcode_test( $atts, $content = null ) {
        return 'hello world';
}
add_shortcode( 'ra-test-code', 'ra_shortcode_test' );

comment:5 sivel4 years ago

  • Keywords reporter-feedback added; needs-patch removed

comment:6 wpmuguru4 years ago

  • Resolution set to invalid
  • Status changed from new to closed

Since my post above, I have created several shortcodes and used hyphens in them all.

comment:7 scribu4 years ago

  • Milestone Future Release deleted
Note: See TracTickets for help on using tickets.