如何在 WordPress HTML 中包含“onclick”对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26333000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:51:28  来源:igfitidea点击:

How to Include "onclick" Object in WordPress HTML

htmlwordpress

提问by Catalyx

I'm using attempting to add an "onclick" object to a page in a singlesite (i.e. rather than multisite) WordPress that triggers an event. The code is:

我正在尝试将“onclick”对象添加到触发事件的单站点(即而不是多站点)WordPress 中的页面。代码是:

<a href="#" onclick="_speakpipe_open_widget(); return false;">Send a voice message</a>

When attempting to save the code, WordPress strips the onclick object leaving:

尝试保存代码时,WordPress 会删除 onclick 对象,并留下:

<a href="#">Send a voice message</a>

A user on another forum suggestedthat this restriction should only apply to multisite non-superadmin users. Again, this is a siglesite with only one admin user.

另一个论坛上的用户建议此限制应仅适用于多站点非超级管理员用户。同样,这是一个只有一个管理员用户的单一站点。

It is understood that WordPress removes "onclick" from HTML to prevent malicious code. Still, does anyone know how to resolve this?

据了解,WordPress 从 HTML 中删除了“onclick”以防止恶意代码。不过,有谁知道如何解决这个问题?

Thanks.

谢谢。

回答by Anoop Asok

You can solve this by changing the anchor tag into button and adding a script. For more info please refer to this link: Wordpress TinyMCE Strips OnClick & OnChange (need jQuery).

您可以通过将锚标记更改为按钮并添加脚本来解决此问题。有关更多信息,请参阅此链接:Wordpress TinyMCE Strips OnClick & OnChange (need jQuery)

回答by Arty-chan

By resolving, I'm assuming you mean to allow the onclick attribute. You will want to be careful with this, because modifying the allowed tags does this for all your users.

通过解析,我假设您的意思是允许 onclick 属性。您需要小心这一点,因为修改允许的标签会为您的所有用户执行此操作。

You can modify the list of allowed tags and attributes, by adding this to your functions.php file:

您可以修改允许的标签和属性列表,方法是将其添加到您的 functions.php 文件中:

function allow_onclick_content() {
  global $allowedposttags, $allowedtags;
  $newattribute = "onclick";

  $allowedposttags["a"][$newattribute] = true;
  $allowedtags["a"][$newattribute] = true; //unnecessary?
}
add_action( 'init', 'allow_onclick_content' );

I suggest trying it with only $allowedposttags first to see if that works for you. According to this other stackexchange post, you should only need allowedtags if you need it for comments or possibly non-logged-in users, but when I did something similar in the past, I needed both of them to work.

我建议先只用 $allowedposttags 尝试一下,看看它是否适合你。根据this otherstackexchange post,如果您需要用于评论或可能未登录的用户,您应该只需要 allowedtags ,但是当我过去做了类似的事情时,我需要它们都工作。

On a side note, if you want a list of all already allowed tags and attributes, look inside your /wp-includes/kses.php file.

附带说明一下,如果您想要所有已经允许的标签和属性的列表,请查看您的/wp-includes/kses.php 文件

回答by vick

It appears that with current Wordpress (I'm on 4.9.4), TinyMCE does the filtering directly on the editor screen, not when the form is submitted. The allowedtags and allowedposttags don't seem to matter, so the solution above does not solve the problem for me.

看来,对于当前的 Wordpress(我使用的是 4.9.4),TinyMCE 直接在编辑器屏幕上进行过滤,而不是在提交表单时进行过滤。allowedtags 和 allowedposttags 似乎无关紧要,所以上面的解决方案对我来说并没有解决问题。

The method I have developed uses the tiny_mce_before_initfilter to alter the allowed tags within TinyMCE. The trick is to add the extended_valid_elementssetting with the updated versions of the elements allowed for a.

我开发的方法使用tiny_mce_before_init过滤器来改变 TinyMCE 中允许的标签。诀窍是extended_valid_elements使用允许的元素的更新版本添加设置a

First, look in the page http://archive.tinymce.com/wiki.php/Configuration3x:valid_elementsto find the current value for a, which right now is

首先,在页面http://archive.tinymce.com/wiki.php/Configuration3x:valid_elements中查找 的当前值a,现在是

a[rel|rev|charset|hreflang|tabindex|accesskey|type|name|href|target|title|class|onfocus|onblur]

And add to the end of that the onclickattribute:

并在该onclick属性的末尾添加:

a[rel|rev|charset|hreflang|tabindex|accesskey|type|name|href|target|title|class|onfocus|onblur|onclick]

Then use that in the filter function like this:

然后在过滤器函数中使用它,如下所示:

function allow_button_onclick_mce($settings) {
  $settings['extended_valid_elements'] =  "a[rel|rev|charset|hreflang|tabindex|accesskey|type|name|href|target|title|class|onfocus|onblur|onclick]";
  return $settings;
}
add_filter('tiny_mce_before_init', 'allow_button_onclick_mce');

which you install in your functions.phpfile in Wordpress. You can see it in action by toggling the text and visual view on the edit page. Without the extended list, the onclick goes away. With it, it remains.

您将其安装functions.php在 Wordpress的文件中。您可以通过在编辑页面上切换文本和视觉视图来查看它的运行情况。如果没有扩展列表,onclick 就会消失。有了它,它仍然存在。