Home > CMS Tutorial > WordPress > body text

How to add WeChat public account function to WordPress plug-in

WBOY
Release: 2023-09-05 09:12:24
Original
1209 people have browsed it

How to add WeChat public account function to WordPress plug-in

How to add WeChat public account function to WordPress plug-in

With the rapid development of mobile Internet, WeChat public account has become one of the important tools for marketing and promotion of many enterprises and individuals. one. For users who use WordPress as a website building platform, how to add the WeChat public account function to their plug-ins has become a common requirement. This article will teach you how to add the WeChat public account function to the WordPress plug-in and provide corresponding code examples.

1. Register a WeChat public account developer account

Before you start, you need to register a WeChat public account developer account. Open the official website of the WeChat public platform (https://mp.weixin.qq.com/), click the registration button, fill in the relevant information according to the guidelines and complete the account registration.

2. Obtain the WeChat public account developer certificate

After successful registration, enter the WeChat public account platform developer center and click "Developer Tools" in the left menu to enter. On the "Official Account Settings" page, find the two important credentials "Developer ID" and "Developer Password".

3. Install WordPress plug-in

Install a plug-in that can use custom code functions on your WordPress website, such as the Custom JavaScript plug-in. After installation, go to the "Settings" menu to find the plug-in and click "New Code Snippet".

4. Add the WeChat public account interface code

On the "New Code Snippet" page, enter the code in Code Example 1. Among them, you need to replace [APPID] with the developer ID of your WeChat official account, and replace [APPSECRET] with the developer password of your WeChat official account.

Code Example 1:

function get_wechat_access_token() {
    $appid = '【APPID】';
    $appsecret = '【APPSECRET】';
    $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret;
    $response = wp_remote_get( $url );
    $body = wp_remote_retrieve_body( $response );
    $data = json_decode( $body );
    return $data->access_token;
}

function get_wechat_qrcode() {
    $access_token = get_wechat_access_token();
    $url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$access_token;
    $body = '{
        "action_name": "QR_LIMIT_STR_SCENE",
        "action_info": {
            "scene": {
                "scene_str": "wechat"
            }
        }
    }';
    $response = wp_remote_post( $url, array(
        'body' => $body
    ) );
    $body = wp_remote_retrieve_body( $response );
    $data = json_decode( $body );
    $ticket = $data->ticket;
    $qrcode_url = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.urlencode( $ticket );
    return $qrcode_url;
}
Copy after login

The two functions in Code Example 1 are used to obtain the access_token of the WeChat official account and to generate the permanent QR code link of the WeChat official account. You can modify the code according to actual needs.

5. Call the WeChat official account interface

In the page or article where the WeChat official account QR code needs to be displayed, use the following code to call the WeChat official account interface and display the QR code.

Code example two:

<img  src="<?php echo get_wechat_qrcode(); ? alt="How to add WeChat public account function to WordPress plug-in" >" alt="微信公众号二维码">
Copy after login

Insert the above code into the position where you need to display the WeChat official account QR code.

Summary:

Through the code examples provided in this article, you can easily add the WeChat public account function to your WordPress plug-in. Of course, if you have a certain understanding of PHP and WordPress development, you can also expand and optimize the code according to actual needs. I wish you good results in your WordPress plugin development!

The above is the detailed content of How to add WeChat public account function to WordPress plug-in. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!