Php 更改内容上的某些链接的属性
我正在构建wordpress插件,用于更改帖子内容上的一些链接。我有这样的功能:Php 更改内容上的某些链接的属性,php,wordpress,Php,Wordpress,我正在构建wordpress插件,用于更改帖子内容上的一些链接。我有这样的功能: public static function encrypt_link_content( $content ){ if ( strpos( $content, 'safelinkThis' ) !== false ) { // get dom from string of content $content = str_get_dom($content); /
public static function encrypt_link_content( $content ){
if ( strpos( $content, 'safelinkThis' ) !== false ) {
// get dom from string of content
$content = str_get_dom($content);
// find link with attribute data-role=safelinkThis
$all_links = $content('a[data-role=safelinkThis]');
$count_links = count($all_links);
$i = 0;
$_links = self::get_links_post($count_links);
foreach ($all_links as $a) {
$href = $a->href;
$encrypt = self::zi_encrypt($href);
$a->href = $_links[$i]."?link=".$encrypt;
$i += 1;
}
$content = (string) $content;
}
return $content;
}
Find us on :
<a href="http://asite.com/page?link=url_encrypted" data-role="safelinkThis">Go to facebook</a>
<a href="http://asite.com/page?link=url_encrypted" data-role="safelinkThis">Go to google</a>
<a href="http://twitter.com">Go to Twitter</a>
该函数已生效,链接的属性href
已更改
内容示例:
Find us on :
<a href="http://facebook.com" data-role="safelinkThis">Go to facebook</a>
<a href="http://google.com" data-role="safelinkThis">Go to google</a>
<a href="http://twitter.com">Go to Twitter</a>
但我认为这是一个不好的做法,若我有很多关于内容的链接,我应该逐个检查数据库。
如果链接主机未包含在数据库中,如何比较数据库中具有列表域的内容上的链接主机,并更改链接属性href
global $wpdb;
$sf_table_name = $wpdb->prefix . 'sf_domain';
foreach ($all_links as $a) {
$href = $a->href;
$aDomain = parse_url($href)['host'];
$record = $wpdb->get_results( "SELECT * FROM $sf_table_name WHERE domain='$aDomain'" );
if(count($record) == 0){
// change this link
}
}