wordpress页面生成二维码

二维码 的生成方便移动端访问,特别当你的主题是自适应主题的时候,有时候我们可以在侧边栏工具上放置一个生成二维码的功能,让我们的主题很炫,很实用,鲜明分享一些常用的jQuery

jquery.qrcode.min.js插件方法
在wordpress头部文件引入jquery.qrcode.min.js插件,官网地址:https://github.com/jeromeetienne/jquery-qrcode,如果速度慢的话,这里提供百度网盘分享:http://pan.baidu.com/s/1boSBUTD
注意:需要jQuery文件支持,你的wp已经引入了jquery文件。
例如这样的结构引入顺序。wp正确的引入方法建议阅读:WordPress引入css/js两种方法

<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="jquery.qrcode.min.js"></script>

(1)、在侧边栏文件或者需要显示二维码的地方调用二维码:如下:

<div id="qrcode">

(2)、调用qrcode插件。

qrcode可以支持canvas和table两种方式渲染图片,默认canvas。一般canvas渲染效果比table要好,区别可以试一下。

调用代码如下:

$('#qrcode').qrcode("511遇见!");//任意字符串当然这里是我们的WP网站的每个页面地址
也可以这样:

$('#qrcode').qrcode({
            render:"table",//渲染方式
            height:100,
            width:100,
            text:"这里是你的页面url"
//      background:"yellow" ,//背景颜色
//      foreground:"red"//前景颜色
 });

其他属性:边框留白大小quietZoneSize,定义图标logo,correctLevel(容错级别,支持L,M,H)Low/Middle/High等等
(3)、支持中文的解决方案
query-qrcode这个库是采用 charCodeAt() 这个方式进行编码转换的,默认不支持中文,
默认会获取它的 Unicode 编码,一般的解码器都是采用UTF-8, ISO-8859-1等方式,
英文是没有问题,如果是中文,一般情况下Unicode是UTF-16实现,长度2位,而UTF-8编码是3位,这样二维码的编解码就不匹配了。
解决方式当然是,在二维码编码前把字符串转换成UTF-8:

function utf16to8(str) {
        var out, i, len, c;
        out = "";
        len = str.length;
        for (i = 0; i < len; i++) {
            c = str.charCodeAt(i);
            if ((c >= 0x0001) && (c <= 0x007F)) {
                out += str.charAt(i);
            } else if (c > 0x07FF) {
                out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
                out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
                out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
            } else {
                out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
                out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
            }
        }
        return out;
    }

(4)、下面是一个来自网络的示例,(版权属于作者)对你有所帮助:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>生成二维码</title>
    <script type="text/javascript" src="jquery-1.7.2.min.js"></script>
    <script type="text/javascript" src="jquery.qrcode.min.js"></script>
</head>
<script type="text/javascript">
//转字符码
    function utf16to8(str) {
        var out, i, len, c;
        out = "";
        len = str.length;
        for (i = 0; i < len; i++) {
            c = str.charCodeAt(i);
            if ((c >= 0x0001) && (c <= 0x007F)) {
                out += str.charAt(i);
            } else if (c > 0x07FF) {
                out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
                out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
                out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
            } else {
                out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
                out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
            }
        }
        return out;
    }
    function qrcode(){       
        //生成二维码
        $('#output').qrcode({
            height:100,
            width:100,
            text:utf16to8($('#text'.html))
//      background:"yellow" ,//背景颜色
//      foreground:"red"//前景颜色
 });
    }
</script>
<body>
<div id="headName" style="border-bottom:solid 1px #F9F3DD;width:100%;line-height: 60px;font-weight: bold;">
    <div style="padding-left: 20px;width: 42%;float: left;">
        <span style="color: #0099FF; font-size: 20px">
            qrcode.
            <a target="_blank" style="color: #B266B2;text-decoration: none;" href="http://easy521.com/">easy521.com</a>
        </span>
    </div>
    <div style="width: 58%;float: none;">
        <button onclick="qrcode()" style="text-align: center;font-weight: bold;border-radius: 5px;font-size:14px;color: #CC3366 ">生成二维码</button>
        <p dir="rtl" style="color: #666666;">提供者:刘谱</p>
    </div>
    <div style="width: 100%;">
        <div style="width: 42%; float: left;">
            <textarea id="text" class="form-control" style="width: 93%; padding: 20px; border-top: 1px solid rgb(221, 221, 221);border-bottom: 1px solid rgb(221, 221, 221);
            resize: none;
             font-size: 14px; height: 243px;" placeholder="请在此输入文本信息">Hello Word</textarea>
        </div>
        <div style="width: 58%; float: right;">
            <div id="output" style="border-bottom: 1px solid rgb(221, 221, 221); border-left: 1px solid rgb(221, 221, 221);border-top: 1px solid rgb(221, 221, 221); padding: 20px; overflow: auto; height: 243px;"></div>
        </div>
        <br style="clear:both;">
    </div>
    <div class="form-control" align="right" style="width: 100%; color:#CCC; padding-right: 20px;"> 最后更新时间:2015.08.20 </div>
</div>
</body>
</html>

原文参考:wordpress页面生成二维码

猜你喜欢

转载自blog.csdn.net/zcp528/article/details/107996469