今天 osx86 问我,说网上找的代码用到 begin 主题上,会导致下载按钮(href=#button)也被替换,导致无法弹出下载界面。其实张戈博客之前分享过一个博客外链转内链的跳转到代码,可以兼容 HotNewspro 主题的下载按钮和文章索引,于是我要他站内搜索下。
他用了后发现确实可以,不过给我反馈了一个问题:确实滤过了下载按钮和索引,但是弹出层上的下载链接没有被替换成跳转链。
我随即看了下,发现确实是这样。因为代码是在文章 content 过滤的,而下载按钮是独立的模板,所以并不会生效。
唉,世上无难事,依葫芦画瓢是也!
方法很简单,找到下载按钮相关模板文件,然后替换不就得了吗?且往下看。
一、新增函数
将如下 php 代码添加到 主题目录下的 functions.php 文件中:
/**
* WordPress 简单的外链判断和跳转函数,兼容非常规的 a 标签
* 文章地址:https://zhang.ge/5041.html
* 跳转代码:https://zhang.ge/2703.html
*/
function link_nofollow($url)
{
if(strpos($url,'://')!==false && strpos($url,home_url())===false)
{
$url = str_replace($url, home_url()."/go/?url=".$url,$url);
}
return $url;
}
这个函数使用很简单,直接传入链接,函数会自动判断,并绝对会返回一个内链,比如:
<?php echo link_nofollow("http://www.baidu.com");?>
就会输出:
https://zhang.ge/go/?url=http://www.baidu.com
需要配合张戈博客之前分享的跳转代码,详见 https://zhang.ge/2703.html。
二、修改模板
已经有了替换函数了,下一步就是找到你要替换的地方,将原先的值传入函数即可。Begin 的下载弹出层代码位于 begin/inc/file.php
原始代码如下:
<?php if ( get_post_meta($post->ID, 'button1', true) ) : ?>
<div id="button_box">
<div id="button_file">
<h3>文件下载</h3>
<div class="file_ad" align="center"><?php echo stripslashes( zm_get_option('ad_f') ); ?></div>
<div class="buttons">
<?php if ( get_post_meta($post->ID, 'button1', true) ) : ?>
<?php $button1 = get_post_meta($post->ID, 'button1', true); ?>
<?php $url1 = get_post_meta($post->ID, 'url1', true); ?>
<a href="<?php echo $url1; ?>" rel="external nofollow" target="_blank"><?php echo $button1; ?></a>
<?php endif; ?>
<?php if ( get_post_meta($post->ID, 'button2', true) ) : ?>
<?php $button2 = get_post_meta($post->ID, 'button2', true); ?>
<?php $url2 = get_post_meta($post->ID, 'url2', true); ?>
<a href="<?php echo $url2; ?>" rel="external nofollow" target="_blank"><?php echo $button2; ?></a>
<?php endif; ?>
<?php if ( get_post_meta($post->ID, 'button3', true) ) : ?>
<?php $button3 = get_post_meta($post->ID, 'button3', true); ?>
<?php $url3 = get_post_meta($post->ID, 'url3', true); ?>
<a href="<?php echo $url3; ?>" rel="external nofollow" target="_blank"><?php echo $button3; ?></a>
<?php endif; ?>
<?php if ( get_post_meta($post->ID, 'button4', true) ) : ?>
<?php $button4 = get_post_meta($post->ID, 'button4', true); ?>
<?php $url4 = get_post_meta($post->ID, 'url4', true); ?>
<a href="<?php echo $url4; ?>" rel="external nofollow" target="_blank"><?php echo $button4; ?></a>
<?php endif; ?>
</div>
<div class="clear"></div>
</div>
</div>
<?php endif; ?>
其中的 url1-4 就是我们要处理的目标,所以如下修改即可:
<?php if ( get_post_meta($post->ID, 'button1', true) ) : ?>
<div id="button_box">
<div id="button_file">
<h3>文件下载</h3>
<div class="file_ad" align="center"><?php echo stripslashes( zm_get_option('ad_f') ); ?></div>
<div class="buttons">
<?php if ( get_post_meta($post->ID, 'button1', true) ) : ?>
<?php $button1 = get_post_meta($post->ID, 'button1', true); ?>
<!-- 用我们自定义的函数过滤一下即可 -->
<?php $url1 = link_nofollow(get_post_meta($post->ID, 'url1', true)); ?>
<a href="<?php echo $url1; ?>" rel="external nofollow" target="_blank"><?php echo $button1; ?></a>
<?php endif; ?>
<?php if ( get_post_meta($post->ID, 'button2', true) ) : ?>
<?php $button2 = get_post_meta($post->ID, 'button2', true); ?>
<?php $url2 = link_nofollow(get_post_meta($post->ID, 'url2', true)); ?>
<a href="<?php echo $url2; ?>" rel="external nofollow" target="_blank"><?php echo $button2; ?></a>
<?php endif; ?>
<?php if ( get_post_meta($post->ID, 'button3', true) ) : ?>
<?php $button3 = get_post_meta($post->ID, 'button3', true); ?>
<?php $url3 = link_nofollow(get_post_meta($post->ID, 'url3', true)); ?>
<a href="<?php echo $url3; ?>" rel="external nofollow" target="_blank"><?php echo $button3; ?></a>
<?php endif; ?>
<?php if ( get_post_meta($post->ID, 'button4', true) ) : ?>
<?php $button4 = get_post_meta($post->ID, 'button4', true); ?>
<?php $url4 = link_nofollow(get_post_meta($post->ID, 'url4', true)); ?>
<a href="<?php echo $url4; ?>" rel="external nofollow" target="_blank"><?php echo $button4; ?></a>
<?php endif; ?>
</div>
<div class="clear"></div>
</div>
</div>
<?php endif; ?>
保存后,文章下载弹出层里面的外链就变成了内链跳转的模式了。
代码和方法都非常简单,有需要的自己折腾吧!不清楚的可以参考一下之前分享 2 篇文章:



//评论者链接重定向 add_filter('widget_text', 'add_redirect_comment_link', 99); function add_redirect_comment_link($text = ''){ $text=str_replace('href="', 'href="'.get_option('home').'/go/?url=', $text); return $text; }这代码也能实现替换小工具的,但是怎么判断自己的URL不替换呢?
沙发 :grin:
之前用的就是你这里分享的方法 大赞
我只是抢沙发的
不明绝叼……
兼容非常规的a标签,这个不错。
[互粉]给力,支持
张哥,不知你这个评论框下面那些按钮怎么实现的呢?
看下这个:http://www.hicc.cc/wordpress-to-add-simple-text-editor-code-limited-lifting-of-comments-add-color-picker-to-achieve-fade-effect.html
我又搜来了 谢谢
很好的博客!分享很给力!
请问张哥,为何你的博客在搜狗兼容模式下,出现错位呢?
这段代码把文章页里面的图片链接给跳转了,导致图片的弹出层显示不了图片。该怎么破?
已修复这个问题,请到这篇文章
https://zhang.ge/4683.html
中拿最新代码覆盖到functions.php
弹出下载功能怎么集成哦?谢谢 :razz:
主题自带功能,没有研究。。。