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

发布于 2025-01-11 03:33:01 字数 521 浏览 2 评论 0原文

我有一个包含数百篇帖子的 WordPress 网站,我需要删除此帖子中的所有外部链接。但新帖子也应该有外部链接。这就是为什么我必须从数据库中删除外部链接而不是使用脚本的原因。

有什么想法如何做到这一点吗?

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

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;
}

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

i have WordPress website with hundreads posts and i need remove all external links from this posts. But also new posts should have external links. So thats reason why i have to remove external links from database and not with script.

Any ideas how to do that?

Edit, i did this script what remove links from posts with ID 1 - 3568

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;
}

Is possible add there something, what will send this data to database?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

じ违心 2025-01-18 03:33:01

首先需要识别保存链接的表和列。

有两种类型的链接:站点链接外部链接

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

WP的post标准表是wp_posts,但是hiperlinks则取决于自定义(可以是wp_post中的一列,也可以是其他地方。这里由你来查找)

假设表是 wp_posts 并且列是 links 那么您需要按模式查找并更新这些列。

主要模式应该是以:http:\\example.com开头(意味着将保留为站点链接而不是外部链接)

翻译为sql

update wp_posts 
set links = ''
where links not like 'http:\\example.com%'

注意:WP标准DB是MySql

更新:

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;
}

First need to identify the table and the column which hold the links.

There are 2 types of links : site links and external links.

Site links should be something like http:\\example.com\anything_else

WP standard table for post is wp_posts, but for hiperlinks depends on the customization (can be a column in wp_post or maybe elsewhere. Here is up to u to find)

Assume the table is wp_posts and the column is links then you need to find by pattern and update with nothing those columns.

Mainly the pattern should be start with : http:\\example.com (means links that will be keep as site links and not external)

Translated to sql

update wp_posts 
set links = ''
where links not like 'http:\\example.com%'

Note: WP standard DB is MySql

Update:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文