Giải pháp hiệu quả tăng Traffic cho website
Traffic Exchange Site

một số code php lưu

Code PHP

Auto ping random post WordPress

random post

domain.com/index.php?random=1
Mở file functions.php và thêm vào

add_action('init','random_add_rewrite');
function random_add_rewrite() {
global $wp;
$wp->add_query_var('random');
add_rewrite_rule('random/?$', 'index.php?random=1', 'top');
}
add_action('template_redirect','random_template');
function random_template() {
if (get_query_var('random') == 1) {
$posts = get_posts('post_type=post&orderby=rand&numberposts=1');
foreach($posts as $post) {
$link = get_permalink($post);
}
wp_redirect($link,307);
exit;
}
}

Tạo file ping.php với nội dung (thay domain cần index vào)

Chạy file ping.php với delay 10s/shot bằng các cách như: set cronjob, add-on autoreload, tag meta refresh, iframe ...
Nên có widget Recent Posts, Related Posts, Random Posts (tag a nên có thêm title="..."), ngoài ra có thể chèn internal links bằng các anchor text: more, here, read more ...
$sitemapUrl = 'https://domain.com/index.php?random=1';

function myCurl($url){
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_exec($ch);
  $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  curl_close($ch);
  return $httpCode;
}
//Google
$url = "http://www.google.com/webmasters/sitemaps/ping?sitemap=".$sitemapUrl;
$returnCode = myCurl($url);
echo "<p>Google Sitemaps has been pinged (return code: $returnCode).</p><hr>";
//Bing / MSN
$url = "http://www.bing.com/webmaster/ping.aspx?siteMap=".$sitemapUrl;
$returnCode = myCurl($url);
echo "<p>Bing / MSN Sitemaps has been pinged (return code: $returnCode).</p><hr>";

random tags

<?php rand_get_tags_but_exclude(); ?>
function rand_get_tags_but_exclude() {
    $args = array('exclude' => '');
        $alltags = get_tags( $args );
        shuffle($alltags);
        $count=0;
        if ($alltags) {
            foreach($alltags as $tag) {
                $count++;
                echo '<a href="'.get_tag_link($tag->term_id).'" title="'.$tag->name.'">'.$tag->name.'</a>, ';
        if( $count >45) break;
        }
    }
}

get_first_image

function get_first_image() {
  global $post, $posts;
  $first_img = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
  $first_img = $matches[1][0];

  if(empty($first_img)) {
$first_img = "/wp-content/uploads/2017/05/AAA-Favicon.jpg";
  }
  return $first_img;
}

randompost (có ảnh ok)

function create_shortcode_randompost() {

        $random_query = new WP_Query(array(
                'posts_per_page' => 5,
                'orderby' => 'rand'
        ));

        ob_start();
        if ( $random_query->have_posts() ) :
                "<ol>";
                while ( $random_query->have_posts() ) :
                        $random_query->the_post();?>
 <div class="listcontent">
   <div class="leftimg">
  <a href="<?php the_permalink(); ?>" class="mid-thumbnail">
    <img src="<?php echo get_first_image(); ?>" alt="<?php the_title(); ?>" />
   </a>
  </div>
  <div class="right">
   <h3><a href="<?php the_permalink()?>" rel="bookmark"><?php the_title();?></a></h3>
   <div class="clear"></div>
   <div class="except">
   <?php zt_content_limit(275, ""); ?></div>
    </div>
 </div>
                <?php endwhile;
                "</ol>";
        endif;
        $list_post = ob_get_contents();

        ob_end_clean();

        return $list_post;
}
add_shortcode('random_post', 'create_shortcode_randompost');

RandomPostSidebar cho Sidebar

function create_shortcode_randompost_sidebar() {

        $random_query = new WP_Query(array(
                'posts_per_page' => 10,
                'orderby' => 'rand'
        ));

        ob_start();
        if ( $random_query->have_posts() ) :
                "<ol>";
                while ( $random_query->have_posts() ) :
                        $random_query->the_post();?>
<a href="<?php the_permalink(); ?>" alt="<?php the_title(); ?>"><?php the_title(); ?></a> <br/><br/>

<?php endwhile;
                "</ol>";
        endif;
        $list_post = ob_get_contents();

        ob_end_clean();

        return $list_post;
}
add_shortcode('random_post_sidebar', 'create_shortcode_randompost_sidebar');

Một số CSS cho random post ở trên

.listcontent { float: left; display: flex;}
.leftimg { min-width: 200px; padding: 0 5px 0 0; float: left;}
.right { flex-grow: 1; float: right;  padding: 5px;}
.except { display: flex; padding: 8px 0 10px 20px; }
.right h3 { margin: 0; color: #047; padding: 0 0 5px 10px; font-size: 1.4em; font-weight: bold; float: right;}
.mid-thumbn img { width: 200px; height: 200px; float: left;}

Chạy shortcode trong widget

có thể cần thêm code này vào function.php    add_filter('widget_text', 'do_shortcode');

Cách viết shortcode vào file PHP

<?php echo do_shortcode('[test_shortcode]'); ?>

//Khởi tạo function cho shortcode sitename va siteurl//

function shortcode_sitename() {
 $blog_title = get_bloginfo();
        return "$blog_title";
}
add_shortcode( 'site_name', 'shortcode_sitename' );
function shortcode_siteurl() {
 $blog_url = get_site_url();
        return "$blog_url";
}
add_shortcode( 'site_url', 'shortcode_siteurl' );


# Limit Post

function zt_content_limit($max_char, $more_link_text = '', $stripteaser = 0, $more_file = '') {
    $content = get_the_content($more_link_text, $stripteaser, $more_file);
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]&gt;', $content);
    $content = strip_tags($content);
   if (strlen($_GET['p']) > 0) {
      echo "";
      echo $content;
      echo "&nbsp;<a href='";
      the_permalink();
      echo "'>"."Read More &rarr;</a>";
      echo "";
   }
   else if ((strlen($content)>$max_char) && ($espacio = strpos($content, " ", $max_char ))) {
        $content = substr($content, 0, $espacio);
        $content = $content;
        echo "";
        echo $content;
        echo "...";
        echo "&nbsp;<a href='";
        the_permalink();
        echo "'>".$more_link_text."</a>";
        echo "";
   }
   else {
      echo "";
      echo $content;
      echo "&nbsp;<a href='";
      the_permalink();
      echo "'>"."Read More &rarr;</a>";
      echo "";
   }
}

Limit text cắt đoạn- Code khác 

<?php
function text_limit($str,$limit=10)
 {
 if(stripos($str," ")){
 $ex_str = explode(" ",$str);
 if(count($ex_str)>$limit){
 for($i=0;$i<$limit;$i++){
 $str_s.=$ex_str[$i]." ";
 }
 return $str_s;
 }else{
 return $str;
 }
 }else{
 return $str;
 }
 }
?>
Hàm trên hoạt động như sau:
– Chỉ định bao nhiêu câu mà bạn muốn hiển thị
– Tìm câu cuối cùng hiển thị là gì
– Nếu câu cuối không phải là khoảng trắng thì nó sẽ tìm đến câu kế tiếp và tìm cho đến khi thấy khoảng trắng.
– Hiển thị ra màn hình
Ví dụ dùng cho hàm trên như sau :
<?php
echo text_limit('Chao Mung Ban Den Voi Thu Thuat Web',7); Xuất ra "Chao Mung Ban Den Voi Thu Thuat "
echo text_limit('Chao Mung Ban Den Voi Thu Thuat Web',3); Xuất ra "Chao Mung Ban"
?> 

 Function Replace

function bbit_doi_aff_id($text)
{
    $return = str_replace('network20', 'fastoloswei', $text);
    return $return;
}
add_filter('the_content', 'bbit_doi_aff_id');
add_filter('the_excerpt', 'bbit_doi_aff_id');

Spin content

// Dạng Spin
// [spin("[Buy|Cheap|Hot|Top|Review for]")]
function spin($text){
    $test = preg_match_all("#\[(.*?)\]#", $text, $out);
    if (!$test) return $text;
    $toFind = Array();
    $toReplace = Array();
    foreach($out[0] AS $id => $match){
    $choices = explode("|", $out[1][$id]);
    $toFind[]=$match;
    $toReplace[]=trim($choices[rand(0, count($choices)-1)]);
    }
    return str_replace($toFind, $toReplace, $text);

CSS

CSS chung

(xem thêm https://www.hostinger.vn/huong-dan/lam-the-nao-de-tao-theme-wordpress-responsive-bang-html5/)
Tốt nhất là để hiển thị theo tỷ lệ %
Phần đầu nên có trong style.css
* {
 box-sizing: border-box;
}
body {
background-color: #f9f9f9;
font-family: Helvetica;
}

Màu hay dùng

Đỏ thẫm #d23f50
Đỏ tươi #fd081e
Nâu nhạt #333
Xanh thẫm (xanh nước biển) #000477
Xanh nhạt hơn #1708fd
Xanh lá #064e05
Vaàng #e2f108
đen #000000
Trắng #fff

Quảng cáo Ads

Chèn ads giữa bài viết

   //Insert ads after second paragraph of single post content.
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = '<div><center><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- benhphoivalao_sidebar-right-1_AdSense1_1x1_as -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-4433132827328600"
     data-ad-slot="6385567975"
     data-ad-format="auto"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></center></div>';
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 2, $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 );
}

Một số code SQL


SELECT * FROM `suckhoeh_nhathuocnghiado`.`wp_posts`where`ID`>'12326'
DELETE FROM `suckhoeh_nhathuocnghiado`.`wp_posts`where`id`>9001
============Replace text in mysql============
UPDATE `wp_posts` SET `post_date` = replace(post_date, '0000-00-00 00:00:00', '2015:10:03 10:00:00')

Blogspot

Replace trong blogspot

đặt cuối cùng, trước thẻ đóng /body
<script type='text/javascript'>
 bodyText = document.getElementById("Blog1");
 theText = bodyText.innerHTML;
 theText = theText.replace("fastoloswei", "tbs0b");
 bodyText.innerHTML = theText;
</script>

Cách tạo widget cố định khi cuộn trang

Post phần code sau trực tiếp trong phần layout (gadget) định đặt code trong sidebar.
<script>
var $stickyHeight = 460; // chiều cao của banner quảng cáo
var $padding = 5; // khoảng cách top của banner khi dính
var $topOffset = 4560; // khoảng cách từ top của banner khi bắt đầu dính (tức là khoảng cách tính từ trên xuống đến vị trí đặt banner )
var $footerHeight = 1200; // Định vị điểm dừng của banner, tính từ chân lên
/* <![CDATA[ */
function scrollSticky(){
if($(window).height() >= $stickyHeight) {
var aOffset = $('#sticky').offset();
if($(document).height() - $footerHeight - $padding < $(window).scrollTop() + $stickyHeight) {
var $top = $(document).height() - $stickyHeight - $footerHeight - $padding - 185;
$('#sticky').attr('style', 'position:absolute; top:'+$top+'px;');
}else if($(window).scrollTop() + $padding > $topOffset) {
$('#sticky').attr('style', 'position:fixed; top:'+$padding+'px;');
}else{
$('#sticky').attr('style', 'position:relative;');
}
}
}
$(window).scroll(function(){
scrollSticky();
});
/* ]]> */
</script>
<div id="sticky">
Đặt code quảng cáo của bạn
</div>

Code cố định Sidebar khi Scroll cuộn chuột lên xuống wordpress


Bước 1: Tạo phần tử muốn đóng đinh trong HTML và chỉnh sửa trong CSS tùy ý bạn
<div id="sidebar">
 <h3>Sidebar</h3>
 <p>Sidebar Content</p>
</div>
Bước 2: Link tới thư viện jQuery trong phần <head> của mã nguồn web:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

Bước 3: Viết code jQuery điều khiển phẩn tử muốn đóng đinh trong sự kiện ready

$(document).ready(function () {
 $.lockfixed("#sidebar", {offset: {top: 20, bottom: 470} });
});

Trong đó,

– #sidebar: là phần tử muốn điều khiển
– top: tọa độ fixed phía trên của phần tử đóng đinh (so với top của trình duyệt) khi người dùng cuộn xuống.
– bottom: tọa độ fixed đáy của phần tử (so với bottom của trình duyệt) khi phần tử chạm đáy phần tử cha khi người dùng cuộn xuống dưới cùng trang web (Thường là với các trang có footer dài)
Trong VD này, footer dài 450px, #sidabar cách footer 20px nên giá trị cách đáy của trình duyệt là 470px.

WordPress tiếp

Code quảng cáo Popup under javascript ẩn đằng sau trình duyệt

Bạn hãy chép toàn bộ đoạn code quảng cáo popunder dưới đây vào mã nguồn của bạn
<script type='text/javascript'>
window.onload = function() {
    var puURL = &#39;https://gicovietnam.blogspot.com/&#39;;
    var puTS = Math.round(+new Date()/1000);
    //var puDate = currentDate.getDate().toString(); //day
    console.log(&#39;T.&#39;+localStorage.puTS+&#39;/&#39;+puTS);
    if (typeof localStorage.puTS == &#39;undefined&#39; || parseInt(localStorage.puTS) &lt;= (puTS - 3000)) {
        console.log(&#39;Yep&#39;);
        var links = document.getElementsByTagName(&#39;a&#39;);
        for(var i = 0, len = links.length; i &lt; len; i++) {
          links[i].onclick = function (e) {
              var puHref = this.getAttribute(&quot;href&quot;);
                var puTarget = this.getAttribute(&quot;target&quot;);
                if (puHref !== &#39;#&#39; &amp;&amp; puHref !== &#39;javascript:void(0)&#39;) {
                e.preventDefault();   
                if (puTarget == &#39;_blank&#39;) {
                    window.open(window.location.href);
                }
                window.open(puHref);
                window.location.href = puURL;
                localStorage.puTS = puTS;
              }
            }
        }
    }
};
</script>
Bạn chỉ cần thay tên miền https://gicovietnam.blogspot.com thành tên miền bạn muốn quảng cáo

Tùy chỉnh dòng code này var puTS = Math.round(+new Date()/1000); theo thời gian xuất hiện quảng cáo giữa các lần.

 VPS

Cài VPS

Cài cho bản Centos 7 trở lên, hơi khác với Centos 6:
yum install wget
wget http://software.virtualmin.com/gpl/scripts/install.sh
yum install perl
/bin/sh install.sh
/bin/systemct1 stop httpd.service (hoặc /sbin/service httpd stop)
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
sudo yum install nginx
sudo systemctl start nginx.service
yum install wbm-virtualmin-nginx wbm-virtualmin-nginx-ssl
chkconfig httpd off

if (!-e $request_filename) {
 rewrite ^.*$ /index.php last;
}
------------sau khi create server
-------------tiến hành chuyển website từ host/share host khác sang
bước 1: vào file manager của website cũ, chọn tất cả các mục trong wordpress (select all) -> Compress all để nén lại.
bước 2: chuyển file nén zip/tar này sang folder của domain trong vps mới, đặt trong mục Public_html
Bước 3: chuyển file sql của database.
- vào phpmyadmin/ hoặc msd của website cũ, export file sql tất cả các cài đặt (full all table)
- trong vps mới, vào database (edit database) --> tạo tên 1 database mới trùng với tên của database cũ.
Chọn vào manager của database mới này --> chọn excute sql --> chọn Run SQL from file (không chọn import text file vì không được-đã test)
sau khi import xong. còn phải đổi tên user và pass của database trong phần config.php của wordpress (chưa test đổi tên user trong database cho trùng!)
bước 4: vào file manager -> sửa file config.php, kiểm tra và sửa DB_USER (thường là root), DB_PASSWORD (đã chọn lúc đầu khi cài vps)
bước 5: Okie chạy domain.




Nếu cần URL Decoder/Encoder Truy cập vào đây

Các bước Get Data và Upload vào database

B1. Get data 2018
B2. Xử lý số liệu sau Crawl
- (Cho vào excel) Chỉnh sửa các collume, rows, xóa rows thừa, fix lại post_name. --> Chuyển lại txt.
- Sửa lỗi chính tả nếu có.
- Bỏ hết các special characters
- Spin content 1 phần.

B3. Chuyển data sang sql
Cách 1:- File Excel convert, tìm tab phù hợp có img hay không có img riêng.
- Chỉnh sửa nội dung trong các cột (Thêm trước content, thêm sau content)
Thêm trước và sau content: quảng cáo, ví dụ Tenofovir cho bệnh gan, Thymos...
- Ngày tháng dự định.
- Tạo Categories trong website, tìm mã số categories (vào file /msd xem mã số categories)
- set id cho dễ config sau này
- Excel chuyển sql, categories, tag, random date.
- Copy sang file sql sẵn, định dạng của file msd, navicat (đã test bằng import sql ok rồi, ->ko cần theo định dạng msd.!) navicat (đã test bằng import sql ok rồi, chọn database vd: bitnami_wordpress, rightclik -> Excute Batch File -> Sql file) Làm bằng Navicat là OK...!!
- (có thể thêm) Chuyển qua localhost-> export sagn msd -> sửa xóa phần wp_post delete trong code.
Cách 2:- Collume từ Excel vào Database bằng NAVICAT
- Copy txt sang excel
- Copy collume từ excel sang csv
- dùng wpallimport xử lý.

B4. Backup dữ liệu ở site cũ (để an toàn)
- Import dữ liệu mới vào site.
.
MYSQL CODE .
SELECT * FROM `suckhoeh_nhathuocnghiado`.`wp_posts`where`ID`>'12326'.
DELETE FROM `suckhoeh_nhathuocnghiado`.`wp_posts`where`id`>9001.
============Replace text in mysql============.
UPDATE `wp_posts` SET `post_date` = replace(post_date, '0000-00-00 00:00:00', '2015:10:03 10:00:00').
UPDATE `wp_posts` SET `post_content` = replace(post_content, 'từkhóa', 'replace').
/wp-admin/edit.php?post_type=post&author=7



Loading...