Pushing WordPress Blog Articles to WeChat Subscribers Using PushBear

pushbearwordpress blog pushwechat subscriptionblog notificationapi integration
Published·Modified·

PushBear is a one-to-many message delivery service provided by Fangtang Technology. It allows you to send group messages to WeChat subscribers directly via the PushBear API without applying for an official WeChat account, such as pushing blog articles.

PushBear Interface

Below, we will use a WordPress blog as an example. You only need to add simple code to automatically push blog articles.

Register PushBear and Set Up Channels

Messages must be sent through a channel. Different channels can send messages to different groups of people (bound via QR codes). Next, create a new channel.

Create Channel

After the channel is created successfully, a unique QR code will be generated. WeChat users only need to scan it to subscribe, allowing you to send group messages to subscribers.

QR Code Subscription

Channel Details

Add Code

Although PushBear provides a testing tool to send group messages directly, it is inconvenient. You can encapsulate the PushBear API again to implement automatic message sending. Save the following code to the root directory of your site and name it sendwx.php.

<?php
	/*
	Author: xiaoz.me
	Last Updated: 2018-03-27
	*/
	header("Content-Type: text/html; charset=UTF-8");
	error_reporting(E_ALL^E_NOTICE^E_WARNING^E_DEPRECATED);
	$title = $_POST['title'];
	$content = $_POST['content'];
	$imgurl = $_POST['imgurl'];
	$url = $_POST['url'];

	$data = array(
		"title"		=>	$title,
		"content"	=>	$content,
		"imgurl"	=>	$imgurl,
		"url"		=>	$url
	);

	// Set password
	$password = "xiaoz.me";
	// Initialize
	$pw = $_GET['pw'];
	// Set COOKIE initialization
	if((isset($pw)) && ($pw == $password)) {
		echo 'Initialization successful!';
		setcookie("wxrss",$pw, time()+3600*24*30,"/");
	}
	// If cookie does not exist
	if(!isset($_COOKIE['wxrss'])){
		$redata = array("code" => "-1","data" => "Push failed, please initialize first!");
		echo json_encode($redata);
		exit;
	}
	else {
		$wxrss = $_COOKIE['wxrss'];
		// If cookie exists and is correct, send message
		if($wxrss == $password) {
			$send = new Send;
			$send->key = 'Fill in PushBear SendKey';	
			$send->wxrss($data);
		}
		else{
			$redata = array("code" => "-1","data" => "Push failed, please initialize first!");
			echo json_encode($redata);
			exit;
		}
	}

	

	class Send{
		var $key;
		function wxrss($data) {
			$url = $data['url'];
			$imgurl = $data['imgurl'];
			$key = $this->key;
			$text = urlencode($data['title']);
			$content = $data['content']."\n";
			//$content = urlencode($content)."\n";
			$content = $content."![]($imgurl) \n";
			$content = $content."Read full article:"."[$url]($url)";
			$content = urlencode($content);
		
			$re = file_get_contents("https://pushbear.ftqq.com/sub?sendkey=".$key."&text=".$text."&desp=".$content);
			echo $re;
		}
	}
	
?>

In the code above, please fill in your own password and the correct PushBear SendKey, then visit http://domain.com/sendwx.php?pw=password to initialize. (Normally, you will see the following screenshot)

Initialization Success

Continue adding the following JavaScript code to the appropriate location in the WordPress theme, usually footer.php.

<script>
$(document).keydown(function(event){
  if(event.keyCode == 77){
    sendwx();
  }
  });
function sendwx(){
  var title = document.title;
  var url = window.location.href;
  var content = $("#content p:first").text();
  var imgurl = $("#content img")[0].src;

  var apiurl = window.location.protocol + "//" + window.location.host + "/sendwx.php";
  //alert(url);

  

  $.post(apiurl,{title:title,url:url,content:content,imgurl:imgurl},function(data,status){
    var obj = eval('(' + data + ')');
    // If successful
    if(obj.code == 0) {
      alert("Push successful!");
    }
    else{
      alert(obj.data);
    }
  });
}
</script>

Modify content to the div id of your article content, which can be viewed using the F12 tool.

Inspect Element

If the settings are correct, entering the shortcut key Ctrl + M on the WordPress article page will automatically send the first paragraph of the current page article and the first image to WeChat subscribers. The effect is as follows.

Push Result

Note: If the article content has no images, it cannot be sent. After initializing sendwx.php, COOKIES will be saved for 30 days. If it exceeds 30 days or the COOKIES are lost, please visit sendwx.php again to initialize. If the push does not work after adding the code, please use the F12 developer tool to analyze.

Summary

The benefit of PushBear is that you can send group messages without applying for an official WeChat account, and it is completely free. Therefore, please do not use it to send spam messages. Although WordPress has many email subscription plugins, they consume more resources, and checking emails frequently is obviously not as convenient as WeChat push notifications. You might as well try PushBear.

Finally, you can scan the QR code below with WeChat to receive timely reminders about Xiaoz Blog updates and various promotional activities.

WeChat QR Code