Tổng hợp một số code cho Theme Flatsome và cũng có thể tương thích với các mẫu theme wordpress khác !
Các code liên quan tới bảo mật, ẩn hiện một vài chức năng như bình luận, tối ưu bài viết…
Loại Bỏ URL Box Comment
Code giúp loại bỏ ô URL trong hộp bình luận mặc định của WP
1 2 3 4 5 6 7 |
/*************** Remove URL of Comment Box ***************/ add_filter('comment_form_default_fields', 'unset_url_field'); function unset_url_field($fields){ if(isset($fields['url'])) unset($fields['url']); return $fields; } |
P/s:
Phải thêm code vào file function cả ở theme & file function trong host !
Redirect Author To Homepage
Thêm Code dưới đây vô file function.php sẽ giúp chuyển hướng Author về trang chủ (nếu chưa login or login bằng acc không phải là administrator)
1 2 3 4 5 6 7 8 |
function wp_redirect_disable_author_query() { $is_author_set = get_query_var( 'author', '' ); if ( $is_author_set != '' && !is_admin()) { wp_redirect( home_url(), 301 ); exit; } } add_action( 'template_redirect', 'wp_redirect_disable_author_query' ); |
Chạy nhiều domain trên 01 web
Đầu tiên phải tiến hành add domain vô Host. Trỏ IP trong DNS domain về Host. Có thể xài plugin: Multiple Domain
- Nếu bạn cần chuyển đổi domain mới thì tham khảo bài viết: Cách đổi domain wp
Giải pháp Canonical Link để tránh trùng lặp nội dung khi website của bạn có nhiều domain cùng trỏ về
Thêm code dưới đây vô file function.php
1 2 3 4 5 6 7 8 |
//canonical - old domain to new domain add_filter('wpseo_canonical', 'swpseo_canonical_domain_replace'); function swpseo_canonical_domain_replace($url){ $domain = 'your-domain.com';// Thay đổi cái này về site chính của bạn vd ở đây là your-domain.com $parsed = parse_url(home_url()); $current_site_domain = $parsed['host']; return str_replace($current_site_domain, $domain, $url); } |
Nếu chưa rõ Canonical là gì thì xem: Thuật Ngữ SEO
Code chức năng import ảnh bằng copy & paste
Giải pháp copy một bài viết có hình ảnh từ web khác về website của mình ? Mà không phải saved ảnh rồi upload hình ảnh lên ?
Một giải pháp cực hay thay thế một vài plugin nào đó ~
Code dưới đây có khả năng sẽ giúp giản hóa công việc nhờ việc web sẽ tự động import hình ảnh sau khi copy/paste vào bài viết. Việc còn lại chỉ là edit bài viết cho đẹp rồi >> Publish !
Code >>Copy >> Paste in Function file.
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 |
//========================================// class Auto_Save_Images{ function __construct(){ add_filter( 'content_save_pre',array($this,'post_save_images') ); } function post_save_images( $content ){ if( ($_POST['save'] || $_POST['publish'] )){ set_time_limit(240); global $post; $post_id=$post->ID; $preg=preg_match_all('/<img.*?src="(.*?)"/',stripslashes($content),$matches); if($preg){ foreach($matches[1] as $image_url){ if(empty($image_url)) continue; $pos=strpos($image_url,$_SERVER['HTTP_HOST']); if($pos===false){ $res=$this->save_images($image_url,$post_id); $replace=$res['url']; $content=str_replace($image_url,$replace,$content); } } } } remove_filter( 'content_save_pre', array( $this, 'post_save_images' ) ); return $content; } function save_images($image_url,$post_id){ $file=file_get_contents($image_url); $post = get_post($post_id); $posttitle = $post->post_title; $postname = sanitize_title($posttitle); $im_name = "$postname-$post_id.jpg"; $res=wp_upload_bits($im_name,'',$file); $this->insert_attachment($res['file'],$post_id); return $res; } function insert_attachment($file,$id){ $dirs=wp_upload_dir(); $filetype=wp_check_filetype($file); $attachment=array( 'guid'=>$dirs['baseurl'].'/'._wp_relative_upload_path($file), 'post_mime_type'=>$filetype['type'], 'post_title'=>preg_replace('/\.[^.]+$/','',basename($file)), 'post_content'=>'', 'post_status'=>'inherit' ); $attach_id=wp_insert_attachment($attachment,$file,$id); $attach_data=wp_generate_attachment_metadata($attach_id,$file); wp_update_attachment_metadata($attach_id,$attach_data); return $attach_id; } } new Auto_Save_Images(); //========================================// |
Code trên sẽ chèn vô file function trên theme nhé
How to Fix 404 on AMP pages after deactivating the AMP plugin
Tâm hư: sau khi xài AMP thì thấy NHANH QUÁ ! nhưng không hiệu quả hơn trong SEO & không tối ưu được cho quảng cáo adsense ! ..v.v.. Mình đã quyết định bỏ AMP
Vì mình đang xài Plugin Yoast SEO PRO nên có tính năng tạo các chuyển hướng REDIRECT
Các làm thì có thể tạo từng chuyển hướng 301 đơn lẻ từng link một. Nhưng thủ công quá. Hãy dùng tính năng: Regular Expression redirects
Ví dụ link một web của mình có AMP nè : domain.com/amp/postname.html
Cần chuyển về link cũ: domain.com/postname.html
Regular expression:
1 |
amp/(.*)\.html |
To URL:
1 |
/$1.html |
Có web thì AMP nằm cuối link, thì bạn chỉ cần đảo vị trí cho phù hợp. Hoặc link của bạn không có .html thì bỏ đi thôi !
Đã thử nghiệm thành công 99% :)) Còn trang chủ thì tạo thêm một cái 301 nữa nhé !
Ngoài ra các BRO còn có code thêm vào file .htacess Nhưng mình không thấy ngon lắm !
Tham khảo thêm về Yoast SEO PRO nè!
Responsive Bảng tính trong bài viết
Một số trường hợp bảng tính table không được tương thích tốt với màn hình nhiều kích cỡ. Nên post bài viết có bảng báo giá này nọ, bảng tính các loại… thì nên check trên các kích thước màn hình coi sao nhé!
Có thể thêm code dưới đây:
1 2 3 4 5 |
<div style="overflow-x:auto;"> < bảng tính > </div> |
Hoặc bạn có thể xài một số plugin hỗ trợ cho TABLE