There are several posts here on stackoverflow describing the same issue, however none of the suggested solutions have worked for me.
When wrapping an anchor tag around a block element, such as a div
, the TinyMCE Visual Editor changes this anchor tag and moves it "up". I've shown this behaviour beneath:
"Wished" DOM:
<a href="#">
<div></div>
</a>
TinyMCe changes that to the following:
<a href="#"></a>
<div></div>
What i've tried so far:
The below is expected by several answers to work, however it doesn't in my case. According to this answer it requires to turn autop
off, which isn't a valid option in my case.
add_filter('tiny_mce_before_init', 'modify_valid_children', 99);
function modify_valid_children($settings){
$settings['valid_children']="+a[div|p|ul|ol|li|h1|span|h2|h3|h4|h5|h5|h6]";
return $settings;
}
I've also found the documentation from TinyMCE themselves, stating the following should do the job:
tinyMCE.init({
valid_children: '+a[div|p|h1|h2|h3|img]';
});
However, I'm not quite sure where to insert this, as it has to be in scope of the TinyMCE .js file, while not being overwritten in case of a core update from wordpress. Any ideas?
I even tried editing the core file of wordpress (wp-includes/js/tinymce/tinymce.min.js
) and adding the above directly in here, without any luck either.
The above source from wordpress.stackexchange explains it thoroughly in detail, but I was hoping that some new options/information had come forth since 2016.