Table of Contents
I have accounts on plenty of microblogging sites (Twitter, Mastodon, Bluesky and even Threads) but they are all owned and controlled by someone else. I decided that I wanted something that I could use and manage, and a quick search came up with Michael Gbadebo's WordPress plugin called Microposts.
I installed this and after playing around for a while, I decided that it was just what I was looking for. However, it was quite cumbersome to make a post from a mobile device when I was out and about. What I wanted was a simple page from which I could post including an image so I built this connector.
Very much inspired by my php2Micropost project this package will allow you to easily integrate creating Microposts from your own code.
Running the script is very straightforward:
-
install composer
-
add php2Micropost
composer.phar require williamsdb/php2micropost
You can read more about how this all works in these blog posts.
Requirements are very simple; it requires the following:
- PHP (I tested on v8.1.13)
- WordPress (I tested on 6.9.4)
- Michael Gbadebo's Micropost plugin
- a WordPress blog and an Application Password (see this post for details of how to do that).
- As above
Here's a few examples to get you started.
require __DIR__ . '/vendor/autoload.php';
use williamsdb\php2micropost\php2Micropost;
$base_url = 'https://www.your-domain.com/wp-json/wp/v2';
$username = 'WordPress username with write permissions';
$password = 'Your 24-character Application Password';
$parseURL = false;
$php2Micropost = new php2Micropost(
base_url: $base_url,
username: $username,
password: $password,
parseUrls: false,
);
$connection = $php2Micropost->wordpress_connect();Text sent can either be plain text or HTML. If you send plain text and $parseURLs is TRUE then the text will be checked for URLs and if any found they will be wrapped in <a href="URL">URL</a>. If you are passing HTML then $parseURLs should be set to FALSE to retain the HTML.
$text = "This is a test post from php2Micropost. " . date('Y-m-d H:i:s');
// post with text and nothing else
$response = $php2Micropost->post_to_wordpress(
connection: $connection,
text: $text,
title: '',
media: '',
);Microposts only accepts a single image so that's all that is allowed here.
$image = "/Users/neilthompson/Downloads/IMG_9547.jpeg";
$text = "This is a test post from php2Micropost. " . date('Y-m-d H:i:s');
// post with text and nothing else
$response = $php2Micropost->post_to_wordpress(
connection: $connection,
text: $text,
title: '',
media: $image,
);If the tag already exists, it will be associated with the post; otherwise, it will be created.
$tags = ['test', 'php2Micropost', 'reading'];
$text = "This is a test post from php2Micropost. " . date('Y-m-d H:i:s');
// post with text and nothing else
$response = $php2Micropost->post_to_wordpress(
connection: $connection,
text: $text,
title: '',
media: $image,
tags: $tags,
);See the open issues for a full list of proposed features (and known issues).
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Distributed under the GNU General Public License v3.0. See LICENSE for more information.
Bluesky - @spokenlikeageek.com
Mastodon - @spokenlikeageek
X - @spokenlikeageek
Website - https://spokenlikeageek.com
Project link - Github