Make WordPress Core

Opened 13 years ago

Closed 11 years ago

Last modified 10 years ago

#19806 closed enhancement (duplicate)

do_allowed_shortcodes to allow processing of limited list of shortcodes

Reported by: beezeee's profile beezeee Owned by: beezeee's profile 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)

#1 in reply to: ↑ description @beezeee
13 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);
}

#2 @nacin
11 years 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.

#3 @Viper007Bond
10 years 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.