×

Tổng hợp code function hay trong WordPress

Tổng hợp code function hay trong Wordpress caodem.com

Dưới đây là tổng hợp một số code thêm chức năng cho WordPress thông qua file function.php trong thư mục theme mà bạn đang sử dụng. Bạn chỉ cần sao chép code về rồi dán vào file function.php rồi lưu lại là xong.

Xóa thanh điều khiển Admin

add_filter( 'show_admin_bar', '__return_false' );

Nén code tăng tốc cho trang

function sanitize_output($buffer)
{
$search = array(
'/\>[^\S ]+/s',
'/[^\S ]+\</s',
'/(\s)+/s'
);
$replace = array(
'>',
'<',
'\\1'
);
$buffer = preg_replace($search, $replace, $buffer);
return $buffer;
}
ob_start("sanitize_output");

Đếm số ký tự trong bài viết

function post_word_count() {
global $post;
$words = str_word_count( strip_tags( get_post_field( 'post_content', get_the_ID() ) ) );
$time = $words/250;
if ( $time < 1) {
echo 1;
} else {
echo round($time, 1, PHP_ROUND_HALF_UP);
}
}

Để hiển thị ra bạn sử dụng đoạn code bên dưới

<?php echo post_word_count(); ?>

Thêm nhóm thành viên quản trị

$add_group_user = add_role('Modquanly', __( 'Mod quản lý' ),
array(
'upload_files' => true, // duoc phep ipload file
'edit_others_pages' => true, // duoc phep sua trang
'edit_posts' => true, // duoc phep su bai viet
'publish_posts' => true, // duoc phep xet duyet bai viet
'read' => true,
'level_3' => true,
)
);

Sử dụng font chữ Google cho WordPress

function google_fonts() {
wp_register_style( 'OpenSans', '//fonts.googleapis.com/css?family=Open+Sans:400,600,700,800' );
wp_enqueue_style( 'OpenSans' );
}
add_action( 'wp_print_styles', 'google_fonts' );

Hiển thị thời gian ở mục bình luận theo dạng bao nhiêu phút trước

function pressfore_comment_time_output($date, $d, $comment){
return sprintf( _x( '%s trước', '%s = human-readable time difference', 'your-text-domain' ), human_time_diff( get_comment_time( 'U' ), current_time( 'timestamp' ) ) );
}
add_filter('get_comment_date', 'pressfore_comment_time_output', 10, 3);

Phân biệt từng nhóm thành viên ở mục bình luận

if ( ! class_exists( 'WPB_Comment_Author_Role_Label' ) ) :
class WPB_Comment_Author_Role_Label {
public function __construct() {
add_filter( 'get_comment_author', array( $this, 'wpb_get_comment_author_role' ), 10, 3 );
add_filter( 'get_comment_author_link', array( $this, 'wpb_comment_author_role' ) );
}

function wpb_get_comment_author_role($author, $comment_id, $comment) {
$authoremail = get_comment_author_email( $comment);
if (email_exists($authoremail)) {
$commet_user_role = get_user_by( 'email', $authoremail );
$comment_user_role = $commet_user_role->roles[0];
$this->comment_user_role = ' <span class="comment-author-label comment-author-label-'.$comment_user_role.'">' . ucfirst($comment_user_role) . '</span>';
} else {
$this->comment_user_role = '';
}
return $author;
}
function wpb_comment_author_role($author) {
return $author .= $this->comment_user_role;
}
}
new WPB_Comment_Author_Role_Label;
endif;

Thêm đoạn css bên dưới để thay đổi màu cho từng nhóm thành viên.

.comment-author-label {
padding: 5px;
font-size: 14px;
border-radius: 3px;
}

.comment-author-label-editor {
color:#66666;
}
.comment-author-label-author {
color:#66666;
}

.comment-author-label-contributor {
color:#44444;
}
.comment-author-label-subscriber {
color:#00cc00;
}

.comment-author-label-administrator {
color:#ff3333;
}

Tắt Widget không dùng tới

add_action( 'widgets_init', function () {
unregister_widget( 'WP_Widget_Pages' );
unregister_widget( 'WP_Widget_Calendar' );
unregister_widget( 'WP_Widget_Archives' );
unregister_widget( 'WP_Widget_Links' );
unregister_widget( 'WP_Widget_Meta' );
unregister_widget( 'WP_Widget_Search' );
unregister_widget( 'WP_Widget_Text' );
unregister_widget( 'WP_Widget_Categories' );
unregister_widget( 'WP_Widget_Recent_Posts' );
unregister_widget( 'WP_Widget_Recent_Comments' );
unregister_widget( 'WP_Widget_RSS' );
unregister_widget( 'WP_Widget_Tag_Cloud' );
unregister_widget( 'WP_Nav_Menu_Widget' );
}, 11);

Chỉ Admin mới được phép truy cập vào trình quản lý WordPress

function lovend_redirect(){
$kiemtra = get_current_user_id();
if( is_admin() && !defined('DOING_AJAX') && ( current_user_can('editor') || current_user_can('author') || current_user_can('subscriber') || current_user_can('contributor') ) ){
wp_redirect( home_url() );
exit;
}
}
add_action('init','lovend_redirect');

Thêm ô nhập thông tin cho thành viên ở hồ sơ thành viên

function add_fields_user($profile_fields){
$profile_fields['phone'] = 'Phone';
$profile_fields['namsinh'] = 'Năm Sinh';
$profile_fields['facebook'] = 'Facebook';
return $profile_fields;
}
add_filter('user_contactmethods', 'add_fields_user');

Bỏ menu không cần thiết trong trình quản lý WordPress

add_action( 'admin_menu', function () {
// Bỏ Dashboard
remove_menu_page( 'index.php' );
// Bỏ Posts
remove_menu_page( 'edit.php' );
// Bỏ Posts -> Categories
remove_submenu_page( 'edit.php', 'edit-tags.php?taxonomy=category' );
// Bỏ Posts -> Tags
remove_submenu_page( 'edit.php', 'edit-tags.php?taxonomy=post_tag' );
// Bỏ Media
remove_menu_page( 'upload.php' );
// Bỏ Media -> Library
remove_submenu_page( 'upload.php', 'upload.php' );
// Bỏ Media -> Add new media
remove_submenu_page( 'upload.php', 'media-new.php' );
// Bỏ Pages
remove_menu_page( 'edit.php?post_type=page' );
// Bỏ Pages -> All pages
remove_submenu_page( 'edit.php?post_type=page', 'edit.php?post_type=page' );
// Bỏ Pages -> Add new page
remove_submenu_page( 'edit.php?post_type=page', 'post-new.php?post_type=page' );
// Bỏ Comments
remove_menu_page( 'edit-comments.php' );
// Bỏ Appearance
remove_menu_page( 'themes.php' );
// Bỏ Appearance -> Themes
remove_submenu_page( 'themes.php', 'themes.php' );
// Bỏ Appearance -> Customize
remove_submenu_page( 'themes.php', 'customize.php?return=' . urlencode( $_SERVER['REQUEST_URI'] ) );
// Bỏ Appearance -> Widgets
remove_submenu_page( 'themes.php', 'widgets.php' );
// Bỏ Appearance -> Menus
remove_submenu_page( 'themes.php', 'nav-menus.php.php' );
// Bỏ Appearance -> Editor
remove_submenu_page( 'themes.php', 'theme-editor.php' );
// Bỏ Plugins
remove_menu_page( 'plugins.php' );
// Bỏ Plugins -> Installed plugins
remove_submenu_page( 'plugins.php', 'plugins.php' );
// Bỏ Plugins -> Add new plugins
remove_submenu_page( 'plugins.php', 'plugin-install.php' );
// Bỏ Plugins -> Plugin editor
remove_submenu_page( 'plugins.php', 'plugin-editor.php' );
// Bỏ Users
remove_menu_page( 'users.php' );
// Bỏ Users -> Users
remove_submenu_page( 'users.php', 'users.php' );
// Bỏ Users -> New user
remove_submenu_page( 'users.php', 'user-new.php' );
// Bỏ Users -> Your profile
remove_submenu_page( 'users.php', 'profile.php' );
// Bỏ Tools
remove_menu_page( 'tools.php' );
// Bỏ Tools -> Available Tools
remove_submenu_page( 'tools.php', 'tools.php' );
// Bỏ Tools -> Import
remove_submenu_page( 'tools.php', 'import.php' );
// Bỏ Tools -> Export
remove_submenu_page( 'tools.php', 'export.php' );
// Bỏ Settings
remove_menu_page( 'options-general.php' );
// Bỏ Settings -> Writing
remove_submenu_page( 'options-general.php', 'options-writing.php' );
// Bỏ Settings -> Reading
remove_submenu_page( 'options-general.php', 'options-reading.php' );
// Bỏ Settings -> Discussion
remove_submenu_page( 'options-general.php', 'options-discussion.php' );
// Bỏ Settings -> Media
remove_submenu_page( 'options-general.php', 'options-media.php' );
// Bỏ Settings -> Permalinks
remove_submenu_page( 'options-general.php', 'options-permalink.php' );
}, 999);

Xóa nhóm thành viên mặc định trên WordPress

add_action( 'admin_init', function () {
remove_role( 'administrator' ); // nhóm admin
remove_role( 'editor' ); // nhóm biên tập viên
remove_role( 'author' ); // nhóm tác giả
remove_role( 'contributor' ); // nhóm cộng tác viên
remove_role( 'subscriber' ); // nhóm thành viên
});

Bỏ các chức năng không cần thiết trong mục Media

add_filter('media_view_strings', function ( $strings ) {
$strings['createGalleryTitle'] = null; // Bỏ nút "Create gallery"
$strings['createPlaylistTitle'] = null; // Bỏ nút "Create Audio Playlist"
$strings['createVideoPlaylistTitle'] = null; // Bỏ nút "Create Video Playlist"
$strings['insertFromUrlTitle'] = null; // Bỏ nút "Inset from URL"
return $strings;
});
16961
"Nếu bạn cảm thấy bài viết này hữu ích, bạn có thể ủng hộ cho tác giả một tách Cafe"
THANH TOÁN ỦNG HỘ
Sacombank
NGUYEN NGOC HOAN
040086597979
Đọc thêm bài khác
×
Chia sẻ trang này tới
QR:  Tổng hợp code function hay trong WordPress

1 Bình luận
  1. Dam CÁO

    Lấy 1 số cái nhưng toàn thấy lỗi đồng chí ơi



Gọi điệnMessengerZalo