【问题标题】:Shortcode Dropdown WordPress - Text Editor OnlyShortcode Dropdown WordPress - 仅限文本编辑器
【发布时间】:2017-07-01 05:00:14
【问题描述】:

我正在尝试在添加媒体按钮旁边的 WordPress 页面编辑器中的纯文本编辑器中添加一个简码下拉列表。 我见过很多与下拉菜单相关的问题,但它们都是专门针对 tinyMce 编辑器的,不是纯文本编辑器

我的 WordPress 网站上的可视化编辑器已被禁用,但我仍然希望用户能够看到所有可用的短代码。

任何帮助将不胜感激。

【问题讨论】:

    标签: javascript php jquery wordpress


    【解决方案1】:

    看起来Quicktags API 只接受按钮,但我们可以用一些jQuery 欺骗系统it does all sort of things ;)

    短代码下拉菜单是从var data 构建的,它的行为应该调整onchange

    add_action( 'admin_print_footer_scripts', 'quicktags_so_42200158' );
    
    function quicktags_so_42200158() {
        if ( wp_script_is( 'quicktags' ) ) {
            wp_enqueue_script('jquery');
            ?>
            <script>
            /* Button name and callback will be replaced */
            QTags.addButton( 'dummy_button', 'Dummy button', function(){} );
    
            jQuery(window).load( function() {
                jQ = jQuery;
    
                /* Build dropdown - http://stackoverflow.com/a/4814600 */
                var data = {
                    '-': 'Select shortcode',
                    'video': 'Video',
                    'audio': 'Audio'
                }
                var s = jQ('<select />');
                s.attr('id','my-shortcodes');
                for(var val in data) {
                    jQ('<option />', {value: val, text: data[val]}).appendTo(s);
                }
    
                /* Change 'Dummy button' for dropdown */
                jQ('#qt_content_dummy_button')[0].outerHTML = s[0].outerHTML;
    
                /* What will be inserted on HTML editor */
                jQ('#my-shortcodes').on('change', function(){
                    var sc = '[' + jQ(this).val() + ']';
                    QTags.insertContent(sc);
                });
            });
            </script>
            <?php
        }
    }
    

    参考A Deeper Look Into the WordPress Text Editor

    【讨论】:

    • 很棒的答案谢谢!!我们最终选择了另一条路线,但绝对是我将来会提到的!
    猜你喜欢
    • 1970-01-01
    • 2021-07-09
    • 2012-11-08
    • 2014-03-26
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 2016-10-21
    • 1970-01-01
    相关资源
    最近更新 更多