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('留言已提交,请勿重复留言。点此返回');
  }

  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');

原文

http://www.gaomozi.com/jianzhan/4768.html 

你可能感兴趣的:(wordpress,wordpress)