Whenever somebody writes a post on our site, the Paly Voice (http://palyvoice.com) there are automatically a lot of <div> elements created. This is bad for layout, since we have a style that applies to divs that we don't want to apply to story styles.

Is there a way to remove the automatic div insertion? If not, can I add a function hook to parse the post and remove them before saving to the DB?

Edit: Example here: http://palyvoice.com/2012/08/22/new-grade-reports-to-be-mailed-home-every-four-weeks-2/


You first should find out where the divs are coming from, since that's not normal behavior. Could be from a plugin or - as Damien said - copy-pasting a text from Word.

To remove the divs you can do a simple

str_replace(array('<div>', '</div>'), '', $content)

either before storing the text in the database (by hooking on save_post), or before displaying it on the site (by adding a filter on the_content).

EDIT: I was wrong, you don't hook on save_post, but instead you filter on wp_insert_post_data. This function below should work:

function remove_divs($data) {
    $filteredContent = str_replace(array('<div>', '</div>'), '', $data['post_content']);
    $data['post_content'] = $filteredContent;

    return $data;
add_filter('wp_insert_post_data', 'remove_divs', 99);

Put this in your functions.php

| improve this answer | |
  • Where can I put this in my files to hook into save_post? Then I'll accept your answer :) – tekknolagi Aug 22 '12 at 14:33
  • Check out my edit, that should work for removing any divs before the post get saved to the database. – pbd Aug 23 '12 at 8:44
  • In which file do you put this? – tekknolagi Aug 23 '12 at 8:57
  • functions.php in your theme folder. – pbd Aug 23 '12 at 9:02
  • What does the 99 mean? – tekknolagi Aug 23 '12 at 14:35

Is the person using cut n paste from word? This can add a lot of horrible extra elements.

In the toolbar for Wordpress there is a button for inserting directly from word .. This should help.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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