Skip to content

Instantly share code, notes, and snippets.

@andandehei
Last active January 3, 2016 14:19
Show Gist options
  • Save andandehei/8475323 to your computer and use it in GitHub Desktop.
Save andandehei/8475323 to your computer and use it in GitHub Desktop.
wordpress教程:怎么再wordpress编辑器里添加新的按钮
<? php
//1.在当前主题的functions.php文件中粘贴以下代码:
//初始化时执行ad1_button函数
add_action('init', 'ad1_button');
function ad1_button() {
//判断用户是否有编辑文章和页面的权限
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_plugin' );
add_filter( 'mce_buttons', 'register_button' );
}
}
//2.添加上述过滤器中的函数:
function register_button( $buttons ) {
array_push( $buttons, "|", "ad1" ); //添加 一个ad1按钮
//array_push( $buttons, "|", "mylink" ); //添加一个mylink按钮
return $buttons;
}
function add_plugin( $plugin_array ) {
$plugin_array['ad1'] = get_bloginfo( 'template_url' ) . '/js/ad1.js'; //ad1按钮的js路径
//$plugin_array['mylink'] = get_bloginfo( 'template_url' ) . '/js/mylink.js'; //mylink按钮的js路径
return $plugin_array;
}
//3.引入第2步中的js文件,将以下代码粘贴到ad1.js文件中,路径请自行设置:
function() {
tinymce.create('tinymce.plugins.ad1', { //注意这里有个 ad1
init : function(ed, url) {
ed.addButton('ad1', { //注意这一行有一个 ad1
title : '谷歌广告',
image : url+'/google.png', //注意图片的路径 url是当前js的路径
onclick : function() {
ed.selection.setContent('[ad1]'); //这里是你要插入到编辑器的内容,你可以直接写上广告代码
}
});
},
createControl : function(n, cm) {
return null;
},
});
tinymce.PluginManager.add('ad1', tinymce.plugins.ad1); //注意这里有两个 ad1
})();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment