Skip to content

Instantly share code, notes, and snippets.

@froger-me
Last active January 19, 2020 04:04
Show Gist options
  • Save froger-me/c918d7f2c4f88eff1b330a50d8962f23 to your computer and use it in GitHub Desktop.
Save froger-me/c918d7f2c4f88eff1b330a50d8962f23 to your computer and use it in GitHub Desktop.
WP Weixin Crossdomain Nexus - Customize the blog ID used for WeChat API authentication calls when using a subdomain-based multisite network
<?php
/*
Plugin Name: WP Weixin Crossdomain Nexus
Plugin URI: https://gist.github.com/froger-me/c918d7f2c4f88eff1b330a50d8962f23
Description: Requires WP Weixin. Customize the blog IDs used for WeChat API authentication and WeChat Pay transactions when using a domain/subdomain-based multisite network. NEEDS TO BE EDITED BY A DEVELOPER BEFORE USE!
Version: 1.3.5
Author: Alexandre Froger
Author URI: https://froger.me
WC tested up to: 3.8.1
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/* ============================================================================ *
* Complex Example *
* *
* Multiple Official Accounts *
* ============================================================================ */
// 2 WeChat Official Accounts (OA):
// - the first one uses blog with ID 1 for both authentication and payment
// - the second one uses blog with ID 2 for both authentication and payment
// - the WeChat App IDs used to determine which OA to use depending on the current
// blog ID are hard coded in this example, but they could come from
// get_site_option() if the developer building on this example creates a setting page.
// - it is assumed the ID of the blog used for authentication before this plugin was
// activated was the default blog with ID 1 (therefore no usermeta table update is
// necessary, all existing users are users of the first OA).
function wpwxdn_wechat_set_id( $blog_id ) {
$official_account_appid_1 = 'EDIT ME !!!'; // The WeChat App ID of the first OA
$official_account_appid_2 = 'EDIT ME !!!'; // The WeChat App ID of the second OA
$current_official_account_appid = wp_weixin_get_option( 'appid' );
if ( $current_official_account_appid === $official_account_appid_1 ) {
$blog_id = 1;
}
if ( $current_official_account_appid === $official_account_appid_2 ) {
$blog_id = 2;
}
return $blog_id;
}
add_filter( 'wp_weixin_ms_auth_blog_id', 'wpwxdn_wechat_set_id', 10, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment