Php 更改Wordpress中所有img标记的html结构
我有一个Wordpress博客,正在尝试实现图像脚本。简而言之,我需要针对所有post图像,将Php 更改Wordpress中所有img标记的html结构,php,regex,wordpress,function,preg-replace,Php,Regex,Wordpress,Function,Preg Replace,我有一个Wordpress博客,正在尝试实现图像脚本。简而言之,我需要针对所有post图像,将src、width和height属性与datasrc、datawidth和dataheight属性交换。然后我需要复制图像行并将其包装在标记中。这是我试图让Wordpress生成/创建的结构: <img data-src="wordpress/image/url/pic.jpg" data-width="{get width of image with PHP & pass-in that
src、width和height
属性与datasrc、datawidth和dataheight
属性交换。然后我需要复制图像行并将其包装在
标记中。这是我试图让Wordpress生成/创建的结构:
<img data-src="wordpress/image/url/pic.jpg" data-width="{get width of image with PHP & pass-in that value here} data-height="{get height of image with PHP and pass-in that value here}" class="fs-img">
<noscript>
<img src="wordpress/image/url/pic.jpg">
</noscript>
另一个“获取图像标签”尝试:
模式匹配尝试:
尝试在这一个上创建我自己的正则表达式,但不确定它是否正确
<?php function restructure_imgs($content) {
global $post;
$pattern = "/<img(.*?)src=('|\")(.*?).(bmp|gif|jpeg|jpg|png)(|\")(.*?)>/i";
list($width, $height, $type, $attr) = getimagesize($2$3.$4$5);
$hwstring = image_hwstring($width, $height);
$replacement = '<img$1data-src=$2$3.$4$5 title="'.$post->post_title.'" data-width="'.$width.'" data-height="'.$height.'" class="fs-img"$6>';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
add_filter('the_content', 'restructure_imgs');
?>
您的方法是在页面呈现之前,我认为这在wordpress中不是不可能的,但是您可以在使用javascript呈现页面之后执行相同的操作。
我试着用jQuery实现这一点,而ti似乎很有效:
<!-- Example image -->
<img src="http://fotografia.deagostinipassion.com/resources/photos/2/2n/ilCielo.JPG" width="200px" height="300px">
<!-- Import jquery -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function () {
$("img").wrap(function() {
$(this).wrap(function() {
var newimg = '<img data-src="' + $(this).attr('src') + '" data-width="' + $(this).attr('width') + '" data-height="' + $(this).attr('height') + '" class="fs-img">';
return newimg;
});
return '<noscript>';
});
});
</script>
$(文档).ready(函数(){
$(“img”).wrap(函数(){
$(this).wrap(function(){
var newimg='';
返回newimg;
});
返回“”;
});
});
如果您需要更多帮助,请咨询我。您不应该使用正则表达式解析HTML,请参阅
<?php function restructure_imgs($content) {
global $post;
$pattern = "/<img(.*?)src=('|\")(.*?).(bmp|gif|jpeg|jpg|png)(|\")(.*?)>/i";
list($width, $height, $type, $attr) = getimagesize($2$3.$4$5);
$hwstring = image_hwstring($width, $height);
$replacement = '<img$1data-src=$2$3.$4$5 title="'.$post->post_title.'" data-width="'.$width.'" data-height="'.$height.'" class="fs-img"$6>';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
add_filter('the_content', 'restructure_imgs');
?>
<!-- Example image -->
<img src="http://fotografia.deagostinipassion.com/resources/photos/2/2n/ilCielo.JPG" width="200px" height="300px">
<!-- Import jquery -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function () {
$("img").wrap(function() {
$(this).wrap(function() {
var newimg = '<img data-src="' + $(this).attr('src') + '" data-width="' + $(this).attr('width') + '" data-height="' + $(this).attr('height') + '" class="fs-img">';
return newimg;
});
return '<noscript>';
});
});
</script>