V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
ivmm
V2EX  ›  问与答

Functions.php 的分与享

  •  
  •   ivmm · 2015-02-20 21:50:45 +08:00 · 2767 次点击
    这是一个创建于 3358 天前的主题,其中的信息可能已经有所发展或是发生改变。

    你的Wordpress秘籍都凝结在那么一份 functions.php 中吧。
    凭着,人人为我,我(ni)为(shi)人(gou)人(shi) 的精神,大家分享一下你的 functions.php 咯。

    这是我的: 也希望看到你的。

    <?php
    /*
    你可以把本来要写在 function.php 中的代码写在这里,
    而无须担心更新主题时 function.php 被覆盖。
    */

    //加载的css和js后面的版本号的问题
    if(!function_exists('cwp_remove_script_version')){
    function cwp_remove_script_version( $src ){ return remove_query_arg( 'ver', $src ); }
    add_filter( 'script_loader_src', 'cwp_remove_script_version' );
    add_filter( 'style_loader_src', 'cwp_remove_script_version' );
    }
    //WP前台顶部清理
    function cwp_header_clean_up(){
    if (!is_admin()) {
    foreach(array('wp_generator','rsd_link','index_rel_link','start_post_rel_link','wlwmanifest_link') as $clean){remove_action('wp_head',$clean);}
    remove_action( 'wp_head', 'feed_links_extra', 3 );
    remove_action( 'wp_head', 'feed_links', 2 );
    remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 );
    remove_action( 'wp_head', 'start_post_rel_link', 10, 0 );
    remove_action( 'wp_head', 'adjacent_posts_rel_link', 10, 0 );
    foreach(array('single_post_title','bloginfo','wp_title','category_description','list_cats','comment_author','comment_text','the_title','the_content','the_excerpt') as $where){
    remove_filter ($where, 'wptexturize');
    }
    /remove_filter( 'the_content', 'wpautop' );
    remove_filter( 'the_excerpt', 'wpautop' );
    /
    wp_deregister_script( 'l10n' );
    }
    }
    //移除某些WP自带的小工具
    function coolwp_remove_meta_widget() {
    unregister_widget('WP_Widget_Pages');
    unregister_widget('WP_Widget_Calendar');
    //unregister_widget('WP_Widget_Archives');
    unregister_widget('WP_Widget_Links');
    unregister_widget('WP_Widget_Meta');
    // unregister_widget('WP_Widget_Search');
    // unregister_widget('WP_Widget_Text');
    unregister_widget('WP_Widget_Categories');
    unregister_widget('WP_Widget_Recent_Posts');
    unregister_widget('WP_Widget_Recent_Comments');
    unregister_widget('WP_Widget_RSS');
    unregister_widget('WP_Widget_Tag_Cloud');
    unregister_widget('WP_Nav_Menu_Widget');
    /register my custom widget/
    }
    add_action( 'widgets_init', 'coolwp_remove_meta_widget',11 );

    //移除WP为仪表盘(dashboard)页面加载的小工具
    function cwp_remove_dashboard_widgets() {
    global $wp_meta_boxes;
    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_drafts']);
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);
    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
    }
    add_action('wp_dashboard_setup', 'cwp_remove_dashboard_widgets',11 );

    //Bonus:加载自定义jQuery库的链接
    /Using SinaApp jQuery CDN/
    function cwp_modify_jquery() {
    if (!is_admin()) {
    wp_deregister_script('jquery');
    wp_register_script('jquery', 'http://cdn.staticfile.org/jquery/2.1.1/jquery.min.js', false, '1.9.1');
    wp_enqueue_script('jquery');
    }
    }
    add_action('init', 'cwp_modify_jquery');

    /**
    * WordPress 后台禁用Google Open Sans字体,加速网站
    * http://www.wpdaxue.com/disable-google-fonts.html
    */
    add_filter( 'gettext_with_context', 'wpdx_disable_open_sans', 888, 4 );
    function wpdx_disable_open_sans( $translations, $text, $context, $domain ) {
    if ( 'Open Sans font: on or off' == $context && 'on' == $text ) {
    $translations = 'off';
    }
    return $translations;
    }

    不要笑,你的呢?

    12 条回复    2015-02-21 19:47:52 +08:00
    zjgood
        1
    zjgood  
       2015-02-20 21:59:25 +08:00 via Android
    lz你不说高三你要好好学习吗?你不是把站都关了吗?你为什么又要搞WordPress了哇?:)
    ivmm
        2
    ivmm  
    OP
       2015-02-20 22:12:49 +08:00
    @zjgood 寒假玩电脑的时间多了,手痒。。。
    已经把OS X黑苹果了,已经给手机安装WIN10,但觉得还是建站有兴趣。。


    ——————
    一般人我不告诉他,我得为三个月后做准备啊
    WildCat
        3
    WildCat  
       2015-02-20 22:57:19 +08:00 via iPhone
    虽然自己高中的时候也热衷于折腾 WordPress,不过还是建议楼主沉下心来刷三个月的题
    Yamade
        4
    Yamade  
       2015-02-20 23:06:55 +08:00
    测试了下.替换后wp挂了.
    ScotGu
        5
    ScotGu  
       2015-02-20 23:10:58 +08:00
    @Yamade 这才是正确姿势!! 23333333
    Yamade
        6
    Yamade  
       2015-02-20 23:11:52 +08:00
    @ScotGu 我去....被坑了?
    ivmm
        7
    ivmm  
    OP
       2015-02-20 23:13:08 +08:00
    @Yamade 要自己测试和主题、插件的兼容性,还有添加的位置不对,必挂
    sennes
        8
    sennes  
       2015-02-20 23:20:25 +08:00
    可以移动到合适的节点吗。
    0x1e240
        9
    0x1e240  
       2015-02-21 08:27:37 +08:00 via Android
    就G头像替换和友链输出俩个
    0x142857
        10
    0x142857  
       2015-02-21 08:39:06 +08:00 via Android
    分与享…… 楼主语文看来……
    geekzu
        11
    geekzu  
       2015-02-21 19:46:05 +08:00
    //后台字体切换雅黑
    function geekzu_admin_font(){
    echo'<style>*{font-family:"Microsoft YaHei";}</style>';
    }
    add_action('admin_head', 'geekzu_admin_font');

    //友情链接
    add_filter( 'pre_option_link_manager_enabled', '__return_true' );

    //判断当前文章是否被百度收录,若没有被收录则可点击提交至百度,加速收录!(此插件在文章页面仅管理员可见)
    function d4v($url){
    $url='http://www.baidu.com/s?wd='.$url;
    $curl=curl_init();
    curl_setopt($curl,CURLOPT_URL,$url);
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
    $rs=curl_exec($curl);
    curl_close($curl);
    if(!strpos($rs,'没有找到')){
    return 1;
    }else{
    return 0;
    }
    }
    add_filter( 'the_content', 'baidu_submit' );
    function baidu_submit( $content ) {
    if( is_single() && current_user_can( 'manage_options') )
    if(d4v(get_permalink()) == 1)
    $content="<p align=right>本文百度已收录!</p>".$content;
    else
    $content="<p align=right><b><a style=color:red target=_blank href=http://zhanzhang.baidu.com/sitesubmit/index?sitename=".get_permalink().">百度未收录!点击此处提交</a></b>(仅管理员可见)</p>".$content;
    return $content;
    }

    //防采集
    $ua = $_SERVER['HTTP_USER_AGENT'];
    $now_ua = array('FeedDemon ','ZmEu','Indy Library','oBot','jaunty'); //将恶意USER_AGENT存入数组
    if(!$ua) { //禁止空USER_AGENT,dedecms等主流采集程序都是空USER_AGENT,部分sql注入工具也是空USER_AGENT
    header("Content-type: text/html; charset=utf-8");
    wp_die('请勿采集,因为采集的站长木JJ! www.geekzu.cn');
    }else{
    foreach($now_ua as $value )
    if(eregi($value,$ua)) {
    header("Content-type: text/html; charset=utf-8");
    wp_die('请勿采集本站,因为采集的站长木JJ!www.geekzu.cn');
    }
    }

    //移除头部多余信息
    remove_action('wp_head','wp_generator'); //禁止在head泄露wordpress版本号
    remove_action('wp_head','rsd_link'); //移除head中的rel="EditURI"
    remove_action('wp_head','wlwmanifest_link'); //移除head中的rel="wlwmanifest"
    remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0 ); //rel=shortlink
    remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );//rel='prev' rel='next'
    remove_action('wp_head', 'rel_canonical' );//rel='canonical'

    //移除wp近期评论加载样式
    function remove_recent_comments_style() {
    global $wp_widget_factory;
    remove_action( 'wp_head' , array( $wp_widget_factory -> widgets['WP_Widget_Recent_Comments'] , 'recent_comments_style' ) );
    }
    add_action( 'widgets_init' , 'remove_recent_comments_style' );

    // 开启支持 文章形式
    add_theme_support( 'post-formats', array( 'aside', 'gallery' ) );

    //头像中转极客族公共加速服务 解决被墙问题
    function get_ssl_avatar($avatar) {
    $avatar = preg_replace('/.*\/avatar\/(.*)\?s=([\d]+)&amp;.*/','<img class="avatar avatar-$2" src="//sdn.geekzu.org/avatar/$1?s=$2" alt="" width="$2" height="$2" />',$avatar);
    return $avatar;
    }
    add_filter('get_avatar', 'get_ssl_avatar');

    //禁用半角符号自动转换为全角
    remove_filter('the_content', 'wptexturize');

    // 禁止全英文和日文评论
    function GeekZu_comment_post( $incoming_comment ) {
    $pattern = '/[一-龥]/u';
    $jpattern ='/[ぁ-ん]+|[ァ-ヴ]+/u';
    if(!preg_match($pattern, $incoming_comment['comment_content'])) {
    err( "评论请用天朝语言 Comments must include the Chinese(like“你好”)" );
    }
    if(preg_match($jpattern, $incoming_comment['comment_content'])){
    err( "日本人滚一边玩儿蛋去!Japanese Get out!日本語出て行け!" );
    }
    return( $incoming_comment );
    }
    add_filter('preprocess_comment', 'GeekZu_comment_post');

    //禁用Open Sans
    class Disable_Google_Fonts {
    public function __construct() {
    add_filter( 'gettext_with_context', array( $this, 'disable_open_sans' ), 888, 4 );
    }
    public function disable_open_sans( $translations, $text, $context, $domain ) {
    if ( 'Open Sans font: on or off' == $context && 'on' == $text ) {
    $translations = 'off';
    }
    return $translations;
    }
    }
    $disable_google_fonts = new Disable_Google_Fonts;
    add_filter('content_save_pre', 'auto_save_image');
    function auto_save_image($content) {
    $upload_path = '';
    $upload_url_path = get_bloginfo('url');
    geekzu
        12
    geekzu  
       2015-02-21 19:47:52 +08:00
    以上是本人博客的functions.php的一部分,希望能对大家有所帮助
    本人博客: http://www.geekzu.cn/
    公共加速服务: http://cdn.geekzu.org/
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2430 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 14:51 · PVG 22:51 · LAX 07:51 · JFK 10:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.