首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从数据库中删除所有外部链接

如何从数据库中删除所有外部链接
EN

Stack Overflow用户
提问于 2022-03-02 23:39:51
回答 1查看 479关注 0票数 1

我有WordPress网站与100个广告帖子,我需要删除所有外部链接从这篇文章。但是新的职位也应该有外部的联系。因此,这就是为什么我必须从数据库中删除外部链接,而不是使用脚本。

有什么办法吗?

编辑,我使用这个脚本删除ID为1- 3568的帖子中的链接。

代码语言:javascript
复制
add_filter( 'the_content', 'filter_the_content_in_the_main_loop' );

function filter_the_content_in_the_main_loop( $content ) {

    if ( is_single(range(1, 3568)) ) {
      $content = preg_replace(array('"<a href(.*?)>"', '"</a>"'), array('',''), $content);
    }

    return $content;
}

有可能添加一些东西,什么会将这些数据发送到数据库?

EN

回答 1

Stack Overflow用户

发布于 2022-03-03 00:11:21

首先需要标识包含链接的表和列。

有两种类型的链接:site linksexternal links

站点链接应该类似于http:\\example.com\anything_else

post的WP标准表是wp_posts,但对于hiperlink则取决于定制(可以是wp_post中的一列,也可以是其他地方的列)。这是你自己找的)

假设表是wp_posts,列是links,那么您需要按模式查找,并不使用这些列进行更新。

主要模式应该是start with : http:\\example.com (意思是将作为站点链接而不是外部链接的链接)。

翻译成sql

代码语言:javascript
复制
update wp_posts 
set links = ''
where links not like 'http:\\example.com%'

注: WP标准DB为MySql

更新:

代码语言:javascript
复制
function filter_the_content_in_the_main_loop( $content ) {
global $post;    
if ( is_single(range(1, 3568)) ) {
    $content = preg_replace(array('"<a href(.*?)>"', '"</a>"'), array('',''), $content);
    //find the post_id and set new content
    //wp standard way of working with post instead of direct sql
    //this will update each time each post, even the new one after display 
    //(if is in range, so for new one to remain unmodified just keep a lower range, 
    //up to last 100 posts)
    $my_post = array();
    $my_post['ID'] = $post->ID;
    $my_post['post_content'] = $content;
    wp_update_post( $my_post );
}

return $content;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71330466

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档