标签归档:wordpress

隐藏 WordPress 后台不常用功能菜单,增加后台易用性

对普通用户来说, WordPress 后台的有些菜单是没用的,比如多媒体、工具等,没用的东西显示出来,除了造成 WordPress 后台界面的复杂性增加,还会带来一些安全性上的问题,比如某天一个客户心血来潮,安装了某个主题或插件、或者把后台的某些菜单删除了,网站页面一下子就显示不正常了。

其实维护一个网站所需要的功能也就是发布或更新一下文章,其他的功能设置好之后,需要改动的情况真的很少。在把 WordPress 站点交付给普通用户使用之前,我们可以把对他们来说没用的菜单隐藏掉,让普通用户只访问自己需要的功能,下面的代码可以帮助我们隐藏掉 WordPress 后台一些不常用的功能。

//移除后台无用的菜单
add_action( 'admin_menu', function(){
    remove_menu_page( 'index.php' ); //仪表盘
    remove_menu_page( 'upload.php' ); //多媒体
    remove_menu_page( 'edit.php?post_type=page' ); //页面
    remove_menu_page( 'edit-comments.php' ); //评论
    remove_menu_page( 'plugins.php' ); //插件
    remove_menu_page( 'tools.php' ); //工具
    remove_menu_page( 'options-general.php' ); //设置
});

remove_menu_page 的参数就是后台页面 URL 地址中最后一个“/” 后面的字符串,除了 WordPress 内置的一些地址,我们还可以通过这个方面隐藏掉一些插件或主题添加的菜单,只需要把该页面地址最后一个斜杠后面的字符作为 remove_menu_page 的参数添加到 remove_menus 的函数里面即可,有些特殊的页面,需要把后台地址最后一个?后面的字符作为作为 remove_menu_page 的值。

把以上代码加入到当前主题的 functions.php 文件中,即可达到隐藏 WordPress 后台不常用功能菜单的目的。

https://www.wpzhiku.com/remove-unuesed-menu-in-wordpress-admin/

wordpress 导航栏高亮使用自定义样式名

add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);

function special_nav_class ($classes, $item) {
    if (in_array('current-page-ancestor', $classes) || in_array('current-menu-item', $classes) ){
        $classes[] = 'active ';
    }
    return $classes;
}

WordPress如何判断登录用户的角色

在WordPress主题或者插件开发的过程中,经常要遇到判断登录用户的角色,并根据不同的用户角色赋予不同的权限。下面总结两种比较常用的判断方法。

一、使用 current_user_can() 判断

current_user_can() 可以根据不同角色拥有的权限来判断用户角色,具体的用户权限,可以在Roles and Capabilities 中找到。

判断用户是否为管理员(Administrator)

if( current_user_can( 'manage_options' ) ) {
echo 'The current user is a administrator';
}

继续阅读

wordpress上传文件自动按日期更名

<?php
/*
Plugin Name: Uploaded Filename Sanitizer
Plugin URI: http://jerry.red/143.html
Description: 将所有上传的文件使用“日期_时间_三位随机数”方式重命名
Version: 1.0
Author: jerry
Author URI: http://jerry.red/
*/

function custom_upload_filter($name)
{
    $time = date('Ymd_His');
    $rand = sprintf('%03d', mt_rand(0, 999));
    $ext  = pathinfo($name, PATHINFO_EXTENSION);
    return "{$time}_{$rand}.{$ext}";
}

add_filter('sanitize_file_name', 'custom_upload_filter');

把这段代码另存为 uploaded-filename-sanitizer.php,上传到 wp-content/plugins 目录下,然后在后台启用这个插件即可。
如果不想以插件的形式使用,可以把这段代码粘贴到当前 WordPress 主题的 functions.php 中。请去掉本代码第一行的 <?php 和注释。

WordPress禁用自动更新邮件通知

将下面的代码添加到当前主题的 functions.php 中:

add_filter( 'auto_core_update_send_email', 'wpb_stop_auto_update_emails', 10, 4 );
function wpb_stop_update_emails( $send, $type, $core_update, $result ) {
if ( ! emptyempty( $type ) && $type == 'success' ) {
return false;
}
return true;
}

以上代码通过添加一个过滤器禁用自动更新邮件通知功能。

wordpress 注册/反注册 样式/脚本

function enqueue_scripts()
{
    // 反注册
    wp_deregister_style('font-awesome');
    wp_deregister_script('jquery');

    // 注册
    wp_enqueue_style('bootstrap', get_template_directory_uri() . '/bootstrap/css/bootstrap.min.css');
    wp_enqueue_script('jquery', get_template_directory_uri() . '/js/jquery.min.js');
    wp_enqueue_script('bootstrap', get_template_directory_uri() . '/bootstrap/js/bootstrap.min.js', ['jquery']);
    wp_enqueue_script('main', get_template_directory_uri() . '/js/main.js', false, false, true);
}

add_action('wp_enqueue_scripts', 'enqueue_scripts');

wordpress 删除多余代码,加速前端加载

function clean_head()
{
    // remove the links to the extra feeds such as category feeds
    remove_action('wp_head', 'feed_links_extra', 3);
    // remove REST API link tag
    remove_action('wp_head', 'rest_output_link_wp_head');
    // remove the link to the Really Simple Discovery service endpoint
    remove_action('wp_head', 'rsd_link');
    // remove the link to the Windows Live Writer manifest file
    remove_action('wp_head', 'wlwmanifest_link');
    // remove rel=canonical for singular queries
    remove_action('wp_head', 'rel_canonical');
    // remove relational links for the posts adjacent to the current post for single post pages
    remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10);
    // remove shortlink
    remove_action('wp_head', 'wp_shortlink_wp_head');
    // remove oEmbed
    remove_action('wp_head', 'wp_oembed_add_discovery_links');
    remove_action('wp_head', 'wp_oembed_add_host_js');
    // 移除 wordpress 信息,防止被恶意扫描
    remove_action('wp_head', 'wp_generator');
    // 移除cdn预读
    remove_action('wp_head', 'wp_resource_hints', 2);
    // 移除emoji
    remove_action('wp_head', 'print_emoji_detection_script', 7);
    remove_action('wp_print_styles', 'print_emoji_styles');
}

add_action('after_setup_theme', 'clean_head');

wordpress中如何用User Role Editor修改使用Meta Slider的权限

1、添加以下代码添加到functions.php

/**
 * You can change this to a different capability by pasting the code below into
 * your themes functions.php file.
 *
 * You can use the 'User Role Editor' plugin to add a custom capability to WordPress
 * specifically for Meta Slider users.
 */
function metaslider_change_required_role($capability) {
    return 'metaslider_use'; // this is the ID of a custom capability added using User Role Editor
}
add_filter('metaslider_capability', 'metaslider_change_required_role');

2、打开 用户User Role Editor
3、选择要修改权限用的角色
4、点击 Add Capability 添加 metaslider_use,然后钩选该属性
5、点击 UpdateYes

初级WordPress模板制作入门

一套完整的WordPress模板应至少具有如下文件:

style.css : CSS(样式表)文件
index.php : 主页模板
archive.php : Archive/Category模板
404.php : Not Found 错误页模板
comments.php : 留言/回复模板
footer.php : Footer模板
header.php : Header模板
sidebar.php : 侧栏模板
page.php : 内容页(Page)模板
single.php : 内容页(Post)模板
searchform.php : 搜索表单模板
search.php : 搜索结果模板

当然,具体到特定的某款模板,可能不止这些文件,但一般而言,这些文件是每套模板所必备的。
继续阅读