Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress removes empty span tag

Tags:

html

wordpress

I use WordPress-editor and I want to display an icon within a "span"-tag like this:

<div id="question1" class="box-around">
   <div class="box-left"><span class="fa fa-search" aria-hidden="true"> </span></div>
   <div class="box-right">
      <h3>Some Heading</h3>
      Some Text
      <span id="question1-answer"> </span>
    </div>
</div>

Whenever I make a change in "visual", it removes the "span"-tag and looks like this:

<div id="question1" class="box-around">
  <div class="box-left"></div>
   <div class="box-right">
      <h3>Some Heading</h3>
       Some Text
       <span id="question1-answer"> </span>
    </div>
</div>

Oddly enough, the span at the bottom (id="question1-answer") is kept. Am I missing something? I already tried to set a whitespace "&nbsp" within the tag, which will be converted to a " " (actual whitespace) after changing text in "visual" and used different tags as well.

Thanks!

like image 598
Nocebo Avatar asked May 30 '17 11:05

Nocebo


2 Answers

Add this code in your active theme functions.php file.

function override_mce_options($initArray) {
    $opts = '*[*]';
    $initArray['valid_elements'] = $opts;
    $initArray['extended_valid_elements'] = $opts;
    return $initArray;
} 
add_filter('tiny_mce_before_init', 'override_mce_options');
like image 109
Vel Avatar answered Nov 15 '22 09:11

Vel


A little more specific - allow empty tags if they have an id, name, class or style attribute:

function override_mce_options($initArray) {
  $opts = '*[id|name|class|style]';
  $initArray['valid_elements'] .= ',' . $opts;
  $initArray['extended_valid_elements'] .= ',' . $opts;
  return $initArray;
}
add_filter('tiny_mce_before_init', 'override_mce_options');

Maybe I'm doing something wrong, but for me it works. Still I'm sure there's a better solution - it would be nice to be able to add only one specific tag to valid elements.

like image 26
Azragh Avatar answered Nov 15 '22 10:11

Azragh