Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Include "onclick" Object in WordPress HTML

Tags:

html

wordpress

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:

<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:

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

A user on another forum suggested that 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?

Thanks.

like image 897
Catalyx Avatar asked Oct 19 '25 03:10

Catalyx


2 Answers

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.

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

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

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

And add to the end of that the onclick attribute:

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.php file 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.

like image 87
vick Avatar answered Oct 21 '25 16:10

vick


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).

like image 25
Anoop Asok Avatar answered Oct 21 '25 16:10

Anoop Asok