Ẩn tab attribute cho product type Woocommerce
1 2 3 4 5 6 7 8 9 10 11 12 | function hide_attributess_for_mobile_booking_data_panel( $tabs ) { $tabs[ 'general' ][ 'class' ][] = 'hide_if_mobile-booking'; $tabs[ 'inventory' ][ 'class' ][] = 'hide_if_mobile-booking'; $tabs[ 'shipping' ][ 'class' ][] = 'hide_if_mobile-booking'; $tabs[ 'linked_product' ][ 'class' ][] = 'hide_if_mobile-booking'; $tabs[ 'variations' ][ 'class' ][] = 'hide_if_mobile-booking'; $tabs[ 'advanced' ][ 'class' ][] = 'hide_if_mobile-booking'; $tabs[ 'attribute' ][ 'class' ][] = 'hide_if_mobile-booking'; return $tabs; } add_filter( 'woocommerce_product_data_tabs', 'hide_attributess_for_mobile_booking_data_panel', 10, 1); |
Trong đó:
mobile-booking: là value Product type
hide: là ẩn
Muốn hiển thị có thể thay hide bằng show
Kết quả:
Nén code tăng tốc độ load trang
Cách 1: Viết hàm php
1 2 3 4 5 6 7 8 9 10 11 12 | 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"); |
Cách 2: Sử dụng htaccess
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/xml application/xhtml+xml application/rss+xml application/x-javascript application/javascript ExpiresActive On ExpiresByType text/css A31536000 ExpiresByType text/fonts A31536000 ExpiresByType text/x-component A31536000 ExpiresByType application/x-javascript A31536000 ExpiresByType application/javascript A31536000 ExpiresByType text/javascript A31536000 ExpiresByType text/x-js A31536000 ExpiresByType text/html A3600 ExpiresByType text/richtext A3600 ExpiresByType image/svg+xml A3600 ExpiresByType text/plain A3600 ExpiresByType text/xsd A3600 ExpiresByType text/xsl A3600 ExpiresByType text/xml A3600 ExpiresByType video/asf A31536000 ExpiresByType video/avi A31536000 ExpiresByType image/bmp A31536000 ExpiresByType application/java A31536000 ExpiresByType video/divx A31536000 ExpiresByType application/msword A31536000 ExpiresByType application/vnd.ms-fontobject A31536000 ExpiresByType application/x-msdownload A31536000 ExpiresByType image/gif A31536000 ExpiresByType application/x-gzip A31536000 ExpiresByType image/x-icon A31536000 ExpiresByType image/jpeg A31536000 ExpiresByType application/json A31536000 ExpiresByType application/vnd.ms-access A31536000 ExpiresByType audio/midi A31536000 ExpiresByType video/quicktime A31536000 ExpiresByType audio/mpeg A31536000 ExpiresByType video/mp4 A31536000 ExpiresByType video/mpeg A31536000 ExpiresByType application/vnd.ms-project A31536000 ExpiresByType application/x-font-otf A31536000 ExpiresByType application/vnd.ms-opentype A31536000 ExpiresByType application/vnd.oasis.opendocument.database A31536000 ExpiresByType application/vnd.oasis.opendocument.chart A31536000 ExpiresByType application/vnd.oasis.opendocument.formula A31536000 ExpiresByType application/vnd.oasis.opendocument.graphics A31536000 ExpiresByType application/vnd.oasis.opendocument.presentation A31536000 ExpiresByType application/vnd.oasis.opendocument.spreadsheet A31536000 ExpiresByType application/vnd.oasis.opendocument.text A31536000 ExpiresByType audio/ogg A31536000 ExpiresByType application/pdf A31536000 ExpiresByType image/png A31536000 ExpiresByType application/vnd.ms-powerpoint A31536000 ExpiresByType audio/x-realaudio A31536000 ExpiresByType image/svg+xml A31536000 ExpiresByType application/x-shockwave-flash A31536000 ExpiresByType application/x-tar A31536000 ExpiresByType image/tiff A31536000 ExpiresByType application/x-font-ttf A31536000 ExpiresByType application/x-font-woff A31536000 ExpiresByType application/vnd.ms-opentype A31536000 ExpiresByType audio/wav A31536000 ExpiresByType audio/wma A31536000 ExpiresByType application/vnd.ms-write A31536000 ExpiresByType application/font-woff A31536000 ExpiresByType application/vnd.ms-excel A31536000 ExpiresByType application/zip A31536000 |
Tạo icon cho post type admin trong wordpress
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 4 5 6 7 8 9 10 | function sangtaosacviet_icon_post_type(){ ?> <style> #adminmenu .menu-icon-tour div.wp-menu-image:before { content: "f322"; } </style> <?php } add_action( 'admin_head', 'sangtaosacviet_icon_post_type' ); |
Tạo chức năng upload logo cho website đơn giản
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 4 5 6 7 8 9 10 | function sangtaosacviet_theme_setup() { add_theme_support( 'custom-logo', array( 'width' => 250, 'height' => 50, 'flex-width' => true, ) ); } add_action( 'after_setup_theme', 'sangtaosacviet_theme_setup' ); |
Trong đó :
width: là chiều rộng của logo
Địa chỉ: Thanh Mai, Thanh Oai, Hà Nội
Điện thoại: 0983.078.804 (Mr.Hiếu)
Email: lehieuit91@gmail.com
Zalo : 0983.078.804
Website: www.sangtaosacviet.com
Kho theme: Kho themes nhiều mẫu mã đẹp
height: là chiều cao của logo
flex-width: cho phép tùy chỉnh chiều rộng khi là true. Trong trường hợp là false chiều cao bị sẽ co theo tỷ lệ với chiều rộng.
Gọi ra logo
1 2 3 | <div class="logo"> <?php the_custom_logo(); ?> </div> |
Chức năng lọc taxonomy trong wordpress
Một số loại website mà ta từng sử dụng chức năng lọc multi taxonomy như bất động sản, du lịch, shop.
Lọc theo taxonomy mình thấy query lọc trong tax_query tương đối phổ biến. Tuy nhiên hôm nay có 1 phương pháp lọc nữa đó là viết thẳng vào mảng array query. Relation là kiểu AND
1 2 3 4 5 6 | $arg_fillter = array( 'post_type' => 'tour', 'posts_per_page' => -1, 'diadiem_Cat' => 'ha-noi', 'loai_hinh_du_lich' => 'team-buildding', ); |
Trong đó:
diadiem_Cat : tên taxonomy A
loai_hinh_du_lich : tên taxonomy B
ha-noi: slug term trong taxonomy A
team_build_ding: slug term trong taxonomy B
Và ta truy vấn bình thường để có dữ liệu trong vòng while loop post
Đếm số bài viết theo thành viên WordPress
1 2 3 4 5 6 | $id_stsvuser = "ID_Cua_Thanh_Vien"; // Đếm bài viết với Post type là Post count_user_posts($id_stsvuser,'post'); // Đếm bài viết với Post_type là Custom_post_type count_user_posts($id_stsvuser,'post_type_cua_ban'); |
Thêm nhóm thành viên cho WordPress
1 2 3 4 5 6 7 8 9 10 11 | // Thêm nhóm thành viên Vip, đặt level nhóm là 3. $sangtaosacviet_new_user = add_role('vipuser', __( 'Vip User' ), array( 'upload_files' => true, 'edit_others_pages' => true, 'edit_posts' => true, 'publish_posts' => true, 'read' => true, 'level_3' => true, ) ); |
Đếm số bình luận của bài viết theo các trạng thái
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | // Lấy thông tin comment theo bài $comments_count = wp_count_comments(get_the_ID()); // hiển thị tổng bình luận echo $comments_count->total_comments; // bình luận đã được duyệt echo $comments_count->approved; // Bình luận được kiểm duyệt echo $comments_count->moderated; // Bình luận spam echo $comments_count->spam; // Bình luận đã xóa echo $comments_count->trash; |
Đếm số thành viên trên website WordPress
1 2 3 4 5 6 7 | // Tổng số thành viên $sangtaosacviet_user = count_users(); echo $sangtaosacviet_user['total_users']; // Số thành viên trong nhóm user $sangtaosacviet_cgroup = count( get_users( array('fields' => array('ID'), 'role' => 'author'))); // Thay author bằng các nhóm: administrator, editor, contributor, subscriber. |
Thêm chức năng tính lượt xem cho WordPress
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | function getPostViews($postID){ // hàm này dùng để lấy số người đã xem qua bài viết $count_key = 'post_views_count'; $count = get_post_meta($postID, $count_key, true); if($count==''){ // Nếu như lượt xem không có delete_post_meta($postID, $count_key); add_post_meta($postID, $count_key, '0'); return "0"; // giá trị trả về bằng 0 } return $count; // Trả về giá trị lượt xem } function setPostViews($postID) {// hàm này dùng để set và update số lượt người xem bài viết. $count_key = 'post_views_count'; $count = get_post_meta($postID, $count_key, true); if($count==''){ $count = 0; delete_post_meta($postID, $count_key); add_post_meta($postID, $count_key, '0'); }else{ $count++; // cộng đồn view update_post_meta($postID, $count_key, $count); // update count } } |
Sau đó ở file single.php các bạn thêm đoạn này để tính view và hiển thị view:
1 2 3 4 5 | // cộng lượt xem setPostViews(get_the_ID()); // Hiển thị số lượt xem getPostViews(get_the_ID()); |
Chức năng giới hạn tiêu đề bài viết WordPress
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | function the_title_limit($max_char, $more_link_text = '') { $title = get_the_title(); $title = strip_tags($title); if (strlen($_GET['p']) > 0) { echo $title; echo " …"; } else if ((strlen($title)>$max_char) && ($espacio = strpos($title, " ", $max_char ))) { $title = substr($title, 0, $espacio); $title = $title; echo $title; echo " …"; echo "$more_link_text"; } else { echo $title; } } |
Sau đó bạn có thể gọi tiêu đề giới hạn số từ bằng cách dùng:
1 | the_title_limit(20) |
Đếm số từ trong nội dung bài viết WordPress
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 4 5 6 7 8 9 10 | 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); } } |
Bạn vào file single.php trong theme và hiển thị số từ của bài viết bằng đoạn code này:
1 | echo post_word_count(); |
Tắt chức năng chia sẻ Rest API trong WordPress
Thêm đoạn code bên dưới vào file Functions.php:
1 2 | add_filter('json_enabled', '__return_false'); add_filter('json_jsonp_enabled', '__return_false'); |
Thêm class vào thẻ P đầu tiên trong bài viết WordPress
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 4 | function sangtaosacviet_p_first( $content ) { return preg_replace( '/<p([^>]+)?>/', '<p$1 class="sangtaosacviet_class">', $content, 1 ); } add_filter( 'the_content', 'sangtaosacviet_p_first' ); |
Tạo Custom post type nhanh cho WordPress
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | function create_custom_post() { register_post_type( 'custom-post', array( 'labels' => array( 'name' => __( 'Custom Post' ), ), 'public' => true, 'hierarchical' => true, 'has_archive' => true, 'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail' ), 'can_export' => true, 'taxonomies' => array( 'post_tag', 'category' ) )); } add_action('init', 'create_custom_post'); |
Hiển thị page trong kết quả tìm kiếm
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 4 5 | function sangtaosacviet_them_pages_from_search() { global $wp_post_types; $wp_post_types['page']->exclude_from_search = true; } add_action( 'init', 'sangtaosacviet_them_pages_from_search' ); |
Thêm danh mục cho File đính kèm WordPress
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 4 5 6 7 8 9 10 11 | // Thêm Category cho Attachment function add_categories_for_attachments() { register_taxonomy_for_object_type( 'category', 'attachment' ); } add_action( 'init' , 'add_categories_for_attachments' ); // Thêm Tag cho Attachment function add_tags_for_attachments() { register_taxonomy_for_object_type( 'post_tag', 'attachment' ); } add_action( 'init' , 'add_tags_for_attachments' ); |
Tạo kích thước Upload Media mới cho WordPress
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 4 5 | // Tạo kích thước upload mới add_image_size( 'sangtaosacviet_custom-thumbnail', 320, 240, true ); // Hiển thị hình ảnh với kích thước mới vừa thêm the_post_thumbnail_url('sangtaosacviet_custom-thumbnail'); |
Thay đổi độ dài của mô tả bài viết trong WordPress
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 4 | function custom_excerpt_length( $length ) { return 30; } add_filter( 'excerpt_length', 'custom_excerpt_length', 999 ); |
Lưu ý: trong đó 30 là độ dài nội dung mô tả cần hiển thị.
Sử dụng Google font cho theme WordPress
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 4 5 | 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' ); |
Lưu ý: bạn có thể thay link font google bên trên theo ý của mình.
Thay logo mặc định trong trang Login wordpress
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 4 | function custom_login_logo() { echo '<style>.login h1 a { background-image: url(link_image.jpg) !important; background-size: 234px 67px; width:234px; height:67px; display:block; }</style>'; } add_action( 'login_head', 'custom_login_logo' ); |
Tắt các Widget không dùng trong WordPress
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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); |
Bỏ chức năng không cần thiết trong Theme WordPress
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 4 5 6 7 8 9 10 | add_action( 'after_setup_theme', function () { remove_theme_support( 'post-formats' ); remove_theme_support( 'post-thumbnails' ); remove_theme_support( 'custom-background' ); remove_theme_support( 'custom-header' ); remove_theme_support( 'automatic-feed-links' ); remove_theme_support( 'html5' ); remove_theme_support( 'title-tag' ); remove_theme_support( 'menus' ); }, 11 ); |
Xóa nhóm thành viên mặc định trong WordPress
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 4 5 6 7 | add_action( 'admin_init', function () { remove_role( 'administrator' ); remove_role( 'editor' ); remove_role( 'author' ); remove_role( 'contributor' ); remove_role( 'subscriber' ); }); |
Chỉ cho Admin vào trang quản trị wp-admin
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 4 5 6 7 8 | function lovend_redirect(){ $lehieuitcheck = 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'); |
Tự động thêm thuộc tính Nofollow cho link ở nội dung bài viết WordPress
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 | function nofollow_sangtaosacviet($text) { $return = str_replace('<a', ' |
Thêm trường tùy biến cho hồ sơ thành viên wordpress
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 4 5 6 7 | function sangtaosacviet_add_fields_user($profile_fields){ $profile_fields['googleplus'] = 'Google+'; $profile_fields['twitter'] = 'Twitter username'; $profile_fields['facebook'] = 'Facebook profile URL'; return $profile_fields; } add_filter('user_contactmethods', 'sangtaosacviet_add_fields_user'); |
Bỏ các cột hiển thị trong Post và Page WordPress
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | add_action( 'admin_init' , function () { // Bỏ cột trong Post add_filter( 'manage_posts_columns', function ( $columns ) { unset( $columns['cb'], $columns['title'], $columns['author'], $columns['categories'], $columns['tags'], $columns['comments'], $columns['date'] ); return $columns; }); // Bỏ cột trong Page add_filter( 'manage_pages_columns', function ( $columns ) { unset( $columns['cb'], $columns['title'], $columns['author'], $columns['categories'], $columns['tags'], $columns['comments'], $columns['date'] ); return $columns; }); }); |
Tùy biến bỏ các thông báo update của WordPress
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | // Tắt thông báo update wordpress phiên bản mới add_action( 'after_setup_theme', function () { add_action( 'init', create_function( '$a', "remove_action( 'init', 'wp_version_check' );" ), 2 ); add_filter( 'pre_option_update_core', '__return_null' ); add_filter( 'pre_site_transient_update_core', '__return_null' ); }, 1 ); // Tắt thông báo update Plugin remove_action( 'load-update-core.php', 'wp_update_plugins' ); add_filter( 'pre_site_transient_update_plugins', '__return_null' ); // Tắt thông báo update các plugin đang bật add_filter( 'transient_update_plugins', function ( $value = '' ) { if ( ( isset( $value->response ) ) && ( count( $value->response ) ) ) { $active_plugins = get_option( 'active_plugins' ); if ( $active_plugins ) { foreach( $value->response as $plugin_idx => $plugin_item ) { if ( ! in_array( $plugin_idx, $active_plugins ) ) { unset( $value->response[$plugin_idx] ); } } } else { foreach( $value->response as $plugin_idx => $plugin_item ) { unset( $value->response ); } } } return $value; }); |
Bỏ nút mặc định trong Media Upload
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 4 5 6 7 | 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; }); |
Bỏ Javascript mặc định wordpress tạo ra trên theme
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 4 | add_action( 'wp_enqueue_scripts', function () { wp_deregister_script( 'jquery' ); wp_deregister_script( 'wp-embed' ); }); |
Bỏ kích thước ảnh mặc định trong WordPress
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 4 5 6 7 | function remove_df_media_sangtaosacviet( $sizes) { unset( $sizes['thumbnail']); unset( $sizes['medium']); unset( $sizes['large']); return $sizes; } add_filter('intermediate_image_sizes_advanced', 'remove_df_media_sangtaosacviet'); |
Bỏ các thẻ trên Header website được WordPress tự động sinh ra
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | add_action( 'init', function () { // Bỏ RSD link (Really Simple Discovery) remove_action( 'wp_head', 'rsd_link' ); // Bỏ Wlwmanifest link (Windows Live Writer manifest) remove_action( 'wp_head', 'wlwmanifest_link' ); // Bỏ feeds remove_action( 'wp_head', 'feed_links', 2 ); // Bỏ extra feeds, such as category feeds remove_action( 'wp_head','feed_links_extra', 3 ); // Bỏ displayed XHTML generator remove_action( 'wp_head', 'wp_generator' ); // Bỏ REST API link tag remove_action( 'wp_head', 'rest_output_link_wp_head', 10 ); // Bỏ oEmbed discovery links. remove_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 ); // Bỏ rel next/prev links remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0); }); |
Xóa Tab widgets trong trang Quản trị WordPress
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | add_action( 'admin_init', function () { // Bỏ Welcome panel remove_action('welcome_panel', 'wp_welcome_panel'); // Bỏ incoming links metabox remove_meta_box( 'dashboard_incoming_links', 'dashboard', 'normal' ); // Bỏ plugins metabox remove_meta_box( 'dashboard_plugins', 'dashboard', 'normal' ); // Bỏ WordPress News metabox remove_meta_box( 'dashboard_primary', 'dashboard', 'normal' ); // Bỏ secondary metabox remove_meta_box( 'dashboard_secondary', 'dashboard', 'normal' ); // Bỏ Quick Draft metabox remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' ); // Bỏ Recent Drafts metabox remove_meta_box( 'dashboard_recent_drafts', 'dashboard', 'side' ); // Bỏ Activity metabox remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' ); // Bỏ At a Glance metabox remove_meta_box( 'dashboard_right_now', 'dashboard', 'normal' ); // Bỏ Activity metabox remove_meta_box( 'dashboard_activity', 'dashboard', 'normal' ); }); |
Xóa các Tab hỗ trợ trong Dashboard WordPress
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | add_filter( 'contextual_help', function ( $old_help, $screen_id, $screen ) { // Bỏ tất cả Tab trợ giúp $screen->remove_help_tabs(); // Tùy chọn bỏ Tab trợ giúp $screen->remove_help_tab( 'overview' ); $screen->remove_help_tab( 'help-navigation' ); $screen->remove_help_tab( 'help-layout' ); $screen->remove_help_tab( 'help-content' ); $screen->remove_help_tab( 'attachment-details' ); $screen->remove_help_tab( 'managing-pages' ); $screen->remove_help_tab( 'managing-pages' ); $screen->remove_help_tab( 'moderating-comments' ); $screen->remove_help_tab( 'adding-themes' ); $screen->remove_help_tab( 'customize-preview-themes' ); $screen->remove_help_tab( 'compatibility-problems' ); $screen->remove_help_tab( 'adding-plugins' ); $screen->remove_help_tab( 'screen-display' ); $screen->remove_help_tab( 'actions' ); $screen->remove_help_tab( 'user-roles' ); $screen->remove_help_tab( 'press-this' ); $screen->remove_help_tab( 'converter' ); $screen->remove_help_tab( 'options-postemail' ); $screen->remove_help_tab( 'options-services' ); $screen->remove_help_tab( 'site-visibility' ); $screen->remove_help_tab( 'permalink-settings' ); $screen->remove_help_tab( 'custom-structures' ); }, 999, 3); |
Xóa nội dung chân trang trang quản trị WordPress
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 4 5 6 7 8 9 | // Bỏ text Trái add_filter( 'admin_footer_text', function () { return ''; }, 999); // Bỏ text Phải add_filter( 'update_footer', function () { return ''; }, 999); |
Bỏ menu không cần thiết trong trang quản trị WordPress
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | 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); |
Bỏ chức năng Emojis (biểu tượng cảm xúc) trên wordpress
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | add_action( 'init', function () { remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); remove_action( 'wp_print_styles', 'print_emoji_styles' ); remove_action( 'admin_print_scripts', 'print_emoji_detection_script' ); remove_action( 'admin_print_styles', 'print_emoji_styles' ); remove_filter( 'the_content_feed', 'wp_staticize_emoji' ); remove_filter( 'comment_text_rss', 'wp_staticize_emoji' ); remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' ); add_filter( 'emoji_svg_url', '__return_false' ); // Tìm gỡ TinyMCE emojis add_filter( 'tiny_mce_plugins', function ( $plugins ) { if ( is_array( $plugins ) ) { return array_diff( $plugins, array( 'wpemoji' ) ); } return array(); }); }); |
Xóa trường mặc định trong khung bình luận wordpress
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 4 5 6 7 | function sangtaosacviet_disable_comment_fields( $fields ) { unset( $fields['email'] ); // xóa trường Email unset( $fields['author'] ); // xóa trường Tên unset( $fields['url'] ); // xóa trường Website return $fields; } add_filter( 'comment_form_default_fields', 'sangtaosacviet_disable_comment_fields' ); |
Ẩn các link trên thanh Admin Bar WordPress
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 | add_action( 'admin_bar_menu', function ( $admin_bar ) { $admin_bar->remove_menu( 'wp-logo' ); // Ẩn WordPress logo $admin_bar->remove_menu( 'about' ); // Ẩn about WordPress link $admin_bar->remove_menu( 'documentation' ); // Ẩn WordPress documentation link $admin_bar->remove_menu( 'support-forums' ); // Ẩn support forums link $admin_bar->remove_menu( 'feedback' ); // Ẩn feedback link $admin_bar->remove_menu( 'site-name' ); // Ẩn site name menu $admin_bar->remove_menu( 'view-site' ); // Ẩn view site link $admin_bar->remove_menu( 'updates' ); // Ẩn updates link $admin_bar->remove_menu( 'comments' ); // Ẩn comments link $admin_bar->remove_menu( 'new-content' ); // Ẩn content link $admin_bar->remove_menu( 'my-account' ); // Ẩn user details tab }, 999); |
Xóa Admin Bar wordpress
Thêm đoạn code bên dưới vào file Functions.php:
1 | add_filter( 'show_admin_bar', '__return_false' ); |
Chạy Shortcode trong widget
Mặc định WordPress không chạy shortcode trong widget text.
Để chạy được ta cần thêm đoạn code sau vào file functions.php của theme
1 2 | //Run Shortcode in widget text add_filter('widget_text', 'do_shortcode'); |
Chuyển hướng tới trang khác khi post có shortcode nào đó
Code này giúp bạn chuyển hướng trang web khi ở theme, post, page nào đó. Hoặc post nào có shortcode nào đó
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 4 5 6 7 8 | function my_page_template_redirect(){ global $post; if( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'your_short_code') && ! is_user_logged_in() ) { wp_redirect(URL_IN_HERE); exit(); } } add_action( 'template_redirect', 'my_page_template_redirect' ); |
Xóa toàn bộ tiền tố trong tiêu đề như Category: ABCXYZ, Taxonomy: ABC XYZ …
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 4 | add_filter('get_the_archive_title', 'sangtaosacviet_get_the_archive_title', 99); function sangtaosacviet_get_the_archive_title($title) { return preg_replace('/.+: /', '', $title); } |
Không cho phép đổi Password ngoại trừ Admin
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 4 5 6 7 | if ( is_admin() ) add_action( ‘init’, ‘disable_password_fields’, 10 ); function disable_password_fields() { if ( ! current_user_can( ‘administrator’ ) ) $show_password_fields = add_filter( ‘show_password_fields’, ‘__return_false’ ); } |
Không cho phép đối email. (Vẫn hiển thị trường email, nhưng không thay đổi được ).
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 4 5 6 | add_action( ‘user_profile_update_errors’, ‘prevent_email_change’, 10, 3 ); function prevent_email_change( $errors, $update, $user ) { $old = get_user_by(‘id’, $user->ID); if( $user->user_email != $old->user_email ) $user->user_email = $old->user_email; } |
Code đang bảo trì website
Thêm đoạn code bên dưới vào file Functions.php:
1 2 3 | function wp_maintenance_mode(){if(!current_user_can('edit_themes') || !is_user_logged_in()){wp_die('<h1 style="color:red">Bảo trì hệ thống</h1><br />Hiện tại chúng tôi đang bảo trì để nâng cấp hệ thống. Cảm ơn bạn đã quan tâm đến dịch vụ của chúng tôi!');} } add_action('get_header', 'wp_maintenance_mode'); |
Trả lời