-3

I want to customize every image or p of the_content(); output, assign class and so on.. What I have to do?

closed as too broad by Mayeenul Islam, Pieter Goosen, TheDeadMedic, tfrommen, Howdy_McGee Apr 15 '15 at 13:49

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

1

You can add a filter to the content directly on functions.php and use str_replace or perhaps some preg_replace (regex) to change the output of the content. Not tested. Try something like this:

function add_class_to_img_and_p_in_content($content) {

   $pattern ="/<img(.*?)class=\"(.*?)\"(.*?)>/i";
   $replacement = '<img$1class="$2 newclass newclasstwo"$3>';

   $content = preg_replace($pattern, $replacement, $content);

   $pattern ="/<p(.*?)class=\"(.*?)\"(.*?)>/i";
   $replacement = '<p$1class="$2 newclass newclasstwo"$3>';

   $content = preg_replace($pattern, $replacement, $content);


   return $content;
}
add_filter('the_content', 'add_class_to_img_and_p_in_content',11);

If your p and img tags do not already have a class attribute you can simply do:

function new_content($content) {

    $content = str_replace('<img','<img class="newImgclass"', $content);
    $content = str_replace('<p>','<p class="newPclass">', $content);

    return $content;
}

add_filter('the_content','new_content');
  • thanks it works for <p>, but not with img – Antonino Lattene Apr 14 '15 at 21:21
  • sorry, thats because I closed the img tag, when infact it never closes without atleast the src attribute. I will edit my answer. – Eddie Would Go Apr 14 '15 at 21:27
  • thanks, now it works, but it replaces all the wordpress classes with the new one. If I want to add my new class to the others? – Antonino Lattene Apr 14 '15 at 21:33
  • edited my answer so you can add newclass to the elements that already have class atribute. – Eddie Would Go Apr 14 '15 at 22:49
  • Thanks. Now I can modify the img. I just want you know that the second part of the fuction (for <p>) applies the rule fot the img, I think something is wrong. I never see a function like this in other themes, and they customize the_content output, do you know how they get it? – Antonino Lattene Apr 15 '15 at 18:35

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