Support » Fixing WordPress » Javascript event in widget

  • Resolved hungpham

    (@hungpham)


    So in a text widget, I inserted a character, or an image. Then inserted a javascript event such as:

    <a href="#" onclick="function_name()"><img class="alignnone size-full wp-image-696" src="https://domains/wp-content/uploads/2019/08/social-media.png" alt="social-media" width="10" height="20" /></a>

    It works as expected. However, if I went to another area, then came back to this widget, open in text mode (not visual mode), then the onclick part disappeared already, only this left:

    <a href="#"><img class="alignnone size-full wp-image-696" src="https://domains/wp-content/uploads/2019/08/social-media.png" alt="social-media" width="10" height="20" /></a>

    Am I missing some setting to keep that?
    Please advice. Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    WP strips out unrecognized attributes in post content. You could use the “wp_kses_allowed_html” filter to add onclick as an allowed attribute for anchor tags.

    Alternately, don’t use the onclick attribute at all. Instead add an event listener to the element from your JS code.

    Thread Starter hungpham

    (@hungpham)

    Yes, I just learned about the event listener. It seems to be better. Thanks 🙂

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Javascript event in widget’ is closed to new replies.