2013-04-30 119 views
0

我一直試圖讓這個工作好幾個小時,現在它讓我的頭變得嘎嘎作響。在TinyMCE中添加自定義下拉框在WordPress中

我幾乎得到它,但不幸的是,當沒有內容被包裝它顯示短代碼的標題。

例如我的一個標題是簡碼1,當其周圍的一些文字包裹它應該顯示[thirdwidth]的內容放在這裏[/ thirdwidth]

但是,如果我不把它包起來,它顯示的標題。

簡碼1 thirdwidth] [/ thirdwidth]

這是爲什麼這樣做呢?

這裏是關於PHP前面的代碼:

function register_customcode_dropdown($buttons) { 
    array_push($buttons, "Shortcodes"); 
    return $buttons; 
} 

function add_customcode_dropdown($plugin_array) { 
    $plugin_array['Shortcodes'] = get_template_directory_uri() . '/style/js/TinyMCE_js.js'; 
    return $plugin_array; 
} 

function customcode_dropdown() { 

    if (! current_user_can('edit_posts') && ! current_user_can('edit_pages')) { 
     return; 
    } 

    if (get_user_option('rich_editing') == 'true') { 
     add_filter('mce_external_plugins', 'add_customcode_dropdown'); 
     add_filter('mce_buttons', 'register_customcode_dropdown'); 
    } 

} 

add_action('init', 'customcode_dropdown'); 

這裏是TinyMCE_js.js文件

(function() { 


    tinymce.create('tinymce.plugins.Shortcodes', { 

     init : function(ed, url) { 
     }, 
     createControl : function(n, cm) { 

      if(n=='Shortcodes'){ 
       var mlb = cm.createListBox('Shortcodes', { 
        title : 'Shortcodes', 
        onselect : function(v) { 
         if(tinyMCE.activeEditor.selection.getContent() == ''){ 
          tinyMCE.activeEditor.selection.setContent(v) 
         } 


         if(v == 'shortcode 1'){ 

          selected = tinyMCE.activeEditor.selection.getContent(); 

          if(selected){ 
           //If text is selected when button is clicked 
           //Wrap shortcode around it. 
           content = '[thirdwidth]'+selected+'[/thirdwidth]'; 
          }else{ 
           content = '[thirdwidth][/thirdwidth]'; 
          } 

          tinymce.execCommand('mceInsertContent', false, content); 

         } 

         if(v == 'shortcode 2'){ 

          selected = tinyMCE.activeEditor.selection.getContent(); 

          if(selected){ 
           //If text is selected when button is clicked 
           //Wrap shortcode around it. 
           content = '[12]'+selected+'[/12]'; 
          }else{ 
           content = '[12][/12]'; 
          } 

          tinymce.execCommand('mceInsertContent', false, content); 

         } 


        } 
       }); 


       // Add some menu items 
       var my_shortcodes = ['shortcode 1','shortcode 2']; 

       for(var i in my_shortcodes) 
        mlb.add(my_shortcodes[i],my_shortcodes[i]); 

       return mlb; 
      } 
      return null; 
     } 


    }); 
    tinymce.PluginManager.add('Shortcodes', tinymce.plugins.Shortcodes); 
})(); 

感謝

回答

1

發現這個問題,這是我離開這個在:

if(tinyMCE.activeEditor.selection.getContent() == ''){ 
          tinyMCE.activeEditor.selection.setContent(v) 
         } 
相關問題