Skip to content

Plugin handbook shortcode example code is incorrect #17

@atachibana

Description

@atachibana

This issue was moved from HelpHub issue #314 reported by oskar-fagerfjall.

Issue Description

In the Shortcodes with Parameters chapter in the plugin handbook, the complete example at the end is incorrect. It works fine for shortcodes without content, but when adding content that content is echoed twice.

Context

The problematic code is the following

    if ( ! is_null( $content ) ) {
        // secure output by executing the_content filter hook on $content
        $o .= apply_filters( 'the_content', $content );
 
        // run shortcode parser recursively
        $o .= do_shortcode( $content );
    }

This code appends the content variable to the output twice, once without running the shortcode parser recursively, and once without properly securing the output.

Steps to reproduce:

  1. Copy the example code into a plugin
  2. Activate the plugin
  3. Make a new post containing [wporg title="A"][wporg title="B"]test[/wporg][/wporg]
  4. Preview the post

Expected Result:

The shortcode should result in

<div><h2>A</h2><div><h2>B</h2>test</div></div>

Actual Result:

The shortcode results in

<div><h2>A</h2><div><h2>B</h2></div>test<div><h2>B</h2></div>test</div>[/wporg]

Metadata

Metadata

Assignees

Labels

developer documentation (DevHub)Improvements or additions to developer documentationpluginsIssues for Plugin Developer Handbook

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions