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.
add_filter( 'show_admin_bar', '__return_false' );
function sanitize_output($buffer){$search = array('/>[^S ]+/s','/[^S ]+'/(s)+/s');$replace = array('>','<','1');$buffer = preg_replace($search, $replace, $buffer);return $buffer;}ob_start("sanitize_output");
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
$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,));
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' );
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);
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 = ' ' . ucfirst($comment_user_role) . '';} 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;
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 = ' ' . ucfirst($comment_user_role) . '';} 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;}
.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;}
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);
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');
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');
add_action( 'admin_menu', function () {// Bỏ Dashboardremove_menu_page( 'index.php' );// Bỏ Postsremove_menu_page( 'edit.php' );// Bỏ Posts -> Categoriesremove_submenu_page( 'edit.php', 'edit-tags.php?taxonomy=category' );// Bỏ Posts -> Tagsremove_submenu_page( 'edit.php', 'edit-tags.php?taxonomy=post_tag' );// Bỏ Mediaremove_menu_page( 'upload.php' );// Bỏ Media -> Libraryremove_submenu_page( 'upload.php', 'upload.php' );// Bỏ Media -> Add new mediaremove_submenu_page( 'upload.php', 'media-new.php' );// Bỏ Pagesremove_menu_page( 'edit.php?post_type=page' );// Bỏ Pages -> All pagesremove_submenu_page( 'edit.php?post_type=page', 'edit.php?post_type=page' );// Bỏ Pages -> Add new pageremove_submenu_page( 'edit.php?post_type=page', 'post-new.php?post_type=page' );// Bỏ Commentsremove_menu_page( 'edit-comments.php' );// Bỏ Appearanceremove_menu_page( 'themes.php' );// Bỏ Appearance -> Themesremove_submenu_page( 'themes.php', 'themes.php' );// Bỏ Appearance -> Customizeremove_submenu_page( 'themes.php', 'customize.php?return=' . urlencode( $_SERVER['REQUEST_URI'] ) );// Bỏ Appearance -> Widgetsremove_submenu_page( 'themes.php', 'widgets.php' );// Bỏ Appearance -> Menusremove_submenu_page( 'themes.php', 'nav-menus.php.php' );// Bỏ Appearance -> Editorremove_submenu_page( 'themes.php', 'theme-editor.php' );// Bỏ Pluginsremove_menu_page( 'plugins.php' );// Bỏ Plugins -> Installed pluginsremove_submenu_page( 'plugins.php', 'plugins.php' );// Bỏ Plugins -> Add new pluginsremove_submenu_page( 'plugins.php', 'plugin-install.php' );// Bỏ Plugins -> Plugin editorremove_submenu_page( 'plugins.php', 'plugin-editor.php' );// Bỏ Usersremove_menu_page( 'users.php' );// Bỏ Users -> Usersremove_submenu_page( 'users.php', 'users.php' );// Bỏ Users -> New userremove_submenu_page( 'users.php', 'user-new.php' );// Bỏ Users -> Your profileremove_submenu_page( 'users.php', 'profile.php' );// Bỏ Toolsremove_menu_page( 'tools.php' );// Bỏ Tools -> Available Toolsremove_submenu_page( 'tools.php', 'tools.php' );// Bỏ Tools -> Importremove_submenu_page( 'tools.php', 'import.php' );// Bỏ Tools -> Exportremove_submenu_page( 'tools.php', 'export.php' );// Bỏ Settingsremove_menu_page( 'options-general.php' );// Bỏ Settings -> Writingremove_submenu_page( 'options-general.php', 'options-writing.php' );// Bỏ Settings -> Readingremove_submenu_page( 'options-general.php', 'options-reading.php' );// Bỏ Settings -> Discussionremove_submenu_page( 'options-general.php', 'options-discussion.php' );// Bỏ Settings -> Mediaremove_submenu_page( 'options-general.php', 'options-media.php' );// Bỏ Settings -> Permalinksremove_submenu_page( 'options-general.php', 'options-permalink.php' );}, 999);
add_action( 'admin_init', function () {remove_role( 'administrator' ); // nhóm adminremove_role( 'editor' ); // nhóm biên tập viênremove_role( 'author' ); // nhóm tác giảremove_role( 'contributor' ); // nhóm cộng tác viênremove_role( 'subscriber' ); // nhóm thành viên});
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;});
Nguồn: //caodem.com/tong-hop-code-function-hay-trong-wordpress/