【发布时间】:2012-12-03 09:47:56
【问题描述】:
我正在制作一个用于在帖子中添加图像映射的 Wordpress 插件。目前我已经将图像映射实现为自定义帖子类型。但是,我在将它们添加到帖子中时遇到了一些问题。
我在媒体插入/上传窗口中创建了一个名为 Image map 的新选项卡。当您单击选项卡中的图像映射时,会触发此函数:
function insertImageMap() {
tinyMCE.execInstanceCommand('mceInsertContent', false, 'content',
'some text or html code'
);
window.parent.tb_remove();
}
但是,tinyMCE 编辑器中没有添加任何内容。该函数被触发,但即使我使用 tinyMCE.getInstanceById('content'),tinyMCE 编辑器的实例也未定义。有没有办法访问帖子编辑器页面的编辑器?
我在我的插件中包含了 tinyMCE 脚本,但它没有显示为未定义。
我发现一个较旧的问题问同样的问题,但答案不是很有帮助: Custom Wordpress Plugin - How do I insert content from popup on post editor?
【问题讨论】:
-
我认为这是因为弹出窗口实际上是一个 iframe。因此,为了访问
tinyMCE实例,您需要通过window.opener或window.parent来获取它(我不能100% 确定它是否是其中的任何一个或其他)。所以你可以做var instance = window.opener.tinyMCE || window.parent.tinyMCE; instance.execInstanceCommand('mceInsertContent', false, 'content', 'some text or html code');。此外,您可以查看源代码(最有可能是 JS)并查看 WordPress 如何将内容发送到编辑器。 -
其实你试过
window.send_to_editor()吗? -
好吧,脚本实际上并没有在 iframe 中运行。我可以访问 tinyMCE 本身的实例,但不能使用 tinyMCE.getInstanceById() 访问帖子编辑器。
-
但是您说您在媒体上传弹出窗口中添加了一个新选项卡,并且这是在 iframe 中加载的。除非您正在做一些非常奇怪的事情,否则它应该在 iframe 中。请尝试
window.send_to_editor('content to go to editor here')并告诉我它是否有效。 -
啊,是的,我误解了一些东西。我之前测试过 send_to_editor,但它没有定义。 Window.parent.send_to_editor 虽然做到了这一点。当您错过这样的明显解决方案时感觉很棒。谢谢!
标签: javascript wordpress tinymce