Khi xây dựng một website, việc chọn cấu hình hosting phù hợp là một trong những quyết định quan trọng để đảm bảo trang web hoạt động mượt mà, tải nhanh và ổn định. Bài viết này sẽ giúp bạn hiểu các yếu tố cần thiết và đề xuất cấu hình hosting tối ưu cho…
Lưu trữ danh mục: Thủ thuật
Mở file functions.php trong child theme của bạn (nếu chưa có child theme, bạn nên tạo child theme để tránh mất thay đổi khi update theme). Thêm đoạn code sau:
1 2 3 4 |
function add_fontawesome_icons() { wp_enqueue_style( 'font-awesome', 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css' ); } add_action( 'wp_enqueue_scripts', 'add_fontawesome_icons' ); |
Mở file functions.php trong child theme của bạn (nếu chưa có child theme, bạn nên tạo child theme để tránh mất thay đổi khi update theme). Thêm đoạn code sau:
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 |
add_filter( 'woocommerce_get_availability', 'webdy_custom_out_of_stock', 1, 2 ); function webdy_custom_out_of_stock( $availability, $_product ) { if ( ! $_product->is_in_stock() ) { // Hotline button HTML $hotline = '<a class="webdy-hotline-out-of-stock" href="tel:0902299161" target="_blank">'; $hotline .= '<i class="fa fa-phone" aria-hidden="true"></i> Gọi ngay: 090.22.99.161</a>'; // Thêm CSS nội tuyến 1 lần duy nhất (nếu chưa chèn) if ( ! wp_script_is( 'webdy-hotline-style', 'done' ) ) { add_action( 'wp_footer', function() { echo '<style id="webdy-hotline-style"> .webdy-hotline-out-of-stock { padding: 10px 25px; font-size: 1rem; font-weight: bold; text-decoration: none; display: inline-flex; align-items: center; gap: 10px; border-radius: 10px; background-color: #fff; color: #000; position: relative; z-index: 1; overflow: hidden; } .webdy-hotline-out-of-stock::before { content: ""; position: absolute; top: -2px; left: -2px; right: -2px; bottom: -2px; background: linear-gradient(45deg, red, orange, yellow, green, cyan, blue, violet, red); background-size: 400%; z-index: -1; animation: rainbow 5s linear infinite; border-radius: 12px; } .webdy-hotline-out-of-stock:hover { color: #fff; background-color: #000; } @keyframes rainbow { 0% { background-position: 0% 50%; } 50% { background-position: 100% 50%; } 100% { background-position: 0% 50%; } } </style>'; }); wp_script_add_data( 'webdy-hotline-style', 'group', 1 ); // đánh dấu đã chèn } $availability['availability'] = $hotline; } return $availability; } |
Mở file functions.php trong child theme của bạn (nếu chưa có child theme, bạn nên tạo child theme để tránh mất thay đổi khi update theme). Thêm đoạn code sau:
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 |
// Ẩn UX Builder và chặn truy cập trực tiếp nếu không phải Admin function webdy_hide_ux_builder_for_non_admins() { if (!current_user_can('administrator')) { // Ẩn menu UX Builder trong Admin Menu add_action('admin_menu', function() { remove_menu_page('ux-builder'); }, 999); // Chặn truy cập trực tiếp vào UX Builder qua URL add_action('admin_init', function() { $current_url = $_SERVER['REQUEST_URI']; if (strpos($current_url, 'app=uxbuilder') !== false) { wp_die('Bạn không có quyền truy cập UX Builder.'); } }); // Ẩn nút "Edit with UX Builder" trong trang chỉnh sửa bài/post add_action('add_meta_boxes', function() { remove_meta_box('ux_builder_edit_button', 'page', 'side'); remove_meta_box('ux_builder_edit_button', 'post', 'side'); // Nếu có Custom Post Type thì thêm dòng này: // remove_meta_box('ux_builder_edit_button', 'ten_custom_post_type', 'side'); }, 999); } } add_action('init', 'webdy_hide_ux_builder_for_non_admins'); |
Chép code sau vào HTML2
1 2 3 4 5 6 7 8 9 10 11 |
<!-- GTranslate: https://gtranslate.io/ --> <a href="#" onclick="doGTranslate('vi|en');return false;" title="English" class="glink nturl notranslate"><img src="/wp-content/plugins/gtranslate/flags/24/en.png" height="24" width="24" alt="English" /> <span>EN</span></a> <a href="#" onclick="doGTranslate('vi|vi');return false;" title="Vietnamese" class="glink nturl notranslate"><img src="/wp-content/plugins/gtranslate/flags/24/vi.png" height="24" width="24" alt="Vietnamese" /> <span>VI</span></a> <style>#goog-gt-tt{display:none!important;}.goog-te-banner-frame{display:none!important;}.goog-te-menu-value:hover{text-decoration:none!important;}.goog-text-highlight{background-color:transparent!important;box-shadow:none!important;}body{top:0!important;}#google_translate_element2{display:none!important;}</style> <div id="google_translate_element2"></div> <script>function googleTranslateElementInit2() {new google.translate.TranslateElement({pageLanguage: 'vi',autoDisplay: false}, 'google_translate_element2');}if(!window.gt_translate_script){window.gt_translate_script=document.createElement('script');gt_translate_script.src='https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit2';document.body.appendChild(gt_translate_script);}</script> <script> function GTranslateGetCurrentLang() {var keyValue = document['cookie'].match('(^|;) ?googtrans=([^;]*)(;|$)');return keyValue ? keyValue[2].split('/')[2] : null;} function GTranslateFireEvent(element,event){try{if(document.createEventObject){var evt=document.createEventObject();element.fireEvent('on'+event,evt)}else{var evt=document.createEvent('HTMLEvents');evt.initEvent(event,true,true);element.dispatchEvent(evt)}}catch(e){}} function doGTranslate(lang_pair){if(lang_pair.value)lang_pair=lang_pair.value;if(lang_pair=='')return;var lang=lang_pair.split('|')[1];if(GTranslateGetCurrentLang() == null && lang == lang_pair.split('|')[0])return;if(typeof ga=='function'){ga('send', 'event', 'GTranslate', lang, location.hostname+location.pathname+location.search);}var teCombo;var sel=document.getElementsByTagName('select');for(var i=0;i<sel.length;i++)if(sel[i].className.indexOf('goog-te-combo')!=-1){teCombo=sel[i];break;}if(document.getElementById('google_translate_element2')==null||document.getElementById('google_translate_element2').innerHTML.length==0||teCombo.length==0||teCombo.innerHTML.length==0){setTimeout(function(){doGTranslate(lang_pair)},500)}else{teCombo.value=lang;GTranslateFireEvent(teCombo,'change');GTranslateFireEvent(teCombo,'change')}} </script> |
Mở file functions.php trong child theme của bạn (nếu chưa có child theme, bạn nên tạo child theme để tránh mất thay đổi khi update theme). Thêm đoạn code sau:
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
// Đăng ký metabox quảng cáo function webdy_ads_metabox() { add_meta_box( 'webdy_ads_metabox', 'Quảng Cáo Dịch Vụ', 'webdy_render_ads_metabox', ['post', 'page'], 'normal', 'default' ); } add_action('add_meta_boxes', 'webdy_ads_metabox'); // Hiển thị metabox function webdy_render_ads_metabox($post) { $ads = get_post_meta($post->ID, '_webdy_ads', true); $ads = is_array($ads) ? $ads : [ ['title' => 'Thiết Kế Website', 'description' => 'Dịch vụ thiết kế website chuyên nghiệp.', 'url' => 'https://webdy.vn', 'image' => ''], ['title' => 'SEO Website', 'description' => 'Tăng thứ hạng từ khóa, tối ưu SEO.', 'url' => 'https://seomaster.com', 'image' => ''], ]; wp_nonce_field('webdy_save_ads', 'webdy_ads_nonce'); echo '<div id="webdy-ads-container">'; foreach ($ads as $index => $ad) { echo '<div class="webdy-ad-item" style="margin-bottom: 10px;"> <input type="text" name="webdy_ads[' . $index . '][title]" value="' . esc_attr($ad['title']) . '" placeholder="Tiêu đề" style="width: 20%; margin-right: 5%;"> <input type="text" name="webdy_ads[' . $index . '][description]" value="' . esc_attr($ad['description']) . '" placeholder="Mô tả" style="width: 30%; margin-right: 5%;"> <input type="url" name="webdy_ads[' . $index . '][url]" value="' . esc_url($ad['url']) . '" placeholder="URL" style="width: 20%; margin-right: 5%;"> <input type="text" name="webdy_ads[' . $index . '][image]" value="' . esc_url($ad['image']) . '" placeholder="Hình ảnh (URL)" style="width: 20%;"> <button type="button" class="remove-ad button" style="margin-left: 10px;">Remove</button> </div>'; } echo '</div><button type="button" id="add-ad" class="button">Thêm Quảng Cáo</button>'; ?> <script> document.addEventListener('DOMContentLoaded', function () { const container = document.getElementById('webdy-ads-container'); const addButton = document.getElementById('add-ad'); addButton.addEventListener('click', function () { const index = document.querySelectorAll('.webdy-ad-item').length; const newField = `<div class="webdy-ad-item" style="margin-bottom: 10px;"> <input type="text" name="webdy_ads[${index}][title]" placeholder="Tiêu đề" style="width: 20%; margin-right: 5%;"> <input type="text" name="webdy_ads[${index}][description]" placeholder="Mô tả" style="width: 30%; margin-right: 5%;"> <input type="url" name="webdy_ads[${index}][url]" placeholder="URL" style="width: 20%; margin-right: 5%;"> <input type="text" name="webdy_ads[${index}][image]" placeholder="Hình ảnh (URL)" style="width: 20%;"> <button type="button" class="remove-ad button" style="margin-left: 10px;">Remove</button> </div>`; container.insertAdjacentHTML('beforeend', newField); }); container.addEventListener('click', function (e) { if (e.target.classList.contains('remove-ad')) { e.target.parentElement.remove(); } }); }); </script> <?php } // Lưu dữ liệu quảng cáo function webdy_save_ads($post_id) { if (!isset($_POST['webdy_ads_nonce']) || !wp_verify_nonce($_POST['webdy_ads_nonce'], 'webdy_save_ads')) return; if (!current_user_can('edit_post', $post_id)) return; if (isset($_POST['webdy_ads']) && is_array($_POST['webdy_ads'])) { $cleaned_ads = array_map(function ($ad) { return [ 'title' => sanitize_text_field($ad['title']), 'description' => sanitize_text_field($ad['description']), 'url' => esc_url_raw($ad['url']), 'image' => esc_url_raw($ad['image']), ]; }, $_POST['webdy_ads']); update_post_meta($post_id, '_webdy_ads', $cleaned_ads); } else { delete_post_meta($post_id, '_webdy_ads'); } } add_action('save_post', 'webdy_save_ads'); // Hiển thị quảng cáo trên frontend function webdy_display_ads($content) { if (is_single() || is_page()) { global $post; $ads = get_post_meta($post->ID, '_webdy_ads', true); if (!empty($ads) && is_array($ads)) { $output = '<div class="webdy-ads-container">'; foreach ($ads as $ad) { if (!empty($ad['title']) && !empty($ad['url'])) { $output .= '<div class="webdy-ad">'; if (!empty($ad['image'])) { $output .= '<a href="' . esc_url($ad['url']) . '" target="_blank"><img src="' . esc_url($ad['image']) . '" style="max-width: 100px; display: block;"></a>'; } $output .= '<a href="' . esc_url($ad['url']) . '" target="_blank"><strong>' . esc_html($ad['title']) . '</strong></a>'; $output .= '<p>' . esc_html($ad['description']) . '</p>'; $output .= '</div>'; } } $output .= '</div>'; $content .= $output; } } return $content; } add_filter('the_content', 'webdy_display_ads'); // Thêm CSS function webdy_ads_styles() { echo '<style> .webdy-ads-container { border: 2px dotted #4CAF50; background-color: #e8f5e9; padding: 15px; margin-top: 20px; font-family: Arial, sans-serif; border-radius: 10px; position: relative; } .webdy-ad { margin-bottom: 15px; } .webdy-ad a { color: #2E7D32; font-size: 16px; font-weight: bold; text-decoration: none; } .webdy-ad p { margin: 5px 0; color: #333; font-size: 14px; } .webdy-ad img { max-width: 100px; border-radius: 5px; margin-bottom: 5px; } </style>'; } add_action('wp_head', 'webdy_ads_styles'); |
Mở file functions.php trong child theme của bạn (nếu chưa có child theme, bạn nên tạo child theme để tránh mất thay đổi khi update theme). Thêm đoạn code sau:
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
/*thêm link sau bài viết*/ // Đăng ký metabox function custom_links_metabox() { add_meta_box( 'custom_links_metabox', 'Custom Links', 'render_custom_links_metabox', ['post', 'page'], // Áp dụng cho bài viết và trang 'normal', 'default' ); } add_action('add_meta_boxes', 'custom_links_metabox'); // Hiển thị metabox function render_custom_links_metabox($post) { // Lấy giá trị meta đã lưu $custom_links = get_post_meta($post->ID, '_custom_links', true); // Nếu không có dữ liệu, sử dụng dữ liệu mẫu mặc định $custom_links = is_array($custom_links) ? $custom_links : [ ['description' => 'Zalo', 'url' => 'https://zalo.me/0902299161'], ['description' => 'Facebook', 'url' => 'https://facebook.com/webdy.vn'], ['description' => 'YouTube', 'url' => 'https://youtube.com'], ['description' => 'Instagram', 'url' => 'https://instagram.com'], ['description' => 'Twitter', 'url' => 'https://twitter.com'] ]; // Nonce để bảo mật wp_nonce_field('save_custom_links', 'custom_links_nonce'); // Hiển thị các trường nhập liệu echo '<div id="custom-links-container">'; foreach ($custom_links as $index => $link) { echo '<div class="custom-link-item" style="margin-bottom: 10px;">'; echo '<input type="text" name="custom_links[' . $index . '][description]" value="' . esc_attr($link['description']) . '" placeholder="Description" style="width: 45%; margin-right: 5%;">'; echo '<input type="url" name="custom_links[' . $index . '][url]" value="' . esc_url($link['url']) . '" placeholder="URL" style="width: 45%;">'; echo '<button type="button" class="remove-link button" style="margin-left: 10px;">Remove</button>'; echo '</div>'; } echo '</div>'; echo '<button type="button" id="add-link" class="button">Add Link</button>'; // Thêm script JS để xử lý thêm và xóa link ?> <script> document.addEventListener('DOMContentLoaded', function () { const container = document.getElementById('custom-links-container'); const addButton = document.getElementById('add-link'); addButton.addEventListener('click', function () { const index = container.children.length; const newField = ` <div class="custom-link-item" style="margin-bottom: 10px;"> <input type="text" name="custom_links[${index}][description]" placeholder="Description" style="width: 45%; margin-right: 5%;"> <input type="url" name="custom_links[${index}][url]" placeholder="URL" style="width: 45%;"> <button type="button" class="remove-link button" style="margin-left: 10px;">Remove</button> </div>`; container.insertAdjacentHTML('beforeend', newField); }); container.addEventListener('click', function (e) { if (e.target.classList.contains('remove-link')) { e.target.parentElement.remove(); } }); }); </script> <?php } // Lưu dữ liệu meta function save_custom_links($post_id) { // Kiểm tra bảo mật if (!isset($_POST['custom_links_nonce']) || !wp_verify_nonce($_POST['custom_links_nonce'], 'save_custom_links')) { return; } // Kiểm tra quyền chỉnh sửa if (!current_user_can('edit_post', $post_id)) { return; } // Lưu dữ liệu if (isset($_POST['custom_links']) && is_array($_POST['custom_links'])) { $cleaned_links = array_map(function ($link) { return [ 'description' => sanitize_text_field($link['description']), 'url' => esc_url_raw($link['url']), ]; }, $_POST['custom_links']); update_post_meta($post_id, '_custom_links', $cleaned_links); } else { delete_post_meta($post_id, '_custom_links'); } } add_action('save_post', 'save_custom_links'); // Hiển thị Custom Links dưới bài viết hoặc trang function display_custom_links_on_frontend($content) { if (is_single() || is_page()) { global $post; $custom_links = get_post_meta($post->ID, '_custom_links', true); if (!empty($custom_links) && is_array($custom_links)) { $output = '<div class="custom-links" style="margin-top: 20px; padding: 10px; border-top: 1px solid #ddd;">'; $output .= '<h4> <span class="gradient">Quý khách cần tư vấn thiết kế web giá rẻ, vui lòng nhấn nút bên dưới:</span></h4>'; $output .= '<div class="link-buttons" style="display: flex; flex-wrap: wrap; gap: 10px;">'; foreach ($custom_links as $link) { $description = esc_html($link['description']); $url = esc_url($link['url']); if (!empty($description) && !empty($url)) { $output .= '<a href="' . $url . '" target="_blank" rel="noopener noreferrer" class="custom-link-button">'; $output .= $description; $output .= '</a>'; } } $output .= '</div>'; $output .= '</div>'; $content .= $output; } } return $content; } add_filter('the_content', 'display_custom_links_on_frontend'); /////----------- |
Mở file functions.php trong child theme của bạn (nếu chưa có child theme, bạn nên tạo child theme để tránh mất thay đổi khi update theme). Thêm đoạn code sau:
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 |
// Thêm cột "Views" vào danh sách bài viết trong Dashboard add_filter('manage_posts_columns', 'posts_column_views'); add_action('manage_posts_custom_column', 'posts_custom_column_views', 5, 2); function posts_column_views($defaults){ $defaults['post_views'] = __('Views'); // Thêm cột "Views" return $defaults; } function posts_custom_column_views($column_name, $post_id){ if ($column_name === 'post_views') { echo getPostViews($post_id); // Hiển thị số lượt xem } } // Hàm lấy số lượt xem của bài viết function getPostViews($postID) { $count = get_post_meta($postID, 'post_views_count', true); return ($count) ? $count : 0; } // Hàm tăng số lượt xem mỗi khi có người truy cập bài viết function setPostViews($postID) { $count = get_post_meta($postID, 'post_views_count', true); $count = ($count) ? $count + 1 : 1; update_post_meta($postID, 'post_views_count', $count); } // Gọi hàm setPostViews() trong single.php để tăng số lượt xem khi có người đọc bài add_action('wp_head', function() { if (is_single()) { setPostViews(get_the_ID()); } }); |