[wp-trac] [WordPress Trac] #19806: do_allowed_shortcodes to allow processing of limited list of shortcodes

WordPress Trac wp-trac at lists.automattic.com
Wed Jan 11 16:09:56 UTC 2012


#19806: do_allowed_shortcodes to allow processing of limited list of shortcodes
-------------------------+-----------------------------
 Reporter:  beezeee      |      Owner:  beezeee
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  General      |    Version:
 Severity:  normal       |   Keywords:  has-patch
-------------------------+-----------------------------
 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.

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/19806>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list