Automatic shortcode selection in wordpress


/ Published in: PHP
Save to your folder(s)

Add this snippet to the functions.php of your wordpress theme. This will add a select menu with an automatically generated list of your shortcodes for your pages and posts.


Copy this code and paste it in your HTML
  1. // shortcode button
  2.  
  3. add_action('media_buttons','add_sc_select',11);
  4. function add_sc_select(){
  5. global $shortcode_tags;
  6. /* ------------------------------------- */
  7. /* enter names of shortcode to exclude bellow */
  8. /* ------------------------------------- */
  9. $exclude = array("wp_caption", "embed");
  10. echo '&nbsp;<select id="sc_select"><option>Shortcode</option>';
  11. foreach ($shortcode_tags as $key => $val){
  12. if(!in_array($key,$exclude)){
  13. $shortcodes_list .= '<option value="['.$key.'][/'.$key.']">'.$key.'</option>';
  14. }
  15. }
  16. echo $shortcodes_list;
  17. echo '</select>';
  18. }
  19. add_action('admin_head', 'button_js');
  20. function button_js() {
  21. echo '<script type="text/javascript">
  22. jQuery(document).ready(function(){
  23. jQuery("#sc_select").change(function() {
  24. send_to_editor(jQuery("#sc_select :selected").val());
  25. return false;
  26. });
  27. });
  28. </script>';
  29. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.