简体   繁体   English

Wordpress / TinyMCE-挂钩/过滤器以在网址中添加Target =“ _ blank”

[英]Wordpress/TinyMCE - Hook/Filter to add Target =“_blank” in a url

I am trying it since last 6 hours and nothing seems to work for me. 自最近6个小时以来,我一直在尝试,似乎没有任何效果。 I want to add target="_blank" on click of a button inside Wordpress editor -TinyMce. 我想在Wordpress编辑器-TinyMce中单击按钮时添加target =“ _ blank”。

Here is what I did till now : 这是我到目前为止所做的:

Ref : https://wordpress.stackexchange.com/questions/141344/add-button-in-tinymce-editor-to-insert-text 参考https : //wordpress.stackexchange.com/questions/141344/add-button-in-tinymce-editor-to-insert-text

functions.php 的functions.php

    add_action( 'init', 'droid_buttons' );
function droid_buttons() {
    add_filter( "mce_external_plugins", "droid_add_buttons" );
    add_filter( 'mce_buttons', 'droid_register_buttons' );
}

function droid_add_buttons( $plugin_array ) {
    $plugin_array['droid'] = get_template_directory_uri() . '/js/text-button.js';
    echo get_template_directory_uri();
    return $plugin_array;
}

function droid_register_buttons( $buttons ) {
    array_push( $buttons, 'droid_title'); // droid_title
    return $buttons;
}

text-button.js 文本button.js

    (function() {
    tinymce.create('tinymce.plugins.Droid', {
    init : function(editor, url) {
    editor.addButton('droid_title', {
    title : 'Droid Title',
    cmd : 'droid_title',
    image : ''
    });

    editor.addCommand('droid_title', function() {
    var selected_text = editor.selection.getContent();
    var element = "http://www.google.com";
    var return_text = "";
    return_text = '<a href="'+element+'" class="droid_title" target="_blank">' + selected_text + '</span>';
    editor.execCommand('mceInsertContent', 0, return_text);
    });

    },

    });

    // Register plugin
    tinymce.PluginManager.add( 'droid', tinymce.plugins.Droid );
})();

So after adding above code in my functions.php file and text-button.js file, it adds a button in wordpress editor to add target="_blank" in on top of inserted link in selected text. 因此,在我的functions.php文件和text-button.js文件中添加以上代码后,它在wordpress编辑器中添加了一个按钮 ,以在所选文本的插入链接顶部添加target =“ _ blank”

when I am inserting an url after clicking insert/edit link in editor, its showing as it is : 当我在编辑器中单击“ 插入/编辑”链接后插入网址时,其显示为:

在单击定义的按钮之前

But after clicking the button..the url is changing to www.google.com as given in code. 但是,点击按钮后, 网址已更改为 www.google.com,如代码中所给。

在此处输入图片说明

I tried to figure out exactly what should I give as my element variable in js file but not able to figure out. 我试图弄清楚应该怎么给我作为js文件中的元素变量,但无法弄清楚。

I went through the solution given here : 我经历了这里给出的解决方案:

WordPress hook / filter to process link inside a post WordPress挂钩/过滤器来处理帖子中的链接

and here : 和这里 :

How to set target attribute in tinyMCE 如何在tinyMCE中设置目标属性

but not able to figure out the issue. 但无法找出问题所在。 Any help would be really appreciable. 任何帮助都是非常可观的。 Thanks so much in advance. 非常感谢。

As per the documentation of the plugin you will need to add a callback function in the addButton event. 根据插件的文档 ,您将需要在addButton事件中添加一个回调函数。 In this call back you should be accomplish what you are wanted. 在此回叫中,您应该完成所需要的。 Update: Please make sure you have the correct path for the js file in the functions.php file. 更新:请确保您在functions.php文件中具有js文件的正确路径。 It seems that in my case it will be $plugin_array['droid'] = get_template_directory_uri() . '/assets/js/text-button.js'; 在我看来,这将是$plugin_array['droid'] = get_template_directory_uri() . '/assets/js/text-button.js'; $plugin_array['droid'] = get_template_directory_uri() . '/assets/js/text-button.js'; You have missed assets directory may be? 您可能错过了资产目录吗?

Try like this: 尝试这样:

(function() {
        tinymce.create('tinymce.plugins.Droid', {
        init : function(editor, url) {
        editor.addButton('droid_title', {
        title : 'Droid Title',
        cmd : 'droid_title',
        image : '',
        onclick: function() {
            var selected_text = editor.selection.getContent();
            var element = "http://www.google.com";
            var return_text = "";
            return_text = '<a href="'+element+'" class="droid_title"  target="_blank">' + selected_text + '</span>';
            editor.insertContent(return_text);
           }
        });

Working example demo 工作示例演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM