Có những đoạn code, câu lệnh mà mình hay phải dùng do vậy mình ghi lại ở bài viết này để khi cần mình mở ra xem, vì thế mà có thể bạn sẽ không tìm thấy gì giúp ích cho bạn ở đây cả!
Regex
#Dot \. #Anything (.*?) # \.(.*?)\.
Cài HocVPS Script
curl -sO https://hocvps.com/install && bash install
Sau khi cài đăng nhập với port 2222. Xem cập nhật tại đây.
Cài WordPress
Nếu lần đầu cài WordPress trên VPS cần chạy đoạn code sau:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar chmod +x wp-cli.phar sudo mv wp-cli.phar /usr/bin/wp
Lệnh tải mã nguồn WordPress về thư mục cần cài:
wp core download --allow-root
Sau khi tải xong chạy lệnh:
chown -R nginx:nginx /home/www.domain.com/public_html hoặc chown -R nginx:nginx /home/*/public_html
Xóa thư mục
rm -rf mydir
Zip các file
zip -r file *
Test và restart nginx
nginx -t service nginx restart
Chặn truy cập IP
server { listen 80 default_server; server_name _; # some invalid name that won't match anything return 444; }
Thêm Alias IP Centos
Copy file mẫu:
cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0:0
Sửa file:
/etc/sysconfig/network-scripts/ifcfg-eth0:0
Nội dung file:
DEVICE="eth0:0" ONBOOT="yes" BOOTPROTO="static" IPADDR="IP_FAILOVER" NETMASK="255.255.255.255" BROADCAST="IP_FAILOVER"
Restart và reboot:
ifup eth0:0 reboot
www hoặc non-www
#www to non-www server { listen 80; server_name www.domain.com; return 301 $scheme://domain.com$request_uri; } #non-www to www server { listen 80; server_name domain.com; return 301 $scheme://www.domain.com$request_uri; }
Bỏ file index.html hoặc index.php khỏi URL
server_name domain.com; if ($request_uri ~ "/(index|index.(php|html?))") { rewrite ^ /$1 permanent; }
Bỏ đuôi .html
server_name domain.com; if ($request_uri ~ "/(index|index.(php|html?))") { rewrite ^ /$1 permanent; } if ($uri !~ /index\.html$) { rewrite ^(/.+)\.html$ $scheme://$host$1 permanent; }
Redirect trang 404
server_name www.domain.com; error_page 404 = @my404; location @my404 { return 301 /; }
Redirect URL
server_name www.domain.com; rewrite ^/sample-url/$ https://domain.com/another-url/ permanent; #Redirect các url chung folder server_name www.domain.com; location ~ ^/folder-folder/(.*)$ { return 301 https://domain.com/; } #Redirect các url chung đuôi .html server_name www.domain.com; rewrite ^(/.+)\.html$ https://domain.com/ permanent;
Cloudflare Wildcard Redirect
*domain.com/* => https://anotherdomain.com/$2 #hoặc *domain.com/* => https://www.anotherdomain.com/$2 #hoặc https://domain.com/* => https://anotherdomain.com/$1 #hoặc *domain.com/* => https://$1anotherdomain.com/$2 # $1 tương ứng với * thứ nhất và $2 tương ứng với * thứ 2 và tương tự.
Thêm site mới LAMP
mkdir -p /var/www/domain.com/public_html mkdir -p /var/www/domain.com/log chown -R www-data:www-data /var/www/domain.com cp /etc/apache2/sites-available/default.com.conf /etc/apache2/sites-available/domain.com.conf a2ensite domain.com service apache2 reload
PHP
//single <?php if (( is_single())) { ?> <div style="text-align:center;"> ... </div> <?php } ?> //theo template if (( !is_page_template( 'page_landing.php' ) )) //front page if ((is_front_page() )) // if (( is_front_page() || is_single())) // if (( is_single() && !is_single(1561)))
Xenforo
<xen:if is="!in_array({$contentTemplate}, array('message_page', 'error', 'search_form', 'search_form_post', 'search_form_profile_post', 'search_results', 'register_form', 'login', 'error_with_login', 'contact', 'moderation_queue_list', 'report_list'))"> #html </xen:if> <xen:if is="in_array({$contentTemplate}, array('forum_list', 'thread_view'))"> #html </xen:if> <xen:if is="{$contentTemplate} == 'forum_list'"> #html </xen:if>
Footer Genesis
add_shortcode( 'sitename', 'site_name' ); function site_name() { return '<a href="' . get_bloginfo( 'url' ) . '" title="' . get_bloginfo( 'sitename' ) . '">' . get_bloginfo( 'name' ) . '</a>'; } add_filter('genesis_footer_creds_text', 'sp_footer_creds_filter'); function sp_footer_creds_filter( $creds ) { $creds = '[footer_copyright] · [sitename] · <a href="/about/">About</a>'; return $creds; }
Thumbnail Genesis
add_image_size( 'thumb65', 65, 65, TRUE );
Xoá Links
add_filter( 'the_content', 'filter_the_content_in_the_main_loop' ); function filter_the_content_in_the_main_loop( $content ) { if ( !is_admin() ) { $content = preg_replace(array('"<a href(.*?)>"', '"</a>"'), array('',''), $content); } return $content; }
Thêm Related Post (Radom) sau đoạn đầu bài viết
//* Hiển thị random post trong bài viết //* Tạo shortcode function wpb_rand_posts() { $args = array( 'post_type' => 'post', 'orderby' => 'rand', 'posts_per_page' => 3, ); $the_query = new WP_Query( $args ); if ( $the_query->have_posts() ) { $string .= '<ul>'; while ( $the_query->have_posts() ) { $the_query->the_post(); $string .= '<li><a href="'. get_permalink() .'">'. get_the_title() .'</a></li>'; } $string .= '</ul>'; /* Restore original Post Data */ wp_reset_postdata(); } else { $string .= 'no posts found'; } return $string; } add_shortcode('wpb-random-posts','wpb_rand_posts'); add_filter('widget_text', 'do_shortcode'); //Hiển thị shortcode sau đoạn đầu tiên trong bài viết add_filter( 'the_content', 'prefix_insert_post_ads' ); function prefix_insert_post_ads( $content ) { $ad_code = '<div class="see-also"><p>See also:</p>[wpb-random-posts]</div>'; if ( is_single() && ! is_admin() ) { return prefix_insert_after_paragraph( $ad_code, 1, $content ); } //Nếu muốn hiển thị post ở sau đoạn 2, 3,.. thì sửa 1 thành 2,3,... return $content; } // Parent Function function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) { $closing_p = '</p>'; $paragraphs = explode( $closing_p, $content ); foreach ($paragraphs as $index => $paragraph) { if ( trim( $paragraph ) ) { $paragraphs[$index] .= $closing_p; } if ( $paragraph_id == $index + 1 ) { $paragraphs[$index] .= $insertion; } } return implode( '', $paragraphs ); } // Nếu muốn hiển thị ở trước đoạn cuối cùng thì thay từ chỗ Hiển thị shortcode thành function ads_added_above_last_p($text) { if( is_single() ) : $ads_text = '<div class="see-also"><p>See also:</p>[wpb-random-posts]</div>'; if($pos1 = strrpos($text, '<p>')){ $text1 = substr($text, 0, $pos1); $text2 = substr($text, $pos1); $text = $text1 . $ads_text . $text2; } endif; return $text; } add_filter('the_content', 'ads_added_above_last_p'); //CSS .see-also ul { font-size: 15px; font-weight: 700; margin-left: 30px; line-height: 1.4; } .see-also p { font-weight: 700; margin-bottom: 10px; }
Thêm Related Post by Category sau đoạn đầu bài viết
//Related Post by Category //Tạo shortcode function wp_cats_posts() { // Default arguments $args = array( 'posts_per_page' => 4, // How many items to display 'post__not_in' => array( get_the_ID() ), // Exclude current post 'no_found_rows' => true, // We don't ned pagination so this speeds up the query ); // Check for current post category and add tax_query to the query arguments $cats = wp_get_post_terms( get_the_ID(), 'category' ); $cats_ids = array(); foreach( $cats as $wpex_related_cat ) { $cats_ids[] = $wpex_related_cat->term_id; } if ( ! empty( $cats_ids ) ) { $args['category__in'] = $cats_ids; } // Query posts $wpex_query = new wp_query( $args ); if ( $wpex_query->have_posts() ) { $string .= '<ul>'; while ( $wpex_query->have_posts() ) { $wpex_query->the_post(); $string .= '<li><a href="'. get_permalink() .'">'. get_the_title() .'</a></li>'; } $string .= '</ul>'; /* Restore original Post Data */ wp_reset_postdata(); } else { $string .= 'no posts found'; } return $string; } add_shortcode('wp-cats-posts','wp_cats_posts'); add_filter('widget_text', 'do_shortcode');
//Insert ads after first paragraph of single post content. add_filter( 'the_content', 'prefix_insert_post_ads' ); function prefix_insert_post_ads( $content ) { $ad_code = '<div class="see-also"><p>See also:</p>[wp-cats-posts]</div>'; if ( is_single() && ! is_admin() ) { return prefix_insert_after_paragraph( $ad_code, 1, $content ); } return $content; } // Parent Function that makes the magic happen function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) { $closing_p = '</p>'; $paragraphs = explode( $closing_p, $content ); foreach ($paragraphs as $index => $paragraph) { if ( trim( $paragraph ) ) { $paragraphs[$index] .= $closing_p; } if ( $paragraph_id == $index + 1 ) { $paragraphs[$index] .= $insertion; } } return implode( '', $paragraphs ); }
Noindex Subpage Genesis
/*Add noindex to low value pages*/ function add_noindex_tags(){ # Get page number for paginated archives. $paged = intval( get_query_var( 'paged' ) ); # Add noindex tag to homepage paginated pages. if(( is_home() || is_front_page() ) && $paged >= 2 ) echo '<meta name="robots" content="noindex,follow">'; } add_action('wp_head','add_noindex_tags', 4 );
Redirect author
function theme_slug_redirect_author_archive() { if ( is_author() ) { header("Location: /"); die(); } } add_action( 'template_redirect', 'theme_slug_redirect_author_archive' );
Đổi author link
add_filter( 'author_link', 'my_author_link' ); function my_author_link() { return home_url( 'about' ); }