• Resolved Yiorgos Theo

    (@hellas1)


    Hi,

    This is a very nice plugin!!!

    I have a small problem:
    I have some custom shortcodes.
    I have added a dropdown list to insert the shortcodes into the editor using the following code:

    function add_sc_select(){
        global $shortcode_tags;
         /* ------------------------------------- */
         /* enter names of shortcode to exclude bellow */
         /* ------------------------------------- */
        $exclude = array("caption", "gallery", "playlist", "embed", "wp_caption", "audio", "video", "acf");
        echo ' <select id="sc_select"><option>Shortcodes</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('media_buttons','add_sc_select',11);
    
    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());
                   jQuery("#sc_select").val("Shortcodes");
                   return false;
               });
            });
            </script>';
    }
    add_action('admin_head', 'button_js');

    The above code works fine with the wordpress editor when editing posts and pages but not with the widget editor.
    The shortcode select list shows up but there’s something wrong with the jQuery and it doesn’t insert the selected shortcodes.

    Any suggertions?

    https://wordpress.org/plugins/black-studio-tinymce-widget/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Marco Chiesi

    (@marcochiesi)

    Your code has 2 issues:
    1. It does not handle correctly multiple editor instances in the same page (this may happens for widgets), because it uses ID selectors
    2. It does not set the wpActiveEditor global var used by send_to_editor.

    The following modified code works fine both on pages/posts and widgets:

    function add_sc_select(){
        global $shortcode_tags;
         /* ------------------------------------- */
         /* enter names of shortcode to exclude bellow */
         /* ------------------------------------- */
        $exclude = array("caption", "gallery", "playlist", "embed", "wp_caption", "audio", "video", "acf");
        echo ' <select id="sc_select" class="sc_select"><option>Shortcodes</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('media_buttons','add_sc_select',11);
    
    function button_js() {
            echo '<script type="text/javascript">
            jQuery(document).ready(function(){
               jQuery(".sc_select").change(function() {
    			   wpActiveEditor = jQuery( "textarea.mceEditor,textarea.wp-editor-area", jQuery(this).closest(".wp-media-buttons").parent().parent() ).attr( "id" );
                   send_to_editor(jQuery(":selected", this).val());
                   jQuery(this).val("Shortcodes");
                   return false;
               });
            });
            </script>';
    }
    add_action('admin_head', 'button_js');
    Thread Starter Yiorgos Theo

    (@hellas1)

    Works great! Grazie Marco!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Shortcodes issue’ is closed to new replies.