Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6350493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:49:13+00:00 2026-05-24T21:49:13+00:00

Currently building a WordPress theme and the wpautop function is quite a pain to

  • 0

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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:49:14+00:00Added an answer on May 24, 2026 at 9:49 pm
    $str = '<p><img src="img/something.jpg" width="1249124" height="20" alt="foo" /></p>';
    $str = preg_replace('%(.*?)<p>\s*(<img[^<]+?)\s*</p>(.*)%is', '$1$2$3', $str);
    

    Edit: Here is the pattern explanation

    Match the regular expression below and capture its match into backreference number 1 «(.*?)»
       Match any single character that is not a line break character «.*?»
          Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
    Match the characters “<p>” literally «<p>»
    Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s*»
       Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
    Match the regular expression below and capture its match into backreference number 2 «(<img[^<]+?)»
       Match the characters “<img” literally «<img»
       Match any character that is NOT a “<” «[^<]+?»
          Between one and unlimited times, as few times as possible, expanding as needed (lazy) «+?»
    Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s*»
       Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
    Match the characters “</p>” literally «</p>»
    Match the regular expression below and capture its match into backreference number 3 «(.*)»
       Match any single character that is not a line break character «.*»
          Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently building a WordPress theme that I would like to have a header
I'm building a Wordpress template from an HTML page. I currently have <?php $queried_post
I am currently finish of my theme for a wordpress website that I am
I'm currently building an app for my work, it work perfectly on my PC
I'm currently building an e-commerce site in wordpress using the WP-ecommerce plugin.In product page,
im building off the wordpress plaintxt sandbox theme, and what i have i a
I'm building an authors page for my Wordpress blog that lists all the current
I'm currently building a Java app that could end up being run on many
I am currently building in Version 3.5 of the .Net framework and I have
I am currently building an internal web application used in a factory/warehouse type location.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.