【问题标题】:Insert content to Wordpress post editor将内容插入 Wordpress 帖子编辑器
【发布时间】: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.openerwindow.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


【解决方案1】:

在 cmets 中找到解决方案:

function insertImageMap() {
    window.parent.send_to_editor('any text');
    window.parent.tb_remove();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多