WordPress函数文件的实用技巧

文章目录
  1. 删除WordPress版本号
  2. 添加自定义仪表盘徽标
  3. 隐藏管理工具栏(在开发过程中)
  4. 隐藏登录错误
  5. 禁用XML-RPC
  6. 自动更新WordPress
  7. 自动更新所有插件
  8. 自动更新所有主题
  9. 更改WordPress中的默认Gravatar头像
  10. 颜色转换为RGB
  11. 删除WordPress中的默认图片链接
  12. 用第三方库替换默认的WordPress jQuery脚本
  13. 禁用对文章类型中的评论和trackback。
  14. 完全禁用评论功能
    1. 禁用评论框
    2. 隐藏现有评论
    3. 删除后台菜单中的“评论”页
    4. 重定向任何试图访问评论页面的用户
    5. 从仪表盘中删除评论metabox。
    6. 删除管理工具栏的评论链接
    7. 隐藏管理工具栏中的评论图标

WordPress主题有一个强大的functions.php文件。在functions.php文件中,您可以向WordPress主题添加独特功能。它可以用来连接到WordPress的核心功能,使您的主题更加模块化,可扩展性和功能性。

本文收集了一些对您的WordPress函数文件最有用的技巧。

删除WordPress版本号

只需在您的函数文件中添加这段代码即可。

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

您应该始终使用最新版本的WordPress。

添加自定义仪表盘徽标

想要让你的WordPress管理区变得“看不出是WordPress”?添加一个自定义仪表板标志是这个过程的第一步。

首先,您需要将您的自定义徽标上传到您的主题的images文件夹中,重命名为custom-logo.png。确保你的自定义标志是16×16像素的大小。

之后,你可以将这段代码添加到你的主题的函数文件中。

function wpb_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', 'wpb_custom_logo');

隐藏管理工具栏(在开发过程中)

show_admin_bar( false );

隐藏登录错误

隐藏登录错误/提示,并用自定义的消息替换掉。

function hide_login_errors(){    
    return 'Oepsss, you broke me!';
}
add_filter( 'login_errors' , 'hide_login_errors' );

禁用XML-RPC

如果您不想远程发布到您的博客文章,则应禁用此功能。这也将完全禁用整个XML-RPC类。

add_filter( 'wp_xmlrpc_server_class' , '__return_false' );
add_filter( 'xmlrpc_enabled' , '__return_false' );

自动更新WordPress

这将自动更新您的WordPress核心程序(包含主要和次要更新)。

define( 'WP_AUTO_UPDATE_CORE' , true );

自动更新所有插件

define( 'WP_AUTO_UPDATE_CORE' , true );

自动更新所有主题

add_filter( 'auto_update_theme' , '__return_true' );

更改WordPress中的默认Gravatar头像

见过博客上默认的神秘人头像吗?你可以轻松地用你自己的自定义头像来代替它。只需上传你想使用的图片作为默认头像,然后将这段代码添加到你的函数文件中。

function wpb_new_gravatar ($avatar_defaults) {
	$myavatar = '头像的网址,包括HTTP(S)前缀';
	$avatar_defaults[$myavatar] = "Default Gravatar";
	return $avatar_defaults;
}
add_filter( 'avatar_defaults', 'wpb_new_gravatar' );

颜色转换为RGB

输入函数十六进制代码(例如#eeeeee),返回RGB值数组。

function hexrgb($color){
    if (isset($color[0])&&$color[0] == '#')
        $color = substr($color, 1);

    if (strlen($color) == 6)
        list($r, $g, $b) = array($color[0].$color[1],
                                 $color[2].$color[3],
                                 $color[4].$color[5]);
    elseif (strlen($color) == 3)
        list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
    else
        return false;

    $r = hexdec($r); $g = hexdec($g); $b = hexdec($b);

    return $r.', '.$g.', '.$b;
}

这是一个辅助开发用的函数。

删除WordPress中的默认图片链接

默认情况下,当您在WordPress文章中插入图片时,它会自动链接到图片文件或附件页面。如果用户点击图片,他们就会被带到一个新的页面,而不是您的文章。

以下是您如何轻松地阻止WordPress自动链接到附件页面的方法。您所要做的就是在您的函数文件中添加这段代码。

function wpb_imagelink_setup() {
    $image_set = get_option( 'image_default_link_type' );
     
    if ($image_set !== 'none') {
        update_option('image_default_link_type', 'none');
    }
}
add_action('admin_init', 'wpb_imagelink_setup', 10);

用第三方库替换默认的WordPress jQuery脚本

如果你确定你想改变核心jQuery版本,在这种情况下,你可以在你的主题的functions.php文件中添加以下代码。

//=以谷歌提供的jQ为例
function modify_jquery() {
	if (!is_admin()) {
		wp_deregister_script('jquery-core');
		wp_register_script('jquery-core', 'http://ajax.lug.ustc.edu.cn/ajax/libs/jquery/1.11.3/jquery.min.js', true, '1.11.3');
		wp_enqueue_script('jquery-core');
		wp_deregister_script('jquery-migrate');
		wp_register_script('jquery-migrate', 'http://cdn.yourdomain.com/wp-includes/js/jquery/jquery-migrate.min.js', true, '1.2.1');
		wp_enqueue_script('jquery-migrate');
	}
}
add_action( 'init', 'register_jquery' );

禁用对文章类型中的评论和trackback。

function df_disable_comments_post_types_support() {
	$post_types = get_post_types();
	foreach ($post_types as $post_type) {
		if(post_type_supports($post_type, 'comments')) {
			remove_post_type_support($post_type, 'comments');
			remove_post_type_support($post_type, 'trackbacks');
		}
	}
}
add_action('admin_init', 'df_disable_comments_post_types_support');

完全禁用评论功能

以符合“非交互式网站”的要求。

每一段代码片段都可单独使用。

禁用评论框

function df_disable_comments_status() {
	return false;
}
add_filter('comments_open', 'df_disable_comments_status', 20, 2);
add_filter('pings_open', 'df_disable_comments_status', 20, 2);

隐藏现有评论

function df_disable_comments_hide_existing_comments($comments) {
	$comments = array();
	return $comments;
}
add_filter('comments_array', 'df_disable_comments_hide_existing_comments', 10, 2);

删除后台菜单中的“评论”页

function df_disable_comments_admin_menu() {
	remove_menu_page('edit-comments.php');
}
add_action('admin_menu', 'df_disable_comments_admin_menu');

重定向任何试图访问评论页面的用户

function df_disable_comments_admin_menu_redirect() {
	global $pagenow;
	if ($pagenow === 'edit-comments.php') {
		wp_redirect(admin_url()); exit;
	}
}
add_action('admin_init', 'df_disable_comments_admin_menu_redirect');

从仪表盘中删除评论metabox。

function df_disable_comments_dashboard() {
	remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
}
add_action('admin_init', 'df_disable_comments_dashboard');

删除管理工具栏的评论链接

function df_disable_comments_admin_bar() {
	if (is_admin_bar_showing()) {
		remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
	}
}
add_action('init', 'df_disable_comments_admin_bar');

隐藏管理工具栏中的评论图标

add_action( 'admin_bar_menu', 'clean_admin_bar', 999 );
function clean_admin_bar( $wp_admin_bar ) {
    $wp_admin_bar->remove_node( 'comments' );
}
https://www.buyprotheme.com/useful-tricks-for-the-wordpress-functions-file/