How to enable the "onclick" object in WordPress HTML

I am trying to add an "onclick" object to a page in single user (that is, not multi user) WordPress that fires an event. Code:

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

      

When trying to save the code, WordPress removes the onclick object, leaving:

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

      

A user on another forum suggested that this limitation should only apply to multi-user users who are not super admins. Again, this communicates with one administrator.

It is clear that WordPress removes "onclick" from HTML to prevent malicious code. However, does anyone know how to solve this?

Thank.

+3


source to share


3 answers


You can solve this by changing your anchor tag to a button and adding a script. For more information see this link: Wordpress TinyMCE Strips OnClick and OnChange (jQuery required) .



+1


source


Allowing, I'm assuming you want to allow the onclick attribute. You want to be careful with this, because changing the valid tags does it for all of your users.

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

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 with $ allowedposttags only to see if that works for you. As per this other stackexchange post, you only need valid tags if you need it for comments or possibly unregistered users, but when I did something like this in the past, I needed them both to work.

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

+1


source


It looks like with the current Wordpress (I'm at 4.9.4) TinyMCE is doing the filtering directly on the editor screen, not when the form is submitted. The allowed tags and the allowed posts don't seem to be important, so the solution above doesn't solve the problem for me.

The method I developed uses a filter tiny_mce_before_init

to change the allowed tags in TinyMCE. The trick is to add a parameter extended_valid_elements

with updated versions of the items allowed for a

.

First look at http://archive.tinymce.com/wiki.php/Configuration3x:valid_elements to find the current value for a

which is now

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

      

And add to the end of this attribute onclick

:

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

      

Then use this in your filter function like:

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 to your functions.php

file in Wordpress. You can see it in action by switching the text and visual appearance on the edit page. Without an expanded list, onclick goes away. He stays with him.

0


source







All Articles