filters - Wrap h1-h6 in a div

So, I am using heading tags h1-h6 on my site in various places, what I'd like to do is put a div around those tags

So, I am using heading tags h1-h6 on my site in various places, what I'd like to do is put a div around those tags using a WP function. Right now I am using some jQuery to get the job done, but I'm trying to minimize the amount of jQuery on my site, so I figured it'd be better to use some kind of preg_replace to find any h1-h6 tags and add an outer div.

My current jQuery looks like this: $("h6, h5, h4, h3, h2, h1").wrap('<div class="title"></div>');

I found some code that works for images, but I'm not sure how to tweak it, so I can use it for heading tags:

function outer_img_wrap( $content ) {
    $image = '/(<img([^>]*)>)/i';
    $wrapper = '<span class="featured">$1</span>';
    $content = preg_replace( $image, $wrapper, $content );
    return $content;
}
add_filter( 'the_content', 'outer_img_wrap' );

Any help is appreciated. The function doesn't have to look like that, just found that for images and thought it was nice and simple.

Thanks,
Josh

So, I am using heading tags h1-h6 on my site in various places, what I'd like to do is put a div around those tags using a WP function. Right now I am using some jQuery to get the job done, but I'm trying to minimize the amount of jQuery on my site, so I figured it'd be better to use some kind of preg_replace to find any h1-h6 tags and add an outer div.

My current jQuery looks like this: $("h6, h5, h4, h3, h2, h1").wrap('<div class="title"></div>');

I found some code that works for images, but I'm not sure how to tweak it, so I can use it for heading tags:

function outer_img_wrap( $content ) {
    $image = '/(<img([^>]*)>)/i';
    $wrapper = '<span class="featured">$1</span>';
    $content = preg_replace( $image, $wrapper, $content );
    return $content;
}
add_filter( 'the_content', 'outer_img_wrap' );

Any help is appreciated. The function doesn't have to look like that, just found that for images and thought it was nice and simple.

Thanks,
Josh

Share Improve this question edited Mar 29, 2019 at 13:29 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Mar 29, 2019 at 12:37 joshmrodgjoshmrodg 1,1731 gold badge16 silver badges42 bronze badges 3
  • Try a regular expression like: <h.*?<\/h.>. – Nicolai Grossherr Commented Mar 29, 2019 at 13:27
  • @Nicolai - Thanks for the quick reply, I ran this through a regex validator and it finds everything, the header tags and the content, looking at my example code above this should work if I add it where the $image variable is right? – joshmrodg Commented Mar 29, 2019 at 14:04
  • Yes, you have to modify the pattern variable, it should be like: /(<h.*?<\/h.>)/ims. Not sure about your issues, it works in my test. – Nicolai Grossherr Commented Mar 29, 2019 at 14:27
Add a comment  | 

2 Answers 2

Reset to default 3

Try this code

function wrap_heading_with_div( $content ) {
    $heading = '/<h\d.*?>(.*?)<\/h\d>/ims';
    $wrapper = '<div class="title">$0</div>';
    $content = preg_replace($heading, $wrapper, $content);
    return $content;
}
add_filter( 'the_content', 'wrap_heading_with_div' );

Live Demo

The code you’ve posted is a great starting point. All you have to do is to change the expressions inside of it.

It’s a little bit trickier, because you have to replace opening and closing tag. I would go for something like this:

function wrap_headers_with_div( $content ) {
    $content = preg_replace('/<h([1-6])>/', '<div class="title"><h$1>', $content);
    $content = preg_replace('/<\/h([1-6])>/', '</h$1></div>', $content);

    return $content;
}
add_filter( 'the_content', 'wrap_headers_with_div' );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745646324a4637994.html

相关推荐

  • filters - Wrap h1-h6 in a div

    So, I am using heading tags h1-h6 on my site in various places, what I'd like to do is put a div around those tags

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信