将下面的代码添加到主题的 function.php 中
function rt_add_link_target( $content ){ // use the <a> tag to split into segments $bits = explode( '<a ', $content ); // loop though the segments foreach( $bits as $key=>$bit ){ // fix the target="_blank" bug after the link if ( strpos( $bit, 'href' ) === false ) continue; // find the end of each link $pos = strpos( $bit, '>' ); // check if there is an end (only fAIls with malformed markup) if( $pos !== false ){ // get a string with just the link's attibutes $part = substr( $bit, 0, $pos ); // for comparison, get the current site/network url $siteurl = network_site_url(); // if the site url is in the attributes, assume it's in the href and skip, also if a target is present if( strpos( $part, $siteurl ) === false && strpos( $part, 'target=' ) === false ){ // add the target attribute $bits[$key] = 'target="_blank" ' . $bits[$key]; } } } // re-assemble the content, and return it return implode( '<a ', $bits ); }
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论(0)