Wordpress教程 2023年07月18日
0 收藏 0 点赞 1,284 浏览 834 个字

将以上代码添加到你的主题的functions.php文件中,并将$old_domain和$new_domain的值替换为你的旧域名和新域名。

这段代码使用正则表达式来匹配和替换文章内容中的图片URL以及特色图像URL。它会检查更换WordPress网站的域名原始图片地址保持不变标签中的src属性和标签中的og:image属性,并将旧域名替换为新域名。

function keep_original_image_urls( $content ) {
    $old_domain = 'http://www.aaa.com'; // 旧域名
    $new_domain = 'http://www.bbb.com'; // 新域名

    $content = str_replace( $new_domain, $old_domain, $content );

    // 替换文章内容中的图片地址
    $pattern = '/<img[^>]+src=['"](https?://www.lvjps.com[^'"]*)['"][^>]*>/i';
    $replacement = '<img src="$1">';
    $content = preg_replace( $pattern, $replacement, $content );

    // 替换文章中的特色图像地址
    $pattern = '/<meta[^>]+og:image[^>]+content=['"](https?://www.lvjps.com[^'"]*)['"][^>]*>/i';
    $replacement = '<meta property="og:image" content="$1">';
    $content = preg_replace( $pattern, $replacement, $content );

    return $content;
}
add_filter( 'the_content', 'keep_original_image_urls' );
add_filter( 'widget_text_content', 'keep_original_image_urls' );

 

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.mizhanw.com/blog/2557.html

相关推荐
在WooCommerce中添加“立即购买”按钮直接跳转到结算页面
add_action( 'woocommerce_after_add_to_cart_button', 'add_content_after…
日期:2024-10-19 点赞:0 阅读:15
实现 WordPress 自动更新所有文章的发布日期为当天的日期
//二开自动更新文章日期为当天发布。 //设置宝塔面板中的定时任务,通过访问带有?update_posts=run的链接触发文章更新.如:c…
日期:2024-09-22 点赞:0 阅读:58
WooCommerce订单列表显示购买产品
// 为 Legacy CPT-based 订单添加自定义列 add_filter('manage_edit-shop_order_colu…
日期:2024-09-20 点赞:0 阅读:115
WooCommerce Order 类的所有Get方法,以面向对象的方法获取订单数据
在开发WooCommerce插件或者与第三方系统交互时,我们需要获取 WooCommerce 订单的数据,WooCommerce的 WC_O…
日期:2024-09-20 点赞:0 阅读:86
为WooCommerce后台订单列表添加按自定义字段功能
  // 添加自定义列到订单列表 add_filter('manage_woocommerce_page_wc-orders_co…
日期:2024-09-20 点赞:0 阅读:72
添加删除 WooCommerce 数据列表中的数据列
转载:添加删除 WooCommerce 数据列表中的数据列 _WordPress智库 (wpzhiku.com) 在 WooCommerce…
日期:2024-09-20 点赞:0 阅读:93
发表评论
暂无评论

还没有评论呢,快来抢沙发~