4

So I'm using HTML5 on a website and am trying to wrap a div in an anchor tag, which is supposedly acceptable in HTML5 websites. But when I do this...

<a href="#mylink"><div class="some_class">Some content</div></a>

...through the WordPress Tiny MCE editor, I get this output:

<p><a href="#mylink">&nbsp;</a></p>
<a href="#mylink"><div class="some_class">Some content</div></a>
<p><a href="#mylink"></a></p>

All kinds of messed up. If I flip to the visual editor and back to Text in Tiny MCE, it has changed the code to this:

<a href="#mylink"> </a>
<div class="some_class">Some content</div>
&nbsp;

Does anyone know how to force Tiny MCE to allow this? (ideally without resorting to the advanced plugin version of Tiny MCE)

Thanks for any advice.

1

1 Answer 1

1

You need to modify the TinyMCE settings, specifically the valid_children setting. To accomplish this in Wordpress, use the tiny_mce_before_init filter reference.

Something like this (untested):

add_filter('tiny_mce_before_init', 'modify_valid_children');

function modify_valid_children($settings){
    $settings['valid_children']="+a[div|p|ul|ol|li|h1|h2|h3|h4|h5|h5|h6]";
    return $settings;
}

You may be able to use +a[*] to capture all elements, but you'll want to take care to avoid nested anchor tags.

4
  • Thanks Matt but I still can't get this to work. FYI, I had to remove the ) on line 6 - it errored otherwise
    – Xav
    Apr 23, 2013 at 16:52
  • Ah, good catch. Looks like I've been writing too much jQuery lately. :) You should also remove the semi-colon. I've updated my answer to reflect this. Not sure if this will make a difference for you, but I don't suspect it will.
    – Matt
    Apr 25, 2013 at 16:10
  • Setting "valid_elements", "invalid_elements" and "extended_valid_elements" can be done through this filter. from a comment in the core file
    – Ralf912
    Apr 25, 2013 at 21:48
  • 1
    Why don't you use just a <span> or <p> instead of a <div>?
    – Ralf912
    Apr 25, 2013 at 22:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.