Currently building a WordPress theme and the wpautop function is quite a pain to work with. I’m pretty much a complete noob when it comes to regex, I was wondering if anyone can help me out.
Say I have:
<p><img src="img/something.jpg" width="1249124" height="20" alt="foo" /></p>
How can I replace that and basically only get rid of the <p></p> tags. Bare in mind that there will be other content so I can’t rule out all <p> tags entirely.
This is the content:
<p><img src="http://85.17.31.78/~tfbox/wp-content/uploads/2011/08/spotify4.jpg" alt="foo" /></p>
<p>The path of the righteous man is beset on all sides by the iniquities of the selfish and the tyranny of evil men. Blessed is he who, in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother's keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy My brothers. And you will know My name is the Lord when I lay My vengeance upon thee.</p>
<p>The path of the righteous man is beset on all sides by the iniquities of the selfish and the tyranny of evil men. Blessed is he who, in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother's keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy My brothers. And you will know My name is the Lord when I lay My vengeance upon thee.</p>
<p>The path of the righteous man is beset on all sides by the iniquities of the selfish and the tyranny of evil men. Blessed is he who, in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother's keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy My brothers. And you will know My name is the Lord when I lay My vengeance upon thee.</p>
Here’s the script:
function ptags() {
$c = get_the_content();
echo preg_replace('#(.*?)<p>\s*(<img[^<]+?)\s*</p>(.*)#s', '$1$2$3', $c);
}
add_filter('the_content', 'ptags', 9);
Edit: Here is the pattern explanation