如何在WP中的TinyMCE中添加多个按钮?

发布于 2024-10-28 20:04:24 字数 1727 浏览 3 评论 0原文

我遵循了 Nettuts 上的教程,了解如何向 TinyMCE 添加自定义按钮(http ://net.tutsplus.com/tutorials/wordpress/wordpress-shortcodes-the-right-way/

它工作得很好,但我想添加很多按钮,我想知道是否有一种聪明的方法无需一遍又一遍地重复所有代码即可完成此操作。

这是我用于添加按钮的代码:

add_shortcode("quote", "quote");  
function quote( $atts, $content = null ) {  
    return '<div class="right text">"'.$content.'"</div>';  
}

add_action('init', 'add_button');  
function add_button() {  
   if ( current_user_can('edit_posts') &&  current_user_can('edit_pages') )  
   {  
     add_filter('mce_external_plugins', 'add_plugin');  
     add_filter('mce_buttons_3', 'register_button');  
   }  
}  
function register_button($buttons) {  
   array_push($buttons, "quote");  
   return $buttons;  
}  
function add_plugin($plugin_array) {  
   $plugin_array['quote'] = get_bloginfo('template_url').'/js/customcodes.js';  
   return $plugin_array;  
}  

然后我使用以下代码创建一个 customcodes.js 文件:

(function() {  
    tinymce.create('tinymce.plugins.quote', {  
        init : function(ed, url) {  
            ed.addButton('quote', {  
                title : 'Add a Quote',  
                image : url+'/image.png',  
                onclick : function() {  
                     ed.selection.setContent('[quote]' + ed.selection.getContent() + '[/quote]');  

                }  
            });  
        },  
        createControl : function(n, cm) {  
            return null;  
        },  
    });  
    tinymce.PluginManager.add('quote', tinymce.plugins.quote);  
})();

那么,我怎样才能添加多个按钮,而不必为每个新按钮执行所有这些代码?

谢谢:)塞巴斯蒂安

I've followed a tutorial on Nettuts on how to add a custom button to TinyMCE (http://net.tutsplus.com/tutorials/wordpress/wordpress-shortcodes-the-right-way/)

It works great and all, but i want to add many buttons and i wonder if there's a smart way to do this without having to duplicate all the code over and over.

Here's the code i use for adding a button:

add_shortcode("quote", "quote");  
function quote( $atts, $content = null ) {  
    return '<div class="right text">"'.$content.'"</div>';  
}

add_action('init', 'add_button');  
function add_button() {  
   if ( current_user_can('edit_posts') &&  current_user_can('edit_pages') )  
   {  
     add_filter('mce_external_plugins', 'add_plugin');  
     add_filter('mce_buttons_3', 'register_button');  
   }  
}  
function register_button($buttons) {  
   array_push($buttons, "quote");  
   return $buttons;  
}  
function add_plugin($plugin_array) {  
   $plugin_array['quote'] = get_bloginfo('template_url').'/js/customcodes.js';  
   return $plugin_array;  
}  

And then i create a customcodes.js file with this code in:

(function() {  
    tinymce.create('tinymce.plugins.quote', {  
        init : function(ed, url) {  
            ed.addButton('quote', {  
                title : 'Add a Quote',  
                image : url+'/image.png',  
                onclick : function() {  
                     ed.selection.setContent('[quote]' + ed.selection.getContent() + '[/quote]');  

                }  
            });  
        },  
        createControl : function(n, cm) {  
            return null;  
        },  
    });  
    tinymce.PluginManager.add('quote', tinymce.plugins.quote);  
})();

So again, how can i add multiple buttons without having to do all this code for each new button?

Thanks :) Sebastian

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

雨轻弹 2024-11-04 20:04:25

如果我正确理解您的问题,您想添加更多按钮,而不必复制 register_button($buttons)add_plugin($plugin_array) 函数吗?

我知道这是一个老问题,但是有一种方法可以在不重复功能的情况下做到这一点。

只需进入您的 customcodes.js 并在 init : function(ed, url) 创建新按钮,就像您对第一个按钮所做的那样,因此它看起来像这样:

init : function(ed, url) {  
             /* your original button */
            ed.addButton('quote', {  
            title : 'Add a Quote',  
            image : url+'/image.png',  
            onclick : function() {  
                 ed.selection.setContent('[quote]' + ed.selection.getContent() + '[/quote]'); 
            }  
        }); 
            /* your second button */
            ed.addButton('singlequote', {  
            title : 'Add a Single Quote',  
            image : url+'/image.png',  
            onclick : function() {  
                 ed.selection.setContent('[singlequote]' + ed.selection.getContent() + '[/singlequote]'); 
            }  
        }); 
} 

依此类推,按钮数量一样多根据您的要求。

之后,只需返回 register_button($buttons) 函数并更新 array_push()

因此,当您只有一个按钮来添加时,它看起来像这样:

function register_button($buttons) {  
array_push($buttons, "quote");  
return $buttons;  }

现在您创建了新按钮,该功能将如下所示。

function register_button($buttons) {  
array_push($buttons, "quote", "singlequote");  
return $buttons; }  

依此类推,具体取决于您添加了多少个新按钮。

您无需复制功能,或添加新操作和过滤器即可将新按钮添加到tinyMCE。

您只需在tinyMCE插件中创建新按钮,并列出您在array_push()中创建的按钮的名称。

也许您不知道array_push()接受多个推送值。
这是 php.net 上的文档

If i am understanding your question correctly you want to add to more buttons without having to make duplicates of the register_button($buttons) and add_plugin($plugin_array) functions?

I know this is an old question, but there is a way to do it without duplicating the functions.

Just go into your customcodes.js and in the init : function(ed, url) create new buttons just like you did the first one, so it would look as such:

init : function(ed, url) {  
             /* your original button */
            ed.addButton('quote', {  
            title : 'Add a Quote',  
            image : url+'/image.png',  
            onclick : function() {  
                 ed.selection.setContent('[quote]' + ed.selection.getContent() + '[/quote]'); 
            }  
        }); 
            /* your second button */
            ed.addButton('singlequote', {  
            title : 'Add a Single Quote',  
            image : url+'/image.png',  
            onclick : function() {  
                 ed.selection.setContent('[singlequote]' + ed.selection.getContent() + '[/singlequote]'); 
            }  
        }); 
} 

And so forth, as many buttons as you require.

After that just head back to your register_button($buttons) function and update the array_push().

So whilst, when you had only one button to add it looked like this:

function register_button($buttons) {  
array_push($buttons, "quote");  
return $buttons;  }

Now that you created your new buttons this function would look like this.

function register_button($buttons) {  
array_push($buttons, "quote", "singlequote");  
return $buttons; }  

And so on, depending how many new buttons youve added.

You do not need to duplicate functions, or add new actions and filters to add new buttons to your tinyMCE.

You just create the new buttons inside your tinyMCE plug-in and list the names of the buttons youve created inside the array_push().

Perhaps you werent aware that array_push() accepts multiple push values.
Here is its documentation on php.net

夢归不見 2024-11-04 20:04:25

在第 3 步修改 php 以按下第二个按钮:

//Step 3: Register Our Button
function register_button($buttons) {  
   array_push($buttons, "BOUTON1");  
   array_push($buttons, "BOUTON2");  
   return $buttons;  
}

向该 BOUTON2 添加特定通道:

//Step 4: Register Our TinyMCE Plugin
    function add_plugin($plugin_array) {  
       $plugin_array['BOUTON1'] = '/yourpathtojs/bouton1.js';  
       $plugin_array['BOUTON2'] = '/yourpathtojs/bouton2.js';  
       return $plugin_array;  
    } 

然后,每个按钮都有不同的文件,请查看使用PLUG1 和 BOUTON1,它比在 nettuts 上更好,因为它们不区分术语“引用”:

bouton1.js :

(function() {  
    tinymce.create('tinymce.plugins.PLUG1', {  
        init : function(ed, url) {  
            ed.addButton('BOUTON1', {  
                title : 'You', image : url+'/image.png',  
                onclick : function() { ed.selection.setContent('[thumb from="youtube" code="'+ed.selection.getContent()+'"]'); }  
            });  
        },  
        createControl : function(n, cm) {  
            return null;  
        },  
    });  
    tinymce.PluginManager.add('BOUTON1', tinymce.plugins.PLUG1);  
})();

bouton2.js :

(function() {  
        tinymce.create('tinymce.plugins.PLUG2', {  
            init : function(ed, url) {  
                ed.addButton('BOUTON2', {  
                    title : 'Vim', image : url+'/image.png',  
                    onclick : function() { ed.selection.setContent('[thumb from="vimeo" code="'+ed.selection.getContent()+'"]'); }  
                });  
            },  
            createControl : function(n, cm) {  
                return null;  
            },  
        });  
        tinymce.PluginManager.add('BOUTON2', tinymce.plugins.PLUG2);  
    })();

Modify the php at step 3 to push your second buttons :

//Step 3: Register Our Button
function register_button($buttons) {  
   array_push($buttons, "BOUTON1");  
   array_push($buttons, "BOUTON2");  
   return $buttons;  
}

Add a specific pass to that BOUTON2 :

//Step 4: Register Our TinyMCE Plugin
    function add_plugin($plugin_array) {  
       $plugin_array['BOUTON1'] = '/yourpathtojs/bouton1.js';  
       $plugin_array['BOUTON2'] = '/yourpathtojs/bouton2.js';  
       return $plugin_array;  
    } 

Then you have distinct files for each, look on the use of PLUG1 and BOUTON1, its better that on nettuts because they dont made distinction with the term 'quote' :

bouton1.js :

(function() {  
    tinymce.create('tinymce.plugins.PLUG1', {  
        init : function(ed, url) {  
            ed.addButton('BOUTON1', {  
                title : 'You', image : url+'/image.png',  
                onclick : function() { ed.selection.setContent('[thumb from="youtube" code="'+ed.selection.getContent()+'"]'); }  
            });  
        },  
        createControl : function(n, cm) {  
            return null;  
        },  
    });  
    tinymce.PluginManager.add('BOUTON1', tinymce.plugins.PLUG1);  
})();

bouton2.js :

(function() {  
        tinymce.create('tinymce.plugins.PLUG2', {  
            init : function(ed, url) {  
                ed.addButton('BOUTON2', {  
                    title : 'Vim', image : url+'/image.png',  
                    onclick : function() { ed.selection.setContent('[thumb from="vimeo" code="'+ed.selection.getContent()+'"]'); }  
                });  
            },  
            createControl : function(n, cm) {  
                return null;  
            },  
        });  
        tinymce.PluginManager.add('BOUTON2', tinymce.plugins.PLUG2);  
    })();
倾听心声的旋律 2024-11-04 20:04:25

除了在现有函数中添加额外的按钮代码之外,我认为没有办法完成您正在尝试的操作。

不幸的是,这是添加按钮的代码,因此如果您想添加另一个按钮,则必须再次添加代码。

如果您想要添加的按钮几乎在所有方面都相似,那么您可以通过执行 foreach {} 后跟 switch(){case '':} 你输入数据的地方,但除非你所有的按钮都做同样的事情,否则这似乎有点多余。

如果您想要做的只是保持 function.php 文件整洁,那么我建议将每个按钮放在一个单独的 .php 文件中,该文件的名称与主函数相同,位于名为 inc 的文件夹中或包含在模板中,然后包含它们像这样:

$temp_path = 'http//www.yourdomain.com/wp-content/theme/yourtheme/includes/';


include $temp_path.'file1.php';
include $temp_path.'file2.php';

我使用 temp_path 变量,因为由于某种原因 bloginfo()get_bloginfo() 似乎在函数文件中不起作用。

顺便说一句,即使它仅供个人使用,也请尝试使用更独特的函数名称,quote 可以是任何东西,tinymce_quote_button 绝对就是这样。这可以避免以后潜在的函数名称冲突。

Apart from maybe adding the extra button code inside your already existing functions, I don't think there's a way to do what you're trying.

Unfortunately, that's the code to add a button, so if you want to add another button you've got to add the code again.

If the buttons you wanted to add were similar in almost every way, you could maybe get away with doing a foreach {} followed by a switch(){case '':} where you feed the data through but unless all your buttons do the same thing this seems a bit redundant.

If all you're trying to do is keep your function.php file tidy then I suggest putting each button in a separate .php file named the same as the main function, in a folder called inc or includes inside your template, then include them like so:

$temp_path = 'http//www.yourdomain.com/wp-content/theme/yourtheme/includes/';


include $temp_path.'file1.php';
include $temp_path.'file2.php';

I'm using a temp_path variable because for some reason bloginfo() and get_bloginfo() just don't seem to work in the functions file.

On a side note, even though it's just for personal use, try to use much more unique function names, quote could be anything, tinymce_quote_button that's definitely what it is. This avoids potential function name clashes later.

錯遇了你 2024-11-04 20:04:25

如果所有按钮都是相关的,并且您想一次添加它们(即您不需要选择添加哪些按钮,您可以在 init 调用中复制 ed.addButton 调用。这将是有意义的将每个 addbutton 调用封装到它自己的函数中,

(function() {  
    function addQuoteButton(ed, url){
        ed.addButton('quote', {  
            title : 'Add a Quote',  
            image : url+'/image.png',  
            onclick : function() {  
                 ed.selection.setContent('[quote]' + ed.selection.getContent() + '[/quote]');  
            }  
        });  
    }
    function addOtherButton(ed, url){
        // functionality to add another button.
    }
    tinymce.create('tinymce.plugins.quote', {  
        init : function(ed, url) {
            addQuoteButton(ed, url);
            addOtherButton(ed,url);  
        },  
        createControl : function(n, cm) {  
            return null;  
        },  
    });  
    tinymce.PluginManager.add('quote', tinymce.plugins.quote);  
})();

为了进一步分解,您可以将 add*Button 函数拆分到它们自己的文件中(如@DouglasMarken 建议的那样)。

If all the buttons are related and you want to add them all at once (ie you don't need to pick and choose which buttons get added, you could just duplicate the ed.addButton calls in the init call. It would make sense to encapsulate each addbutton call into it's own function,

(function() {  
    function addQuoteButton(ed, url){
        ed.addButton('quote', {  
            title : 'Add a Quote',  
            image : url+'/image.png',  
            onclick : function() {  
                 ed.selection.setContent('[quote]' + ed.selection.getContent() + '[/quote]');  
            }  
        });  
    }
    function addOtherButton(ed, url){
        // functionality to add another button.
    }
    tinymce.create('tinymce.plugins.quote', {  
        init : function(ed, url) {
            addQuoteButton(ed, url);
            addOtherButton(ed,url);  
        },  
        createControl : function(n, cm) {  
            return null;  
        },  
    });  
    tinymce.PluginManager.add('quote', tinymce.plugins.quote);  
})();

to get further decomposition, you could then split the add*Button functions out into their own files (as suggested by @DouglasMarken).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文