functions.php常用函数

建站笔记之functions.php函数

Published in Dec-27-2018


文章目录

在设计WordPress主题时,在functions.php文件里添加一套通用的自定义函数将会大大提高开发效率,这样就不必每次开发主题时都需先查找然后复制同样的函数。这里记录一些常用的函数,方便以后使用!

解决Gravatar头像不显示的问题

if ( ! function_exists( 'get_cravatar_url' ) ) {
/**
* 解决Gravatar头像不显示的问题
*
*/
function get_cravatar_url( $url ) {
$sources = array(
'www.gravatar.com',
'0.gravatar.com',
'1.gravatar.com',
'2.gravatar.com',
'secure.gravatar.com',
'cn.gravatar.com'
);
return str_replace( $sources, 'cravatar.cn', $url );
}
add_filter( 'um_user_avatar_url_filter', 'get_cravatar_url', 1 );
add_filter( 'bp_gravatar_url', 'get_cravatar_url', 1 );
add_filter( 'get_avatar_url', 'get_cravatar_url', 1 );
}
if ( ! function_exists( 'set_defaults_for_cravatar' ) ) {
/**
* 替换WordPress讨论设置中的默认头像
*/
function set_defaults_for_cravatar( $avatar_defaults ) {
$avatar_defaults['gravatar_default'] = 'Cravatar 标志';
return $avatar_defaults;
}
add_filter( 'avatar_defaults', 'set_defaults_for_cravatar', 1 );
}

禁止纯英文评论,禁止辣鸡评论

//禁止纯英文评论
function refused_spam_comments( $comment_data ) { 
  $pattern = '/[一-龥]/u'; 
  if(!preg_match($pattern,$comment_data['comment_content'])) { 
  wp_die('评论必须含中文!'); 
} 
  return( $comment_data ); 
} 
add_filter('preprocess_comment','refused_spam_comments');

给头部添加feed链接

// 给头部添加feed链接

if (function_exists(‘automatic_feed_links’)) {

automatic_feed_links();

} else {

return;

}

启用嵌套评论

一般来说,启用嵌套评论需要在头部区域添加一小段代码到wp_head 标签的前面。可以在functions.php文件里添加这段代码:

// 起用嵌套评论

function enable_threaded_comments(){

if (!is_admin()) {

if (is_singular() AND comments_open() AND (get_option(‘thread_comments’) == 1))

wp_enqueue_script(‘comment-reply’);

}

}

add_action(‘get_header’, ‘enable_threaded_comments’);

这有助于保持 <head> 文件的整洁性,注意,这个函数需要放置在jQuery-inclusion函数的后面才能正常运作。

删除Head区域多余东西

WordPress <head>文件里含有大量的多余东西, 诸如,版本号、WLW、RSD和索引链接。为了清除这些不必要信息,可以在functions.php文件里添加下面的代码:

// remove junk from head

remove_action(‘wp_head’, ‘rsd_link’);

remove_action(‘wp_head’, ‘wp_generator’);

remove_action(‘wp_head’, ‘feed_links’, 2);

remove_action(‘wp_head’, ‘index_rel_link’);

remove_action(‘wp_head’, ‘wlwmanifest_link’);

remove_action(‘wp_head’, ‘feed_links_extra’, 3);

remove_action(‘wp_head’, ‘start_post_rel_link’, 10, 0);

remove_action(‘wp_head’, ‘parent_post_rel_link’, 10, 0);

remove_action(‘wp_head’, ‘adjacent_posts_rel_link’, 10, 0);

自定义摘要的长度

使用下面这个函数就可以给摘要指定任何长度而不用受默认的55字的限制。

//自定义文章摘要长度

function custom_excerpt_length($length) {

return 20;

}

add_filter(‘excerpt_length’, ‘custom_excerpt_length’);

   只需要将 “20” 替换为任何你需要的字数。

自定义摘要后 “继续阅读”字符串

不管你怎么称呼这个方括号里的省略号[…]” ,总之这是WordPress默认的紧跟摘要 后面部分,我想删除方括号,使用下面这段代码你可以对它进行任何更改:

// 自定义“继续阅读”后面的字符

function custom_excerpt_more($more) {

return ‘…’;

}

add_filter(‘excerpt_more’, ‘custom_excerpt_more’);

给博客添加图标

如果你想给博客添加个图标,下面的代码将会非常实用。创建完图标后只要上传图片到网站的根目录下即可。只要在functions.php文件的 <head>区域添加下面的几行代码:

// 给博客添加图标

function blog_favicon() {

echo ‘<link rel=”Shortcut Icon” type=”image/x-icon” href=”‘.get_bloginfo(‘wpurl’).’/favicon.ico” />’;

}

add_action(‘wp_head’, ‘blog_favicon’);

   你可以随意更改目录,同时确保wp_head包含在你的主题 header.php文件里。

给博客后台添加一个不同的图标

有必要给WordPress后台添加一个特别的图标,这样被收藏为书签或是处理标签时就更加容易认出。只要将图标上传到主题的/images/ 目录下,加上下面的代码即可:

// 博客后台添加图标

function admin_favicon() {

echo ‘<link rel=”Shortcut Icon” type=”image/x-icon”
		
			href=”‘.get_bloginfo(‘stylesheet_directory’).’/images/favicon.png” />’;

}

add_action(‘admin_head’, ‘admin_favicon’);

像前面一样,同样可以随意更改目录。不过最好将后台图标和前台图标分开放在不同的目录下。

自定义后台登陆图标

是否想利用WordPress图标在各个登陆页面给自己做宣传?那么,可以将这个WordPress图标替换为其他自定义图片,创建自定义登陆图片,并将其命名为“custom-login-logo.png”将图片上传至主题的/images/ 目录下,用下面的代码:

// 自定义后台登录图标

function custom_login_logo() {

echo ‘<style type=”text/css”>

h1 a { background-image: 
		
url(‘.get_bloginfo(‘template_directory’).’/images/custom-login-logo.png) !important; }

</style>’;

}

add_action(‘login_head’, ‘custom_login_logo’);

   这里关键是要你设置路径和图片名称一致。另外,在创建图片的时候,记住图片的属性:宽为30px, 高为31px,透明GIF格式,头部背景色#464646 。

禁用无用的小工具区域

非常方便的函数,可用于删除主题中不需要的小工具区域,这是自定义主题必不可少的一个函数:

//禁用无用的小工具区域

function disable_all_widgets($sidebars_widgets) {

//if (is_home())

$sidebars_widgets = array(false);

return $sidebars_widgets;

}

add_filter(‘sidebars_widgets’, ‘disable_all_widgets’);

   这个代码属于即插即用型,不需要任何更改。注意:如果只想在主页禁用小工具,那么就将第三栏的 “//”删除。

删除WordPress更新提示

// 删除WordPress更新提示

if (!current_user_can(‘edit_users’)) {

add_action(‘init’, create_function(‘$a’, “remove_action(‘init’, ‘wp_version_check’);”), 2);

add_filter(‘pre_option_update_core’, create_function(‘$a’, “return null;”));

}

在body_class 与 post_class中加入分类ID

默认情况下,WordPress body_class和 post_class并没有包含当前文章的分类ID。 不过,可以用下面的代码来实现:

// 在body_class 与 post_class中加入分类ID

function category_id_class($classes) {

global $post;

foreach((get_the_category($post->ID)) as $category)

$classes [] = ‘cat-’ . $category->cat_ID . ‘-id’;

return $classes;

}

add_filter(‘post_class’, ‘category_id_class’);

add_filter(‘body_class’, ‘category_id_class’);

即使没有使用分类ID,这个函数还是非常好使的。



评论

曹同学的bilibili 曹同学的知乎 曹同学的email
返回网站顶部
正在获取,请稍候...
00:00/00:00