admin管理员组文章数量:1130349
I would like to use a simple function in order to add a specific attribute (lets call it “example”) to the first image of every blog post, so that I don't have to do it manually in thousands of posts.
Unfortunately I'm not very good with preg_replace so any help will be appreciated.
I would like to use a simple function in order to add a specific attribute (lets call it “example”) to the first image of every blog post, so that I don't have to do it manually in thousands of posts.
Unfortunately I'm not very good with preg_replace so any help will be appreciated.
Share Improve this question asked Oct 26, 2018 at 9:21 k8310k8310 31 bronze badge1 Answer
Reset to default 1add_filter( 'the_content', 'wpse317670_add_img_attribute' );
function wpse317670_add_img_attribute( $content ) {
$from = '/'.preg_quote('<img', '/').'/';
$to = '<img example="example"';
return preg_replace($from, $to, $content, 1);
}
This will add the example="example" to the first image found in every post content.
There is another option, without using regular expression (possibly much faster and will use less memory):
add_filter( 'the_content', 'wpse317670_add_img_attribute' );
function wpse317670_add_img_attribute( $content ) {
$from = '<img';
$to = '<img example="example"';
$pos = strpos( $content, $from );
if ( $pos !== false ) {
return substr_replace( $content, $to, $pos, strlen( $from ) );
}
return $content;
}
I would like to use a simple function in order to add a specific attribute (lets call it “example”) to the first image of every blog post, so that I don't have to do it manually in thousands of posts.
Unfortunately I'm not very good with preg_replace so any help will be appreciated.
I would like to use a simple function in order to add a specific attribute (lets call it “example”) to the first image of every blog post, so that I don't have to do it manually in thousands of posts.
Unfortunately I'm not very good with preg_replace so any help will be appreciated.
Share Improve this question asked Oct 26, 2018 at 9:21 k8310k8310 31 bronze badge1 Answer
Reset to default 1add_filter( 'the_content', 'wpse317670_add_img_attribute' );
function wpse317670_add_img_attribute( $content ) {
$from = '/'.preg_quote('<img', '/').'/';
$to = '<img example="example"';
return preg_replace($from, $to, $content, 1);
}
This will add the example="example" to the first image found in every post content.
There is another option, without using regular expression (possibly much faster and will use less memory):
add_filter( 'the_content', 'wpse317670_add_img_attribute' );
function wpse317670_add_img_attribute( $content ) {
$from = '<img';
$to = '<img example="example"';
$pos = strpos( $content, $from );
if ( $pos !== false ) {
return substr_replace( $content, $to, $pos, strlen( $from ) );
}
return $content;
}
本文标签: Add attribute only to first image of every post via functionsphp
版权声明:本文标题:Add attribute only to first image of every post via functions.php 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749230804a2336449.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论