作者: chudafu

  • wordpress禁用Feed

    将以下代码插入到functions.php文件中,即可禁用Feed。

    function wpjzp_feed_disabled()
    {
        wp_die("Feed已经关闭, 请访问网站<a href="" . get_bloginfo("url") . "">首页</a>!");
    }
     
    add_action("do_feed", "wpjzp_feed_disabled", 1);
    add_action("do_feed_rdf", "wpjzp_feed_disabled", 1);
    add_action("do_feed_rss", "wpjzp_feed_disabled", 1);
    add_action("do_feed_rss2", "wpjzp_feed_disabled", 1);
    add_action("do_feed_atom", "wpjzp_feed_disabled", 1);
  • wordpress禁用xmlrpc

    将以下代码插入到functions.php文件中即可

    add_filter("xmlrpc_enabled", "__return_false");
    add_filter("xmlrpc_methods", function ($methods) {
        unset($methods["pingback.ping"]);
        return $methods;
    });
  • wordpress移除wp_head不常用代码

    把不常用的代码移除,让wordpress快起来,想要非一样的感觉,可以试试。

    remove_action("wp_head", "wp_generator");
    foreach (["rss2_head", "commentsrss2_head", "rss_head", "rdf_header", "atom_head", "comments_atom_head", "opml_head", "app_head"] as $action) {
        remove_action($action, "the_generator");  //删除 head 中的 WP 版本号
    }
    remove_action("wp_head", "rsd_link");                        //删除 head 中的 RSD LINK
    remove_action("wp_head", "wlwmanifest_link");                //删除 head 中的 Windows Live Writer 的适配器?
     
    remove_action("wp_head", "feed_links_extra", 3);            //删除 head 中的 Feed 相关的link
     
    remove_action("wp_head", "index_rel_link");                //删除 head 中首页,上级,开始,相连的日志链接
    remove_action("wp_head", "parent_post_rel_link", 10);
    remove_action("wp_head", "start_post_rel_link", 10);
    remove_action("wp_head", "adjacent_posts_rel_link_wp_head", 10);
     
    remove_action("wp_head", "wp_shortlink_wp_head", 10, 0);    //删除 head 中的 shortlink
    remove_action("wp_head", "rest_output_link_wp_head", 10);    // 删除头部输出 WP RSET API 地址
     
    remove_action("template_redirect", "wp_shortlink_header", 11);        //禁止短链接 Header 标签。
    remove_action("template_redirect", "rest_output_link_header", 11);    // 禁止输出 Header Link 标签。
  • wordpress禁止多地同时登录

    wordpress禁止用户在不同地点同时登录,管理员除外。

    function pcl_user_has_concurrent_sessions()
    {
        return (is_user_logged_in() && count(wp_get_all_sessions()) > 2);
    }
     
    add_action("init", function () {
        // 除了管理员,其他人不允许多地同时登陆。
        if (!current_user_can("manage_options")) {
            if (!pcl_user_has_concurrent_sessions()) {
                return;
            }
            $newest = max(wp_list_pluck(wp_get_all_sessions(), "login"));
            $session = pcl_get_current_session();
            if ($session["login"] === $newest) {
                wp_destroy_other_sessions();
            } else {
                wp_destroy_current_session();
            }
        }
    });
  • 实现wordpress一篇文章只允许同一IP评论一次

    在使用wordpress建站时,常常遇到被垃圾留言困扰,有些通过机器发垃圾留言,一发就是成百上千条,这个很烦人,因此,有些人干脆直接在wordpress网站上把留言评论功能给关闭了。

    如果你的wordpress主题必须要使用留言评论,有一个办法可以规避这个问题,即实现wordpress一篇文章只鸡同一IP的人评论一次就可以。

    将以下代码添加到functions.php中

    // 一篇文章只允许同一IP评论一次
    //获取评论用户的ip,参考wp-includes/comment.php
    function wdp_getIP() {
      $ip = $_SERVER['REMOTE_ADDR'];
      $ip = preg_replace( '/[^0-9a-fA-F:., ]/', '', $ip );
        
      return $ip;
    }
    function wdp_only_one_comment( $commentdata ) {
      global $wpdb;
      $currentUser = wp_get_current_user();
      
      // 不限制管理员发表评论
      if(empty($currentUser->roles) || !in_array('administrator', $currentUser->roles)) {
        $bool = $wpdb->get_var("SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = ".$commentdata['comment_post_ID']."  AND (comment_author = '".$commentdata['comment_author']."' OR comment_author_email = '".$commentdata['comment_author_email']."' OR comment_author_IP = '".wdp_getIP()."') LIMIT 0, 1;");
        if($bool)
          wp_die('留言已提交,请勿重复留言。<a href="'.get_permalink($commentdata['comment_post_ID']).'">点此返回</a>');
      }
    
      return $commentdata;
    }
    add_action( 'preprocess_comment' , 'wdp_only_one_comment', 20);
  • 自字义wordpress摘要显示的字数长度

    以下代码可以实现,自定义wordpress文章摘要显示的字数长度,数值为字符号,汉字占两个字符。

     
    function new_excerpt_length($length) {
    return 100;
    }
    add_filter('excerpt_length', 'new_excerpt_length');
    
    
  • 禁用WordPress中的搜索功能

    以下代码可以禁止使用wordpress中的搜索功能

     
    function wdp_filter_query( $query, $error = true ) {
     
    if ( is_search() ) {
    $query->is_search = false;
    $query->query_vars[s] = false;
    $query->query[s] = false;
     
    // to error
    if ( $error == true )
    $query->is_404 = true;
    }
    }
     
    add_action( 'parse_query', 'wdp_filter_query' );
    add_filter( 'get_search_form', create_function( '$a', "return null;" ) );
    
    
  • 给wordpress用户个人资料添加自定义字段

    在制作个人博客时,有时会需要显示作者的QQ或微信,下面这段代码就可以实现这个功能。

     
    function wdp_new_contactmethods( $contactmethods ) {
    // Add QQ
    $contactmethods['qq'] = 'qq';
    //add WeiXin
    $contactmethods['weixin'] = 'weixin';
     
    return $contactmethods;
    }
    add_filter('user_contactmethods','wdp_new_contactmethods',10,1);
    
    

    在需要调用的位置通过以下代码调用即可

     
    <?php echo $curauth->qq; ?>
    
    
  • 给wordpress仪表盘添加自定义图标

    wordpress后台仪表盘默认的图标是wordpress自带的,如果要将图片修改为自己的,只需要在function.php文件中加入以下代码。

    function wdp_custom_logo() {
    echo '
    <style type="text/css">
    #wpadminbar #wp-admin-bar-wp-logo > .ab-item .ab-icon:before {
    background-image: url(' . get_bloginfo('stylesheet_directory') . '/images/custom-logo.png) !important;
    background-position: 0 0;
    color:rgba(0, 0, 0, 0);
    }
    #wpadminbar #wp-admin-bar-wp-logo.hover > .ab-item .ab-icon {
    background-position: 0 0;
    }
    </style>
    ';
    }
    //hook into the administrative header output
    add_action('wp_before_admin_bar_render', 'wdp_custom_logo');
    
    
  • 删除wordpress版本号代码

    一段代码就可以删除wordpress的版本号,掌握这些就可以自己制作主题系列。

     
    function wpb_remove_version() {
    return '';
    }
    add_filter('the_generator', 'wpb_remove_version');