WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 20 months ago

Last modified 16 months ago

#19806 closed enhancement (duplicate)

do_allowed_shortcodes to allow processing of limited list of shortcodes

Reported by: beezeee Owned by: beezeee
Milestone: Priority: normal
Severity: normal Version:
Component: General Keywords: has-patch
Focuses: Cc:

Description

We have a front facing content editor and want to enable our users to use a specific set of shortcodes, without exposing all of the sites shortcodes to them.

Here's the function we use for this. It is passed an array of allowed shortcodes, and any other shortcodes are stripped from the content.

function do_allowed_shortcodes($content, $shortcodes=array())
{
    //if no allowed shortcodes are provided, strip all shortcodes and return content
	if (empty($shortcodes))
	{
		return(strip_shortcodes($content));
	}
    //foreach allowed shortcode provided, build the necessary regex to temporarily change [shortcode] to {shortcode} and then back
	foreach ($shortcodes as $shortcode)
	{
		$allowed_matches[] = '/\[('.$shortcode.'[^\]]*)\]/';
		$restore_allowed_matches[] = '/\{('.$shortcode.'[^\}]*)\}/';
	}
    //change safe shortcodes from [shortcode] to {shortcode}
	$safe_content = preg_replace($allowed_matches, '{$1}', $content);
    //strip remaining [shortcodes]
	strip_shortcodes($safe_content);
    //change {shortcodes} back to [shortcodes] and return result of do_shortcode() on that content
	$trimmed_content = preg_replace($restore_allowed_matches, '[$1]', $safe_content);
	return do_shortcode($trimmed_content);
}

Seems like this would be a nice addition to core functionality.

Change History (3)

comment:1 in reply to: ↑ description @beezeee4 years ago

realized there was a typo, here's the correct code

function do_allowed_shortcodes($content, $shortcodes=array())
{
    //if no allowed shortcodes are provided, strip all shortcodes and return content
	if (empty($shortcodes))
	{
		return(strip_shortcodes($content));
	}
    //foreach allowed shortcode provided, build the necessary regex to temporarily change [shortcode] to {shortcode} and then back
	foreach ($shortcodes as $shortcode)
	{
		$allowed_matches[] = '/\[('.$shortcode.'[^\]]*)\]/';
		$restore_allowed_matches[] = '/\{('.$shortcode.'[^\}]*)\}/';
	}
    //change safe shortcodes from [shortcode] to {shortcode}
	$safe_content = preg_replace($allowed_matches, '{$1}', $content);
    //strip remaining [shortcodes]
	$clean_content = strip_shortcodes($safe_content);
    //change {shortcodes} back to [shortcodes] and return result of do_shortcode() on that content
	$trimmed_content = preg_replace($restore_allowed_matches, '[$1]', $clean_content);
	return do_shortcode($trimmed_content);
}

comment:2 @nacin20 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Hi beezeee, sorry you never received a reply here. This suggestion has also been tracked in a few other tickets. I think the most recent one is #25435.

comment:3 @Viper007Bond16 months ago

Here's a much cleaner way of processing only specific shortcodes in a string:

http://www.viper007bond.com/2009/11/22/wordpress-code-earlier-shortcodes/

Note: See TracTickets for help on using tickets.