将以下代码插入到functions.php文件中即可
add_filter("xmlrpc_enabled", "__return_false");
add_filter("xmlrpc_methods", function ($methods) {
unset($methods["pingback.ping"]);
return $methods;
});
将以下代码插入到functions.php文件中即可
add_filter("xmlrpc_enabled", "__return_false");
add_filter("xmlrpc_methods", function ($methods) {
unset($methods["pingback.ping"]);
return $methods;
});
把不常用的代码移除,让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禁止用户在不同地点同时登录,管理员除外。
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建站时,常常遇到被垃圾留言困扰,有些通过机器发垃圾留言,一发就是成百上千条,这个很烦人,因此,有些人干脆直接在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文章摘要显示的字数长度,数值为字符号,汉字占两个字符。
function new_excerpt_length($length) {
return 100;
}
add_filter('excerpt_length', 'new_excerpt_length');
以下代码可以禁止使用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;" ) );
在制作个人博客时,有时会需要显示作者的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自带的,如果要将图片修改为自己的,只需要在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的版本号,掌握这些就可以自己制作主题系列。
function wpb_remove_version() {
return '';
}
add_filter('the_generator', 'wpb_remove_version');
wordpress作为一个经典的建站系统,只要你懂技术,可以通过wordpress搭建出,任何形式的网站。要想搭建wordpress网站,wordpress函数是必须要熟练掌握的。
下面这些就是本人整理的一些wordpress建站必备的函数
一、基本的条件判断函数
is_home():是否为主页
is_single():是否为内容页 (Post)
is_page():是否为内容页 (Page)
is_category():是否为 Category/Archive 页
is_tag():是否为标签 (Tag) 存档页
is_date():是否为指定日期存档页
is_year():是否为指定年份存档页
is_month():是否为指定月份存档页
is_day():是否为指定日存档页
is_time():是否为指定时间存档页
is_archive():是否为存档页
is_search():是否为搜索结果页
is_404():是否为 "HTTP 404: Not Found" 错误页
is_paged():主页 /Category/Archive 页是否以多页显示
二、header部分常用函数
<?php bloginfo('name'); ?>:博客名称 (Title)
<?php bloginfo('stylesheet_url'); ?>:CSS 文件路径
<?php bloginfo('pingback_url'); ?>:PingBack URL
<?php bloginfo('template_url'); ?>:模板文件路径
<?php bloginfo('version'); ?>:WordPress 版本
<?php bloginfo('atom_url'); ?>:Atom URL
<?php bloginfo('rss2_url'); ?>:RSS 2.o URL
<?php bloginfo('url'); ?>:博客 URL
<?php bloginfo('html_type'); ?>:博客网页 HTML 类型
<?php bloginfo('charset'); ?>:博客网页编码
<?php bloginfo('description'); ?>:博客描述
<?php wp_title(); ?>:特定内容页 (Post/Page) 的标题
三、wordpress模板制作过程中常用到的函数和命令
<?php get_header(); ?>:调用 Header 模板
<?php get_sidebar(); ?>:调用 Sidebar 模板
<?php get_footer(); ?>:调用 Footer 模板
<?php the_content(); ?>:显示内容 (Post/Page)
<?php if(have_posts()):?>:检查是否存在 Post/Page
<?php while(have_posts()):the_post(); ?>:如果存在Post/Page则予以显示
<?php endwhile; ?>:While 结束
<?php endif; ?>:If 结束
<?php the_time('字符串') ?>:显示时间,时间格式由"字符串"参数决定,具体参考 PHP 手册
<?php comments_popup_link(); ?>:正文中的留言链接,如果使用 comments_popup_script(); 则新窗口打开链接
<?php the_title(); ?>:内容页 (Post/Page) 标题
<?php the_permalink() ?>:内容页 (Post/Page) URL
<?php the_category(',') ?>:特定内容页 (Post/Page) 所属 Category
<?php the_author(); ?>:作者
<?php the_ID(); ?>:特定内容页 (Post/Page) ID
<?php edit_post_link(); ?>:如果用户已登录并具有权限,显示编辑链接
<?php get_links_list(); ?>:显示 Blogroll 中的链接
<?php comments_template(); ?>:调用留言/回复模板
<?php wp_list_pages(); ?>:显示 Page 列表
<?php wp_list_categories(); ?>:显示 Categories 列表
<?php next_post_link('%link '); ?>:下一篇文章链接
<?php previous_post_link('%link'); ?>:上一篇文章链接
<?php get_calendar(); ?>:日历
<?php wp_get_archives() ?>:显示内容存档
<?php posts_nav_link(); ?>:导航,显示上一篇/下一篇文章链接
<?php include(TEMPLATEPATH . '/文件名'); ?>:嵌入其他文件,可为定制的模板或其他类型文件
四、与wordpress模板相关的一些其它函数
<?php _e('Message'); ?>:输出相应信息
<?php wp_register(); ?>:显示注册链接
<?php wp_loginout(); ?>:显示登录/注销链接
<!–next page–>:将当前内容分页
<!–more–>:将当前内容截断,以不在主页/目录页显示全部内容
<?php timer_stop(1); ?>:网页加载时间(秒)
<?php echo get_num_queries(); ?>:网页加载查询量