Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
976 views
in Technique[技术] by (71.8m points)

Create shortcode in Wordpress that disables wpautop() on wrapped content?

Looking to place a graphic on the same line/row as an h3 title like this:

<h3 style="display:inline;">Graphic Advantage</h3><img style="max-width: 50px; width: 100%;" src="https://sgdesign.com/images/SGDadvantage.png" alt="SGD Advantage"  />

First I make the h3 display = inline so it only occupies it's actual width rather than the full row. I could float the h3 and the graphic left and then do a clearfix but it seems excessive. The goal is to simply place both the h3 and small graphic on the same line.

It fails because of Wordpresses auto formatting function wpautop() which sets automatically, a paragraph tag < p >

But yet this works when the secondary content (graphic) is wrapped in a shortcode:

<h3 style="display: inline;">Advantage SGDesign</h3>
[tooltip text="Throughout our site you'll see this icon that will help identify significant differences between SGDesign and other companies"]<img style="max-width: 50px; width: 100%;" src="https://sgdesign.com/images/SGDadvantage.png" alt="SGD Advantage"  />[/tooltip]

QUESTION: Best practice to thwart the wpautop() function within a wrapped tag of some type?

Maybe creating a null shortcode like

function no_wp_autoformat_shortcode() {
return null;
}
add_shortcode('nowpautop', 'no_wp_autoformat_shortcode');

This will work but now it hides the wrapped < img ...

So the question changes to how to make an image show when wrapped by a Shortcode ?

<h3 style="display:inline;">Graphic Advantage</h3>[nowpautop]<img style="max-width: 50px; width: 100%;" src="https://sgdesign.com/images/SGDadvantage.png" alt="SGD Advantage"  />[/nowpautop]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is a way to stop Wordpress editor to automatically add in <p> element using remove_filter( 'the_content', 'wpautop' );. The down side of this will disable all the <p> elements in wordpress which is not good when comes to paragraphing your text. (It also gets problematic when people cut and paste text from word documents...)

Yes using shortcode seems good to me without having to overwrite the Wordpress core functionalities


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...