如何方便的在更新主题时保留 functions.php 里的自定义功能函数

2016-08-01 06:09 578 5 条评论 龙笑天下
导语: 本文主要介绍,如何高效正确的管理 functions.php 文件,以及在主题升级时,更好的更新 functions.php 文件,保留里面的自定义功能函数。方法有点多,总能找到适合你的!
Dragon主题购买

如何方便的在更新主题时保留 functions.php 里的自定义功能函数

相信大家对 wordpress 主题中的functions.php文件一定不陌生,这个文件是很多人折腾的最多的文件,本站很多教程中的代码也是直接加到 functions.php 就可以使用的,可是每次更新主题时这个文件都会被主题默认的给覆盖,所以更新主题前都得提前备份自定义的代码,这样就有点麻烦了!那么,怎么对 functions.php 进行高效正确的更新呢?

其实办法非常简单,以下有 3 种方法对 functions.php 文件进行管理,免去更新主题时复制黏贴大量代码的麻烦!

方法一

实现步骤

为 WordPress 引入其他 functions.php 文件,将下面的内容保存为 utf-8 格式的 php 文件,并起名为 functions-lxtx.php,放入跟主题的 functions.php 文件同目录;方法来自 @xzblog.cn

<?php
/**
 * Customize Function of Theme By ILXTX.COM
 * @author    龙笑天 <lxt@ilxtx.com>
 * @site      龙笑天下 <www.ilxtx.com>
 * @copyright Copyright (c) 2014-2016, Zhiyan
 * @link      https://www.ilxtx.com/update-functions.html
 */
?>
<?php
/* 请在下面加入自定义函数 */



?>

然后在 functions.php 中加入以下代码,在主题更新 functions.php 文件时保持加入下面这段代码就可以了,是不是很方便!

// 引入其它 functions.php 文件
@include(TEMPLATEPATH.'/functions-lxtx.php');

最后,和在 functions 文件里面加代码一样,在这个 functions-lxtx.php 文件里面最后一个?>前加入自定义功能代码就可以了。

举例

比如,这里有个小功能,就是“后台颜色随机切换”的代码:


// 后台颜色随机切换
function Bing_random_admin_color(){
static $color;
if( isset( $color ) ) return $color;
$color = array_keys( $GLOBALS['_wp_admin_css_colors'] );
$color = $color[array_rand( $color )];
return $color;
}
add_filter( 'get_user_option_admin_color', 'Bing_random_admin_color' );

将上面的代码加入那个 functions-lxtx.php 文件里面,就是下面这样的:

<?php
/**
 * Customize Function of Theme By ILXTX.COM
 * @author    龙笑天 <lxt@ilxtx.com>
 * @site      龙笑天下 <www.ilxtx.com>
 * @copyright Copyright (c) 2014-2016, Zhiyan
 * @link      https://www.ilxtx.com/update-functions.html
 */
?>
<?php
/* 请在下面加入自定义函数 */

// 后台颜色随即切换
function Bing_random_admin_color(){
static $color;
if( isset( $color ) ) return $color;
$color = array_keys( $GLOBALS['_wp_admin_css_colors'] );
$color = $color[array_rand( $color )];
return $color;
}
add_filter( 'get_user_option_admin_color', 'Bing_random_admin_color' );

?>

方法二

自己制作一个小插件,通过插件来加载自定义的函数功能。摘自 @云落

步骤如下

首先将下面的代码保存为 utf-8 格式的 php 文件,名字随意,最好英文或数字吧~~然后放进 wp-content/plugins 文件夹里面;

< ?php
/*
Plugin Name: 模版函数扩展插件
Description: 给主题模版函数库里添加点小功能
Author: 龙笑天下
*/
//文件里面加入代码就可以和 functions 文件一样了
 
?>

和在 functions 文件里面加代码一样,将功能代码加到上面的最后一个?>即可,然后在后台的插件列表里就可以看到这个自己制作的“模版函数扩展插件”了。这样一来,以后自定义功能代码都可以放到这个“自制”的小插件里,主题更新可以随意更新不用担心这些自定义代码丢失或被覆盖了。

方法三

将功能代码存为一个个独立的 php 文件来管理。来自 @v7v3

实现步骤

第一步:在主题文件夹下新建一个名为 inc 的文件夹

第二步:将以下代码加入到当前主题的 functions.php 文件中,代码如下:

define('INC', TEMPLATEPATH.'/inc');
IncludeAll( INC );
function IncludeAll($dir){
    $dir = realpath($dir);
    if($dir){
        $files = scandir($dir);
        sort($files);
        foreach($files as $file){
            if($file == '.' || $file == '..'){
                continue;
            }elseif(preg_match('/.php$/i', $file)){
                include_once $dir.'/'.$file;
            }
        }
    }
}

以后每次为主题扩展功能的时候只要将代码保存为一个新的 php 文件,文件名不限,但最好是英文或数字,然后将文件放入到 inc 目录下即可,效果和直接将代码加到 functions.php 文件一样,而且一旦代码出错只要直接删除出错代码的文件即可。

PS:以上几种方法,大家任选其一吧~~

「点点赞赏,手留余香」

还没有人赞赏,快来当第一个赞赏的人吧!

声明:本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

2016-07-17

2016-08-08

发表评论

表情 格式 贴图 链接 私密 签到
Dragon主题购买阿里云特价云服务器1核2G低至86元,N4共享型服务器3年仅需799元腾讯云特价云服务器1核2G 88元/年 2核4G3M688元/3年,更有千元代金券礼包免费领!
评论
正在努力加载中...
扫一扫二维码分享
×
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies. Learn more