从帖子内容中删除/剥离所有外部链接

时间:2021-06-14 18:56:49

标签: wordpress

我想删除所有退出我帖子内容的外部链接(从我的域中发出的链接)。

我进行了研究并最终得到了这个,但它仍然无法正常工作:

    function remove_links_from_post($post){
    $post_content = stripslashes($post["post_content"]);

    if(!preg_match_all("/(<a.*>)(.*)(<\/a>)/ismU",$post_content,$outbound_links,PREG_SET_ORDER)){
        return $post;
    }

    foreach($outbound_links as $key => $value){
        preg_match("/href\s*=\s*[\'|\"]\s*(.*)\s*[\'|\"]/i",$value[1],$href);

        if((substr($href[1],0,7)!="http://" && substr($href[1],0,8)!="https://") || substr($href[1],0,strlen(get_bloginfo("url")))==get_bloginfo("url")){
            unset($outbound_links[$key]);
        }else{
            $post_content = str_replace($outbound_links[$key][0],$outbound_links[$key][2],$post_content);
        }
    }

    $post["post_content"] = addslashes($post_content);

    return $post;
}

add_filter("wp_insert_post_data", "remove_links_from_post");

0 个答案:

没有答案
相关问题