WordPress generates a two-dimensional code tutorial for the article

Source: Internet
Author: User
Tags fread

The simplest way to invoke this is to call Google directly.

The code is as follows Copy Code

"width=" "height=" alt= "QR Code"/>

Just change the size of the 150 image in the above code,

The code is as follows Copy Code

<?php The_permalink ();?>

is to automatically get the article link, if you want to generate a blog site two-dimensional code as long as the replacement of the blog domain name fixed links can be.

I have changed, encapsulated into a function, so that the size of the picture can be changed, but in fact, it is not convenient to use, because the theme of the function must be written dead, unless combined with the background to set two-dimensional code picture size, it is not very useful.


The above method is directly through the Google API to generate pictures, some places may be due to network problems, will be very slow, so the small butterfly students in the Hu Changmong request, and do a local cache image processing

First, add code in functions.php

The code is as follows Copy Code

Function Get_qr ($url, $path, $qrpic) {    

 set_time_limit (10);//Set 10 sec timeout
  $destination _folder = $path $path. ' /':'';
  $localname = $destination _folder. $qrpic;    
  $file = fopen ($url, "RB");//fop r+ mode of the EN function: Read-write method open file pointer to file header
 if ($file) {    
   $NEWF = fopen ($localname, "W B "); w+, read-write method open file pointer to file header if the file does not exist then try to create the
  if ($NEWF)     
   while ( !feof ($file)) {    
    fwrite ($NEWF, Fread ($file, 1024 * 2), 1024 * 2); /write file, fread control file maximum size, here is 2M
   }    
 }    
 if ($file) {    
  fclose ($file); //close fopen open files   
 }    
 if ($NEWF) {    
  fclose ($NEWF);     
 }    

}

Second, create a new folder called QRCode in the root directory of the Web site to ensure write access

The following code is placed where you need to output a two-dimensional code picture, such as single.php

The code is as follows Copy Code
<?php
$LOCALQR = Abspath. ' qrcode/'. get_the_id (). JPG ';
if (!file_exists ($LOCALQR)) {//If the picture already exists, it will not be saved again
GET_QR ("http://chart.googleapis.com/chart?cht=qr&chs=100x100&choe=UTF-8&chld=L|4&chl=". get_ Permalink (), "QRCode", get_the_id (). JPG ");//The picture name is named after the article ID
}
?>
/qrcode/<?php the_id () >.jpg "width=" "height=" alt= "QR Code"/& Gt

<!--The default two-dimensional code picture size is 100*100, if you want to change, need to change 4 places, has been marked with blue;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.