Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using the below to fetch the content of a page by its name and output the html

  $my_wp_query = new WP_Query();
  $page = $my_wp_query->query(
    array(
      'post_type' => 'page',
      'name' => 'some-page-slug'
    )
  );
  $page = $page[0];

  $page_data['content'] =  apply_filters('the_content', $page->post_content);

The field $page->post_content unmodified contains a string with the content I have typed in the backend as well as a reference to the gallery in the form [gallery="1,2,3,4"] at the end of the string. When I run it through the apply_filters('the_content', $page->post_content) method, it restores the HTML tags such as the <p> elements, however it also includes the gallery code which has its own style and quite lengthy markup as illustrated below.

<p>Residential Gallery. Etiam lobortis sapien vel erat sodales et suscipit leo pretium. Pellentesque a arcu sed velit congue sagittis ac vel erat. Curabitur non felis leo, vel vestibulum purus. Donec eget velit eget magna rutrum facilisis. In hac habitasse platea dictumst.</p>
<p>Quisque sit amet pellentesque risus. Aliquam erat volutpat. Ut at magna justo, tincidunt viverra odio. Pellentesque a arcu sed velit congue sagittis ac vel erat. Curabitur non felis leo, vel vestibulum purus. Donec eget velit eget magna rutrum facilisis. In hac habitasse platea dictumst. Quisque sit amet pellentesque risus. Aliquam erat volutpat.</p>
<p>Ut at magna justo, tincidunt viverra odio. Curabitur non felis leo, vel vestibulum purus. Donec eget velit eget magna rutrum facilisis. In hac habitasse platea dictumst. Quisque sit amet pellentesque risus. Aliquam erat volutpat. Ut at magna justo, tincidunt viverra odio.</p>
<style type='text/css'>
    #gallery-1 {
        margin: auto;
    }
    #gallery-1 .gallery-item {
        float: left;
        margin-top: 10px;
        text-align: center;
        width: 33%;
    }
    #gallery-1 img {
        border: 2px solid #cfcfcf;
    }
    #gallery-1 .gallery-caption {
        margin-left: 0;
    }
</style>
<!-- see gallery_shortcode() in wp-includes/media.php -->
<div id='gallery-1' class='gallery galleryid-0 gallery-columns-3 gallery-size-thumbnail'><dl class='gallery-item'>
    <dt class='gallery-icon'>
        <a href='http://local.definitive-final2.co.uk/blog/residential/georgian-house/georgian1/' title='georgian1'><img width="150" height="150" src="http://local.definitive-final2.co.uk/blog/wp-content/uploads/2013/01/georgian1-150x150.jpg" class="attachment-thumbnail" alt="georgian1" /></a>
    </dt></dl><dl class='gallery-item'>
    <dt class='gallery-icon'>
        <a href='http://local.definitive-final2.co.uk/blog/residential/georgian-house/georgian2/' title='georgian2'><img width="150" height="150" src="http://local.definitive-final2.co.uk/blog/wp-content/uploads/2013/01/georgian2-150x150.jpg" class="attachment-thumbnail" alt="georgian2" /></a>
    </dt></dl><dl class='gallery-item'>
    <dt class='gallery-icon'>
        <a href='http://local.definitive-final2.co.uk/blog/residential/georgian-house/georgian3/' title='georgian3'><img width="150" height="150" src="http://local.definitive-final2.co.uk/blog/wp-content/uploads/2013/01/georgian3-150x150.jpg" class="attachment-thumbnail" alt="georgian3" /></a>
    </dt></dl><br style="clear: both" /><dl class='gallery-item'>
    <dt class='gallery-icon'>
        <a href='http://local.definitive-final2.co.uk/blog/residential/georgian-house/georgian4/' title='georgian4'><img width="150" height="150" src="http://local.definitive-final2.co.uk/blog/wp-content/uploads/2013/01/georgian4-150x150.jpg" class="attachment-thumbnail" alt="georgian4" /></a>
    </dt></dl><dl class='gallery-item'>
    <dt class='gallery-icon'>
        <a href='http://local.definitive-final2.co.uk/blog/residential/paris-house/paris4/' title='paris4'><img width="150" height="150" src="http://local.definitive-final2.co.uk/blog/wp-content/uploads/2013/01/paris4-150x150.jpg" class="attachment-thumbnail" alt="paris4" /></a>
    </dt></dl>
    <br style='clear: both;' />
</div>

I would like to only retrieve the HTML text with <p>'s etc and not the gallery's <style> and <div> tags.

How might I achieve this?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

I may be misinterpreting you, but I think what you want to do is run through strip_shortcodes

  $page_data['content'] = strip_shortcodes($page->post_content);
  $page_data['content'] = apply_filters('the_content', $page_data['content']);

Alternately you could conditionally hook that function to the_content as demonstrated on the Codex Page.

share|improve this answer
    
Both elegant and simple. Thank you. –  Rodgers and Hammertime Jan 14 '13 at 0:12
add comment

All you need to do is apply preg_replace to remove all patterns matching \[gallery[^\]]+\] from your content (before expanding everything out).

share|improve this answer
    
Scratch this. @s_ha_dum has a much better WP-specific solution. –  Dan Jan 14 '13 at 0:15
    
Thanks for this. I was initially trying this approach - for absolutely ages as it happens - but couldn't nail the now seemingly simple regex rule! –  Rodgers and Hammertime Jan 14 '13 at 0:20
2  
Because it isn't a simple regex rule. Here is the one the WordPress core uses: core.trac.wordpress.org/browser/tags/3.5/wp-includes/… –  s_ha_dum Jan 14 '13 at 0:24
    
Wow... That is a gnarly regex! –  Dan Jan 14 '13 at 0:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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