简体   繁体   English

Wordpress功能在自定义TinyMCE编辑器按钮内不起作用

[英]Wordpress functions doesn't work inside custom TinyMCE editor button

I created simple TinyMCE plugin like below. 我创建了一个简单的TinyMCE插件,如下所示。 Basically it's a new button in TinyMCE editor that'll pop up lightbox (thick box) when pressed. 基本上,这是TinyMCE编辑器中的一个新按钮,按下后会弹出灯箱(厚框)。

// create plugin
tinymce.create('tinymce.plugins.callthickbox', {  
    init : function(ed, url) {  
        ed.addButton('callthickbox', {  
            title   : 'This is Thick Box',  
            cmd     : 'showthickbox'
        });
        ed.addCommand('showthickbox', function() {
            ed.execCommand('mceInsertContent', false, callthatthickbox(url,null));
        });

    //blah blah blah the rest...
});
tinymce.PluginManager.add('callthickbox', tinymce.plugins.callthickbox);

and call it via: 并通过以下方式调用:

// call the PHP form
function callthatthickbox(url) {
    tb_show( "This is Thick Box", url + '/thickbox-form.php', false);   
}

The thick box is popped up and the PHP file (the thick box's content) is called alright, but all the WP functions don't work inside those PHP file such as wp_list_categories() or bloginfo('url') . 将会弹出厚框,并且PHP文件(厚框的内容)也可以正常使用,但是所有WP函数在这些PHP文件中都无法正常工作,例如wp_list_categories()bloginfo('url')

Apparently the PHP file lose its right to access WP functions. 显然,PHP文件失去了访问WP函数的权利。 I could try passing variables via URL but it doesn't seem to be elegant solution, especially if I want to pass an array. 我可以尝试通过URL传递变量,但这似乎不是一个很好的解决方案,尤其是当我想传递数组时。

Anybody has better solution? 有人有更好的解决方案吗?

many thanks before! 非常感谢!

EDIT: I tried to load wp-load.php inside PHP file and it works, but isn't that a bit overkill? 编辑:我试图将wp-load.php加载到PHP文件中,并且它可以工作,但是那不是一点点大刀阔斧吗? any thought? 任何想法?

check out the load process for wordpress. 查看Wordpress的加载过程。 if you are calling the functions from your plugin file they won't be loaded yet. 如果您是从插件文件中调用函数,则不会加载它们。

What you will need to do is 您需要做的是

add_action('init', 'your function callback name');

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

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