Update: Automatically create media_buttons for shortcode selection

Adding this snippet to the functions.php of your wordpress theme will add a select menu with an automatically generated list of your shortcodes. For everyone that is going to say hey wait a minute you posted this snippet before! Yes this is true but I added a few updates to this version.

 

The old version of this snippet is located here (Add custom media_buttons for shortcode selection ) I made a few changes to this version.

  • 1.) You don’t need to add shortcode, the snippet will generate the list for you.
  • 2.) you will notice the comment “enter names of shortcode to exclude bellow” this will let you exclude any shortcodes you don’t want clients to see.
  • 3.) I fixed the pesky javascript issue that would only let you you add shortcode when using the html editor.

This could still use some work ill keep posting the updates. Enjoy!

add_action('media_buttons','add_sc_select',11);
function add_sc_select(){
global $shortcode_tags;
/* ------------------------------------- */
/* enter names of shortcode to exclude bellow */
/* ------------------------------------- */
$exclude = array("wp_caption", "embed");
echo '&nbsp;<select id="sc_select"><option>Shortcode</option>';
foreach ($shortcode_tags as $key => $val){
if(!in_array($key,$exclude)){
$shortcodes_list .= '<option value="['.$key.'][/'.$key.']">'.$key.'</option>';
}
}
echo $shortcodes_list;
echo '</select>';
}
add_action('admin_head', 'button_js');
function button_js() {
echo '<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("#sc_select").change(function() {
send_to_editor(jQuery("#sc_select :selected").val());
return false;
});
});
</script>';
}

Leave a comment