• Resolved janneval

    (@janneval)


    Hi!

    I’ve got bit of problem because WP is automatically changing (=”correcting”) my html in a way that creates mistake.

    I have a shortcode I wrote into my custom theme. This shortcode is supposed to create a small div element between the text. (That element contains other things, but it should not matter.) In the layout the div should look as if it was a single word in the text.

    .
    WHAT I TRY TO DO

    So when I write in the WP post/page editor like this:

    Text text text text text text text [MY-SHORTCODE] text text text text text text text

    It should be seen on the page like this:

    Text text text text text text text STUFF-IN-THE-DIV text text text text text text text

    The html of it should be like this:

    <p>Text text text text text text text
    <div style="display:inline-block">STUFF-IN-THE-DIV</div>
    text text text text text text text</p>

    .
    THE PROBLEM

    However, it does not work like that. Wp does “correction” to the html before outputting it to the browser. It makes my shortcode-made element cut the line in wrong way. So it look like this:

    Text text text text text text text
    STUFF-IN-THE-DIV text text text text text text text

    And the html is twisted like this:

    <p>Text text text text text text text </p>
    <div style="display:inline-block">STUFF-IN-THE-DIV</div>
    text text text text text text text <p></p>

    My question is how can I avoid this automatic “correction” and get the html which intend to get.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi,

    You may look into this filter WordPress applies to content to add P tags.
    You can disable this filter if you want:

    https://codex.www.ads-software.com/Function_Reference/wpautop

    Thread Starter janneval

    (@janneval)

    I see. It’s getting bit tricky now. Because if I disable the whole filter, it brings other problems. In general the filter is very useful and needed, because otherwise there is no p-tags in text users write. Not to use it at all is probably worse than bearing single problem it creates.

    However, what I could try is to disable this filter and then make my own similar filter to be used instead. And my own filter I could make by copying the original filter code and then making the necessary changes to it.

    (So practically this would do the same as editing the original filter, but to edit WP core code is always a bad idea.)

    Let’s see if it works! I’ll come back to report you when I have time to try it. Thank you for your advice, Mohsin!

    Thread Starter janneval

    (@janneval)

    I did it! IT WORKS!!

    No need to make any of that complicated thing I explain above, nothing! It’s very simple. (after hour of research it really feels almost too simle!) I just noticed that wpautop() does not add a p-element for an object element. So I replaced my div element by an object element. Perfect!

    So here is output of my shortcode:

    <p>Text text text text text text text
    <object style="display:inline-block">STUFF-INSIDE</object>
    text text text text text text text</p>

    Thanks Mohsin for a tip!

    Awesome! Keep up great work!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to override the automatic "corrections" WP makes to html?’ is closed to new replies.