TinyMCE自定义简码按钮不显示

时间:2018-09-13 09:30:39

标签: javascript php wordpress tinymce

我试图在TinyMCE编辑器中注册一个按钮(我不在乎新的Gutenberg或其他任何编辑器)。无论出于什么原因,它都不会显示,不是控制台错误,但要注意的一件事是它没有将图像拖入开发人员控制台源中。您知道代码有什么问题吗?

functions.php文件

function gw_cta($atts, $content = null) {
    extract(shortcode_atts(array(
        "icon" => '',
        "text" => '',
        "classes" => '',
        "popup" => '',
        "link" => ''
    ), $atts));
    if ($popup == "yes") { $popup = 'id="cta-popup"'; } else { $popup = '';}
    return '<a href="'.$link.'" class="cta '.$classes.'" '.$popup.'><span class="fa '.$icon.'"></span>'.$text.'</a>';
} add_shortcode("gw-cta", "gw_cta");

add_action('init', 'gw_cta_add_button');

function gw_cta_add_button() {
    if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') && get_user_option('rich_editing') == 'true') {
        return;
    }
    add_filter("mce_external_plugins", "gw_cta_register_tinymce_plugin_callback");
    add_filter('mce_buttons', 'gw_cta_add_tinymce_button_callback');
}

function gw_cta_register_tinymce_plugin_callback($plugin_array) {
    $url = get_bloginfo('template_url').'/GW-Addon/js/tinymce.js';
    $plugin_array['gw_cta_custom_button'] = $url;
    return $plugin_array;
}

function gw_cta_add_tinymce_button_callback($buttons) {
    $buttons[] = "gw-cta";
    return $buttons;
}

tinymce.js文件

jQuery(document).ready(function($) {
tinymce.create('tinymce.plugins.gw_cta_custom_plugin', {
    init : function(ed, url) {
        ed.addCommand('gw_cta_command', function() {
            content =  '[gw-cta icon="" text="" link="" popup="" classes=""]'+tinyMCE.activeEditor.selection.getContent();+'[/gw-cta]';
            tinymce.execCommand('mceInsertContent', false, content);
        });
        ed.addButton('gw-cta', {
            title : 'Insert shortcode',
            cmd : 'gw_cta_command',
            image: url + '/gw-cta.png'
        });
    },
});
tinymce.PluginManager.add('w-cta', tinymce.plugins.gw_cta_custom_plugin);
});

0 个答案:

没有答案