Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress visual editor tinymce how to preserve custom attributes

I am writing a WordPress plugin that adds buttons to the tinymce editor in WP. I am adding custom attributes to DOM elements like data-initial="3" .. but WordPress does not save them. Even if I try to write the custom attributes in the html editor when I switch to the visual editor and back the custom attributes are not there anymore.

Any solutions to make WP preserve custom attributes ?

Thanks!

like image 354
digitalzoomstudio Avatar asked Oct 21 '25 02:10

digitalzoomstudio


1 Answers

You need to add your custom attributes to valid_elements in the tinymce init

valid_elements: "@[id|class|title|style|data-initial|...]," + //Your attributes HERE!!!
        "a[name|href|target|title]," +
        "#p,-ol,-ul,-li,br,img[src],-sub,-sup,-b,-i," +
        "-span,hr",

in order to keep them in the editor - otherwise the tinymce cleanup functionality will remove them (because they are not valid).

like image 87
Thariama Avatar answered Oct 24 '25 20:10

Thariama



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!