Dưới đây là một số đoạn mã thường được sử dụng trong việc thiết kế website WordPress mà Tùng Bùi SEO hay sử dụng. Đây cũng là thư viện code Tùng đã chọn lọc để đưa vào. Hy vọng giúp nhiều cho Anh/Chị em nhé. Xem và chỉ cần copy bỏ vào file Functions.php là được nhé
2.Code WordPress Hay Dùng
![100+ Code Wordpress Và Fix Teachnical Seo Update [current_date] 1 Code WordPress Hay Dùng](https://tungphat.com/wp-content/uploads/2024/07/code-wordpress-hay-dung.png)
Code hàm replace_text
Công dụng:
- ( tìm và thay thế text) trong thẻ <body> …</body>
- Thay thế địa chỉ trong nhiều bài viết
function replace_text($text) { $text = str_replace('look-for-this-string', 'replace-with-this-string', $text); $text = str_replace('look-for-that-string', 'replace-with-that-string', $text); return $text; } add_filter('the_content', 'replace_text');
Code chặn Index phân trang
Công dụng:
- Chặn index phân trang bài viết hay phân trang sản phẩm
- Hạn chế trùng lặp dữ liệu
function gk_subpage_fix() { if(is_paged()) echo '<meta name="robots" content="noindex,follow"/>';} add_action('wp_head', 'gk_subpage_fix');
2.Cách Sửa lỗi “XML declaration allowed only at the start of the document” trên Sitemap WordPress
![100+ Code Wordpress Và Fix Teachnical Seo Update [current_date] 2 Sửa lỗi “XML declaration allowed only at the start of the document”](https://tungphat.com/wp-content/uploads/2024/07/loi-sitemap.xml_.png)
Bước 1: Tạo file whitespacefix.php trong thư mục public_html
Và thêm đoạn code bên dưới vào.
<?php function ___wejns_wp_whitespace_fix($input) { $allowed = false; $found = false; foreach (headers_list() as $header) { if (preg_match("/^content-type:\\s+(text\\/|application\\/((xhtml|atom|rss)\\+xml|xml))/i", $header)) { $allowed = true; } if (preg_match("/^content-type:\\s+/i", $header)) { $found = true; } } if ($allowed || !$found) { return preg_replace("/\\A\\s*/m", "", $input); } else { return $input; } } ob_start("___wejns_wp_whitespace_fix"); ?>
Thêm code xong bạn hãy Save lại.
Bước 2: Include file whitespacefix.php vào index.php
include('whitespacefix.php');
Sau khi làm xong, hãy truy cập Sitemap website của bạn theo dường dẫn: https://domain.com/sitemap_index.xml
để kiểm tra kết quả. Chúc bạn thành công
3.Đổi tên Related product thành sản phẩm tương tự trong Woocommerce WordPress
Tùng Bùi SEO chỉ cho bạn:
- Thêm đoạn code chèn vào file function.php
- Hoặc bạn thay đổi Chữ “Sản phẩm tương tự” bằng chữ khác mà bạn mong muốn
add_filter("ngettext", "rename_relatedproduct_text", 10, 3); function rename_relatedproduct_text($translated, $text, $domain) { if ($text === "Related products" && $domain === "woocommerce") { $translated = esc_html__("Sản phẩm tương tự", $domain); } return $translated; }
4. Khai báo robots.txt chuẩn nhất
User-agent: * Disallow: /wp-admin/ Allow: /wp-admin/admin-ajax.php Disallow: /component/* Disallow: /wp-login.php* Disallow: /cdn-cgi/ Disallow: /?author= Disallow: /author/ Disallow: /feed/$ Disallow: /tag/ Disallow: /search/ Disallow: /?s= Disallow: /?__hstc= Disallow: /p=* Disallow: /comment-page Disallow: /*comment-page* Sitemap: https://tungphat.com/sitemap_index.xml
5./Cách sửa lỗi briefly unavailable for scheduled maintenance trong WordPress
Để đưa trang web của bạn ra khỏi chế độ bảo trì, bạn chỉ cần xóa tệp .maintenance khỏi thư mục gốc của trang web bằng cách sử dụng FTP. Kết nối với các tệp của trang web, tìm tệp .maintenance, nhấp chuột phải và chọn Xóa.
![100+ Code Wordpress Và Fix Teachnical Seo Update [current_date] 3 Tìm file .maintenance và chọn Delete](https://tungphat.com/wp-content/uploads/2024/07/tim-file-maintenace.png)
6./Sửa lỗi Videos Missing Field Data “name” & “uploadDate” In Google Search Console
![100+ Code Wordpress Và Fix Teachnical Seo Update [current_date] 4 Sữa lỗi schema video](https://tungphat.com/wp-content/uploads/2024/07/sua-loi-schema-video.png)
Dùng Rank Math SEO: Tiến hành cài đặt Schema Video
![100+ Code Wordpress Và Fix Teachnical Seo Update [current_date] 5 schema-templates](https://tungphat.com/wp-content/uploads/2024/07/schema-templates.png)
![100+ Code Wordpress Và Fix Teachnical Seo Update [current_date] 6 tạo schema video Rank Math](https://tungphat.com/wp-content/uploads/2024/07/tao-schema-video-rank-math.png)
![100+ Code Wordpress Và Fix Teachnical Seo Update [current_date] 7 schema-video](https://tungphat.com/wp-content/uploads/2024/07/schema-video.png)
Và đây là kết quả
![100+ Code Wordpress Và Fix Teachnical Seo Update [current_date] 8 kết quả fix lỗi schema video Google Seach Consle](https://tungphat.com/wp-content/uploads/2024/07/ket-qua-fix-loi-schema-video-google-seach-console.png)
7. Code tắt thông báo lỗi Flatsome – chứa các bản sao lỗi thời của một số tệp mẫu Flatsome
add_action('admin_enqueue_scripts', 'ds_admin_theme_style1'); function ds_admin_theme_style1() { echo '<style>#flatsome-notice,.notice.notice-info{ display: none; }</style>'; if(current_user_can('editor')) { echo '<style>#menu-posts-guarantee_period,#menu-posts-blocks,#toplevel_page_wpcf7{ display: none; }</style>'; } }
8.Hướng dẫn xóa product, product-category trong đường dẫn của woocommerce
/* * Code Bỏ /product/ hoặc /cua-hang/ hoặc /shop/ ... có hỗ trợ dạng %product_cat% * Thay /cua-hang/ bằng slug hiện tại của bạn */ function devvn_remove_slug( $post_link, $post ) { if ( !in_array( get_post_type($post), array( 'product' ) ) || 'publish' != $post->post_status ) { return $post_link; } if('product' == $post->post_type){ $post_link = str_replace( '/cua-hang/', '/', $post_link ); //Thay cua-hang bằng slug hiện tại của bạn }else{ $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link ); } return $post_link; } add_filter( 'post_type_link', 'devvn_remove_slug', 10, 2 ); /*Sửa lỗi 404 sau khi đã remove slug product hoặc cua-hang*/ function devvn_woo_product_rewrite_rules($flash = false) { global $wp_post_types, $wpdb; $siteLink = esc_url(home_url('/')); foreach ($wp_post_types as $type=>$custom_post) { if($type == 'product'){ if ($custom_post->_builtin == false) { $querystr = "SELECT {$wpdb->posts}.post_name, {$wpdb->posts}.ID FROM {$wpdb->posts} WHERE {$wpdb->posts}.post_status = 'publish' AND {$wpdb->posts}.post_type = '{$type}'"; $posts = $wpdb->get_results($querystr, OBJECT); foreach ($posts as $post) { $current_slug = get_permalink($post->ID); $base_product = str_replace($siteLink,'',$current_slug); add_rewrite_rule($base_product.'?$', "index.php?{$custom_post->query_var}={$post->post_name}", 'top'); add_rewrite_rule($base_product.'comment-page-([0-9]{1,})/?$', 'index.php?'.$custom_post->query_var.'='.$post->post_name.'&cpage=$matches[1]', 'top'); add_rewrite_rule($base_product.'(?:feed/)?(feed|rdf|rss|rss2|atom)/?$', 'index.php?'.$custom_post->query_var.'='.$post->post_name.'&feed=$matches[1]','top'); } } } } if ($flash == true) flush_rewrite_rules(false); } add_action('init', 'devvn_woo_product_rewrite_rules'); /*Fix lỗi khi tạo sản phẩm mới bị 404*/ function devvn_woo_new_product_post_save($post_id){ global $wp_post_types; $post_type = get_post_type($post_id); foreach ($wp_post_types as $type=>$custom_post) { if ($custom_post->_builtin == false && $type == $post_type) { devvn_woo_product_rewrite_rules(true); } } } add_action('wp_insert_post', 'devvn_woo_new_product_post_save'); /* * Remove product-category in URL * Thay product-category bằng slug hiện tại của bạn. Mặc định là product-category */ add_filter( 'term_link', 'devvn_product_cat_permalink', 10, 3 ); function devvn_product_cat_permalink( $url, $term, $taxonomy ){ switch ($taxonomy): case 'product_cat': $taxonomy_slug = 'cua-hang'; //Thay bằng slug hiện tại của bạn. Mặc định là product-category if(strpos($url, $taxonomy_slug) === FALSE) break; $url = str_replace('/' . $taxonomy_slug, '', $url); break; endswitch; return $url; } // Add our custom product cat rewrite rules function devvn_product_category_rewrite_rules($flash = false) { $terms = get_terms( array( 'taxonomy' => 'product_cat', 'post_type' => 'product', 'hide_empty' => false, )); if($terms && !is_wp_error($terms)){ $siteurl = esc_url(home_url('/')); foreach ($terms as $term){ $term_slug = $term->slug; $baseterm = str_replace($siteurl,'',get_term_link($term->term_id,'product_cat')); add_rewrite_rule($baseterm.'?$','index.php?product_cat='.$term_slug,'top'); add_rewrite_rule($baseterm.'page/([0-9]{1,})/?$', 'index.php?product_cat='.$term_slug.'&paged=$matches[1]','top'); add_rewrite_rule($baseterm.'(?:feed/)?(feed|rdf|rss|rss2|atom)/?$', 'index.php?product_cat='.$term_slug.'&feed=$matches[1]','top'); } } if ($flash == true) flush_rewrite_rules(false); } add_action('init', 'devvn_product_category_rewrite_rules'); /*Sửa lỗi khi tạo mới taxomony bị 404*/ add_action( 'create_term', 'devvn_new_product_cat_edit_success', 10, 2 ); function devvn_new_product_cat_edit_success( $term_id, $taxonomy ) { devvn_product_category_rewrite_rules(true); }
8.Đoạn code thay chữ quick view trong Flatsome
Đây là đoạn code để thay đổi ‘Quick View’ => ‘Xem nhanh’:
function gsviec_translations($strings) { $text = array( 'Quick View' => 'Xem nhanh' ); $strings = str_ireplace(array_keys($text), $text, $strings); return $strings; } add_filter( 'gettext', 'gsviec_translations', 20 );
Các bạn có thể thay ‘Quick View’ => ‘Xem nhanh’ tùy tích.
9. Thêm code Breadcrumb Rank Math vào File Single của Child theme Flatsome
Sử dụng đoạn mã sau trong các tệp mẫu giao diện của bạn để hiển thị breadcrumbs.
<?php if (function_exists('rank_math_the_breadcrumbs')) rank_math_the_breadcrumbs(); ?> OR
10. Code redirect 301 xử lý xóa link srsltid trên Google Search Tìm Kiếm
function fix_srsltid() { if (isset($_GET['srsltid'])) { $url = strtok($_SERVER["REQUEST_URI"], '?'); wp_redirect(esc_url(home_url($url)), 301); exit; } } add_action('template_redirect', 'fix_srsltid');
> Nếu website bạn chưa chuẩn seo, có những lỗi không sửa được. Thì Alo/Chat Zalo cho Tùng nhé 0902.313.677. Tùng sẽ tư vấn và hỗ trợ bạn gói dịch vụ thiết kế website trọn gói bao gồm( 1 tên miền .com/ .vn + 1 Hosting Pro + Code giao diện và chức năng hoàn thiện). Giúp website bạn nhanh chống có thứ hạng cao trên công cụ tìm kiếm google.
11. Cập nhật tự động ngày tháng năm trong tiêu đề, nội dung bài viết
add_filter( 'the_title', 'do_shortcode' ); // * Shortcode hiển thị thời gian hiện tại. // * shortcode: 02/2025 add_shortcode ('thoigian', 'get_thoigian'); function get_thoigian () { $thoigian= date ("m/Y"); // * Thay đổi định dạng tại đây return "$thoigian"; } // thay đổi và lấy tháng năm hiện tại title SEO add_action( 'rank_math/vars/register_extra_replacements', function(){ rank_math_register_var_replacement( 'namthang', [ 'name' => esc_html__( 'Current month', 'rank-math' ), 'description' => esc_html__( 'Get and return the current month', 'rank-math' ), 'variable' => 'namthang', 'example' => my_Current_Month_Custom_callback(), ], 'my_Current_Month_Custom_callback' ); }); function my_Current_Month_Custom_callback(){ //Có thể tuỳ biến dòng dưới để đúng định dạng mong muốn return date('m').'/'.date('Y'); } // Code chèn ngày tháng tự động function current_date_shortcode() { return date('d/m/Y'); } add_shortcode('current_date', 'current_date_shortcode'); function replace_current_date_in_title($title, $id = null) { if ((is_single() || is_home() || is_category() || is_front_page()) && !is_admin()) { $title = str_replace('07/02/2025', do_shortcode('07/02/2025'), $title); } return $title; } add_filter('the_title', 'replace_current_date_in_title', 10, 2); function replace_current_date_in_content($content) { if ((is_single() || is_home() || is_category() || is_front_page()) && !is_admin()) { $content = str_replace('07/02/2025', do_shortcode('07/02/2025'), $content); } return $content; } add_filter('the_content', 'replace_current_date_in_content');
Cách dùng thêm các biến vào title và description
- [‘current_date’]
- [‘thoigian’]
- %currentdate%
>>> Lưu ý bạn copy nhớ bỏ dấu ” nhé. Vì tôi đăng bài nó hiển thị ngày, nên bạn thật sự lưu ý ở đây nhé.
>> Xem thêm các bài viết hỗ trợ có thể bạn cần
- Cách tích hợp live chat Facebook vào website
- Domain là gì? Tên miền là gì? Khái niệm, công dụng và cách đăng ký
- Cách vào web bị sập: Hướng dẫn chi tiết & Khắc phục lỗi
- Sửa lỗi Ảnh Thumbnail Khi Chia Sẻ Zalo Link Website: Nguyên Nhân Và Cách Khắc Phục
![100+ Code Wordpress Và Fix Teachnical Seo Update [current_date] 14 Tùng Bùi - CEO Tùng Phát](https://tungphat.com/wp-content/uploads/2023/02/ceo-tungbui.png)
Tùng Bùi SEO tôi là CEO & Founder Công ty SEO Tùng Phát. Với hơn + 10 năm kinh nghiệm làm SEO thực chiến ( Seo từ khóa website, Thiết Kế Website Chuẩn Seo, Triển khai hệ thống phần mềm trên nền tảng Odoo).
Tôi sẽ mang đến cho Anh/Chị – chiến lược thay đổi số giúp doanh nghiệp tối ưu hóa hệ thống trên phần mềm Odoo và nhiều giải pháp Marketing giúp doanh nghiệp tiếp cận khách hàng hiệu quả cao thông qua các chiến lược tối ưu Seo tổng thể, seo từ khóa website lên top Google.
Nến bạn cần tư vấn chiến lược seo hãy ib qua zalo Mr. Tùng: 0902.313.677.
Xem thêm về Chuyên gia SEO: Tùng Bùi tại đây:https://tungphat.com/tung-bui