Skip to content

williamsdb/php2Micropost

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


php2Micropost

A simple library that allows posting to WordPress Microposts via the API.

Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. Roadmap
  5. Contributing
  6. License
  7. Contact
  8. Acknowledgments

About The Project

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.

Buy Me a Coffee at ko-fi.com

(back to top)

Built With

(back to top)

Getting Started

Running the script is very straightforward:

  1. install composer

  2. add php2Micropost

composer.phar require williamsdb/php2micropost

You can read more about how this all works in these blog posts.

Prerequisites

Requirements are very simple; it requires the following:

  1. PHP (I tested on v8.1.13)
  2. WordPress (I tested on 6.9.4)
  3. Michael Gbadebo's Micropost plugin
  4. a WordPress blog and an Application Password (see this post for details of how to do that).

Installation

  1. As above

(back to top)

Usage

Here's a few examples to get you started.

Setup and connect to WordPress

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();

Sending post with only text

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: '',
);

Uploading a post with a single image

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,
);

Adding tags to a post

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,
);

(back to top)

Known Issues

See the open issues for a full list of proposed features (and known issues).

(back to top)

Contributing

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!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

(back to top)

License

Distributed under the GNU General Public License v3.0. See LICENSE for more information.

(back to top)

Contact

Bluesky - @spokenlikeageek.com

Mastodon - @spokenlikeageek

X - @spokenlikeageek

Website - https://spokenlikeageek.com

Project link - Github

(back to top)

Acknowledgments

(back to top)

About

A simple library that allows posting to WordPress Microposts via the API

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages