添加“插入按钮”在工具栏Tinymce编辑器上

发布于 2025-02-05 09:06:34 字数 1116 浏览 3 评论 0 原文

我想在工具栏Tinymce编辑器上添加一个“插入按钮”。

我想要的一个示例:

一个我想要的示例

使用工具栏按钮,我会喜欢在我的编辑器中创建一个按钮并更改其样式。 (就像Gutenberg编辑器一样)

gutenberg编辑器

使用Tinymce编辑器,我具有此代码:

function wpc_boutons_tinymce($buttons) {
            $buttons[] = 'underline';
            $buttons[] = 'fontselect';
            $buttons[] = 'fontsizeselect';
            $buttons[] = 'edit-block';
            return $buttons;
        }


        add_filter("mce_buttons_3", "wpc_boutons_tinymce");

            $content = '';
            $editor_id = 'mycustomeditor';
            $settings = array( 
                'wpautop' => false, 
                'media_buttons' => false, 
                'quicktags' => array(
                        'buttons' => 'strong,em,del,ul,ol,li,block,close'
                    ),
            );
            wp_editor( $content, $editor_id, $settings );

我没有找到该怎么做,你能帮我吗?

I want to add an "insert button" on my toolbar tinymce editor.

An example of what I want :

An example of what I want

With the toolbar button, I would like to create a button in my editor and change its style. (Like the gutenberg editor does)

gutenberg editor

To use the tinymce editor, I have this code :

function wpc_boutons_tinymce($buttons) {
            $buttons[] = 'underline';
            $buttons[] = 'fontselect';
            $buttons[] = 'fontsizeselect';
            $buttons[] = 'edit-block';
            return $buttons;
        }


        add_filter("mce_buttons_3", "wpc_boutons_tinymce");

            $content = '';
            $editor_id = 'mycustomeditor';
            $settings = array( 
                'wpautop' => false, 
                'media_buttons' => false, 
                'quicktags' => array(
                        'buttons' => 'strong,em,del,ul,ol,li,block,close'
                    ),
            );
            wp_editor( $content, $editor_id, $settings );

I didn't find how to do, can you help me ?

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

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

发布评论

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

评论(1

桜花祭 2025-02-12 09:06:35

这是我实施的代码,请按照步骤

粘贴主题中的代码 functions.php

add_action( 'init', 'wptuts_buttons' );
function wptuts_buttons() {
    add_filter( "mce_external_plugins", "wptuts_add_buttons" );
    add_filter( 'mce_buttons', 'wptuts_register_buttons' );
}
function wptuts_add_buttons( $plugin_array ) {
    $plugin_array['wptuts'] = get_template_directory_uri() . '/wptuts-editor-buttons/wptuts-plugin.js';
    return $plugin_array;
}
function wptuts_register_buttons( $buttons ) {
    array_push( $buttons, 'dropcap', 'showrecent' ); // dropcap', 'recentposts
    return $buttons;
}

require( 'wptuts-editor-buttons/wptuts.php' );

在您的主题根文件中创建一个文件夹,名为 wptuts-editor buttons
然后在 wptuts-editor buttons 中创建一个名为 wptuts.php 的文件,然后粘贴代码

<?php
 
add_shortcode( 'recent-posts', 'wptuts_recent_posts' );
function wptuts_recent_posts( $atts ) {
    extract( shortcode_atts( array(
        'numbers' => '5',
    ), $atts ) );
    $rposts = new WP_Query( array( 'posts_per_page' => $numbers, 'orderby' => 'date' ) );
    if ( $rposts->have_posts() ) {
        $html = '<h3>Recent Posts</h3><ul class="recent-posts">';
        while( $rposts->have_posts() ) {
            $rposts->the_post();
            $html .= sprintf(
                '<li><a href="%s" title="%s">%s</a></li>',
                get_permalink($rposts->post->ID),
                get_the_title(),
                get_the_title()
            );
        }
        $html .= '</ul>';
    }
    wp_reset_query();
    return $html;
}

另外,您需要在 wptuts-editor buttons &gt中创建一个JS文件。 wptuts-plugin.js 并粘贴代码

(function() {
    tinymce.create('tinymce.plugins.Wptuts', {
        init : function(ed, url) {
            ed.addButton('dropcap', {
                title : 'DropCap',
                cmd : 'dropcap',
                image : url + '/dropcap.jpg'
            });
 
            ed.addButton('showrecent', {
                title : 'Add recent posts shortcode',
                cmd : 'showrecent',
                image : url + '/images.jpg'
            });
 
            ed.addCommand('dropcap', function() {
                var selected_text = ed.selection.getContent();
                var return_text = '';
                return_text = '<span class="dropcap">' + selected_text + '</span>';
                ed.execCommand('mceInsertContent', 0, return_text);
            });
 
            ed.addCommand('showrecent', function() {
                var number = prompt("How many posts you want to show ? "),
                    shortcode;
                if (number !== null) {
                    number = parseInt(number);
                    if (number > 0 && number <= 20) {
                        shortcode = '[recent-post number="' + number + '"/]';
                        ed.execCommand('mceInsertContent', 0, shortcode);
                    }
                    else {
                        alert("The number value is invalid. It should be from 0 to 20.");
                    }
                }
            });
        },
        // ... Hidden code
    });
    // Register plugin
    tinymce.PluginManager.add( 'wptuts', tinymce.plugins.Wptuts );
})();

整个解决方案我遵循了文章

Here is my implemented code Follow the step

Paste the code in your theme functions.php

add_action( 'init', 'wptuts_buttons' );
function wptuts_buttons() {
    add_filter( "mce_external_plugins", "wptuts_add_buttons" );
    add_filter( 'mce_buttons', 'wptuts_register_buttons' );
}
function wptuts_add_buttons( $plugin_array ) {
    $plugin_array['wptuts'] = get_template_directory_uri() . '/wptuts-editor-buttons/wptuts-plugin.js';
    return $plugin_array;
}
function wptuts_register_buttons( $buttons ) {
    array_push( $buttons, 'dropcap', 'showrecent' ); // dropcap', 'recentposts
    return $buttons;
}

require( 'wptuts-editor-buttons/wptuts.php' );

Create a folder in your theme root file named wptuts-editor-buttons
Then create a file in wptuts-editor-buttons named wptuts.php and paste the code

<?php
 
add_shortcode( 'recent-posts', 'wptuts_recent_posts' );
function wptuts_recent_posts( $atts ) {
    extract( shortcode_atts( array(
        'numbers' => '5',
    ), $atts ) );
    $rposts = new WP_Query( array( 'posts_per_page' => $numbers, 'orderby' => 'date' ) );
    if ( $rposts->have_posts() ) {
        $html = '<h3>Recent Posts</h3><ul class="recent-posts">';
        while( $rposts->have_posts() ) {
            $rposts->the_post();
            $html .= sprintf(
                '<li><a href="%s" title="%s">%s</a></li>',
                get_permalink($rposts->post->ID),
                get_the_title(),
                get_the_title()
            );
        }
        $html .= '</ul>';
    }
    wp_reset_query();
    return $html;
}

Also, You need to create a js file in wptuts-editor-buttons > wptuts-plugin.js and paste the code

(function() {
    tinymce.create('tinymce.plugins.Wptuts', {
        init : function(ed, url) {
            ed.addButton('dropcap', {
                title : 'DropCap',
                cmd : 'dropcap',
                image : url + '/dropcap.jpg'
            });
 
            ed.addButton('showrecent', {
                title : 'Add recent posts shortcode',
                cmd : 'showrecent',
                image : url + '/images.jpg'
            });
 
            ed.addCommand('dropcap', function() {
                var selected_text = ed.selection.getContent();
                var return_text = '';
                return_text = '<span class="dropcap">' + selected_text + '</span>';
                ed.execCommand('mceInsertContent', 0, return_text);
            });
 
            ed.addCommand('showrecent', function() {
                var number = prompt("How many posts you want to show ? "),
                    shortcode;
                if (number !== null) {
                    number = parseInt(number);
                    if (number > 0 && number <= 20) {
                        shortcode = '[recent-post number="' + number + '"/]';
                        ed.execCommand('mceInsertContent', 0, shortcode);
                    }
                    else {
                        alert("The number value is invalid. It should be from 0 to 20.");
                    }
                }
            });
        },
        // ... Hidden code
    });
    // Register plugin
    tinymce.PluginManager.add( 'wptuts', tinymce.plugins.Wptuts );
})();

Whole the Solution I followed the article
https://code.tutsplus.com/tutorials/guide-to-creating-your-own-wordpress-editor-buttons--wp-30182

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