我们在更新wordpress主题的时候,会导致functions-php
中的自定义代码被覆盖,更新的时候一般都是上传后覆盖源文件,但是如果我们新建一个更新包中没有的文件,这样就不会被覆盖,最后只需要在functions-php
中添加一条引入语句即可。目前博主使用的子比主题是自带这个功能的,支持在线更新,而不用担心被覆盖,这里就给大家演示如何操作。
具体操作
- 如果您需要添加一些自定义的PHP代码
- 您可以在当前目录下新建一个 func.php 的文件,写入你的php代码
- 主题会自动判断文件进行引入
- 使用此方式更新主题的时候func.php文件的内容将不会被覆盖
- 当然需要注意php的代码规范,错误代码将会引起网站严重错误!
首先在 functions-php
同级目录新建一个 func.php
的文件,然后添加自定义代码,一定要以 <?php
开头,例如:
<?php
/**
* WordPress 自动为文章标签添加该标签的链接
*/
function wpkj_auto_add_tag_link($content){
$limit = 3; // 设置同一个标签添加几次链接
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
$link = get_tag_link($tag->term_id);
$keyword = $tag->name;
$cleankeyword = stripslashes($keyword);
$url = '<a target="_blank" href="'.$link.'" title="'.str_replace('%s', addcslashes($cleankeyword, '$'), __('View all posts in %s')).'">'.addcslashes($cleankeyword, '$').'</a>';
$regEx = '\'(?!((<.*?)|(<a.*?)))('. $cleankeyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))\'s';
$content = preg_replace($regEx,$url,$content,$limit);
}
}
return $content;
}
add_filter( 'the_content', 'wpkj_auto_add_tag_link', 1 );
/**
* WordPress外链自动新窗口打开并添加nofollow属性
* https://beiwangshan.com/archives/667.html
*/
add_filter( 'the_content', 'cn_nf_url_parse');
function cn_nf_url_parse( $content ) {
$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>";
if(preg_match_all("/$regexp/siU", $content, $matches, PREG_SET_ORDER)) {
if( !empty($matches) ) {
$srcUrl = get_option('siteurl');
for ($i=0; $i < count($matches); $i++)
{
$tag = $matches[$i][0];
$tag2 = $matches[$i][0];
$url = $matches[$i][0];
$noFollow = '';
$pattern = '/target\s*=\s*"\s*_blank\s*"/';
preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
if( count($match) < 1 )
$noFollow .= ' target="_blank" ';
$pattern = '/rel\s*=\s*"\s*[n|d]ofollow\s*"/';
preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
if( count($match) < 1 ) $noFollow .= ' rel="nofollow" '; $pos = strpos($url,$srcUrl); if ($pos === false) { $tag = rtrim ($tag,'>');
$tag .= $noFollow.'>';
$content = str_replace($tag2,$tag,$content);
}
}
}
}
$content = str_replace(']]>', ']]>', $content);
return $content;
}
//WP文章内容中加入广告位
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = '<div>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8781808763705766"
crossorigin="anonymous"></script>
<!-- 文章页脚 -->
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-8781808763705766"
data-ad-slot="8132682838"
data-ad-format="auto"
data-full-width-responsive="true"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>';
if ( is_single() && ! is_admin() ) {
// 下面一行数字2代表段落 第一个段落后面加入广告位
return prefix_insert_after_paragraph( $ad_code, 5, $content );
}
return $content;
}
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;
}
}
return implode( '', $paragraphs );
}
//WordPress文章部分内容关注微信公众号后可见
function weixingzh_secret_content($atts, $content=null){
extract(shortcode_atts(array('key'=>null,'keyword'=>null), $atts));
if(isset($_POST['secret_key']) && $_POST['secret_key']==$key){
return '<div class="hidden-box show"><div class="hidden-text">[输入密码后可见]隐藏内容</div><div class="secret-password">'.$content.'</div></div>';
} else {
return '
<div class="gzhhide">
<div class="gzhtitle">抱歉!隐藏内容,请输入密码后可见!<i class="fa fa-lock"></i><span></span></div>
<div class="gzh-content">请打开微信扫描右边的二维码回复关键字「<span><b>'.$keyword.'</b></span>」获取密码,也可以微信直接搜索「科技毒瘤君」关注微信公众号获取密码。
<div class="gzhcode" style="background: url(https://cdn.beiwangshan.com/uploads/images/blog10/20220118175928.png);background-size: 100%;" width="150" height="150" alt="科技毒瘤就微信公众号"></div>
</div>
<div class="gzhbox"><form action="'.get_permalink().'" method="post">
<input type="password" size="20" name="secret_key">
<button type="submit">立即提取</button></form></div></div>';
}
}
add_shortcode('weixingzh', 'weixingzh_secret_content');
// 后台文本编辑框中添加公众号隐藏简码按钮
function wpsites_add_weixingzh_quicktags() {
if (wp_script_is('quicktags')){
?>
<script type="text/javascript">
QTags.addButton( 'weixingzh', '公众号隐藏', '【weixingzh keyword="关键字" key="验证码"】隐藏内容【/weixingzh】',"" );
</script>
<?php
}
}
add_action( 'admin_print_footer_scripts', 'wpsites_add_weixingzh_quicktags' );
然后,在functions-php
中,加入如下的代码
/**
* 担心wordpress主题更新导致functions-php中的自定义代码被覆盖,试试这种方式,一键备份
* https://www.beiwangshan.com/2076.html
*/
if (file_exists(get_theme_file_path('/func.php'))) {
require_once get_theme_file_path('/func.php');
}
最后保存,刷新网页查看效果!
需要注意的就是
需要注意php的代码规范,错误代码将会引起网站严重错误!
© 版权声明
THE END
暂无评论内容