Create blog post in Wordpress from tinderbox

I played with an interface to use Tinderbox to create posts in Wordpress. Just a first round…

The Wordpress PlugIn:

<?php
/**
 * Plugin Name: Tinderbox REST Plugin
 * Version: 0.0.1
 * Author: Detlef Beyer
 */

// Enqueue scripts or styles if needed

// Register the custom REST API endpoint
add_action('rest_api_init', 'tinderbox_plugin_register_endpoint');

function tinderbox_plugin_register_endpoint() {
    register_rest_route('tinderbox-plugin/v1', '/create-post', array(
        'methods' => 'POST',
        'callback' => 'tinderbox_plugin_create_post',
    ));
}

// Callback function to create the post
function tinderbox_plugin_create_post($request) {
    $params = $request->get_params();

    // Extract data from the REST request (e.g., title, content, etc.)
    $post_title   = sanitize_text_field($params['title']);
    $post_content = wp_kses_post($params['content']);

    // Set the category ID for 'tinderbox'
    $category_id = get_category_by_slug('tinderbox')->term_id;

    // Create post array with the category ID
    $new_post = array(
        'post_title' => $post_title,
        'post_content' => $post_content,
        'post_status' => 'publish',
        'post_type' => 'post',
        'post_category' => array($category_id),
    );

    // Insert the post into the database
    $post_id = wp_insert_post($new_post);

    if ($post_id) {
        // Post created successfully
        return array('status' => 'success', 'post_id' => $post_id);
    } else {
        // Error creating the post
        return array('status' => 'error', 'message' => 'Failed to create post.');
    }
}

and the code you need in Tinderbox:

var:string restURL = "https://api.example.com/tinderbox-plugin/v1";
var:string postContent = $Text;
var:string postTitle = $Name;

-- Create the string to make the REST call
var:string curlCmd   = "curl " + restURL + " \
  			-H 'Content-Type: application/json' \
  			-d '" + "{'title': '" + postTitle + "', 'content': '" + postContent + "', 'status': 'publish'}'";

-- Execute the command
curlRet = runCommand(requestURL);

just as an inspiration…

7 Likes

That is very cool!

Where did you put/call the Tinderbox code?

Hi Oliver,

that’s just a first sketch - should work as it is - but I think there are several parts missing for a final solution. I will collect ideas here and build the final one if there is enough interest :wink:

Right now - to test it:
Create a new directory in the wp-content/plugins/ folder of your WordPress installation and name it “tinderbox_rest”
Inside the new directory, create a PHP file with the same name as the plugin folder: “tinderbox_rest.php” and copy the code into the file. Now you can activate the new plugin in Wordpress. Create a category named “tinderbox” in Wordpress.

create a new stamp in Tinderbox and copy the TBX code to the stamp. That’s it.
Run the stamp on any note of your TBX file and it should appear as a new post in your wordpress install.

P.S.: and you need to enter the url to your Wordpress install followed by “/tinderbox-plugin/v1” into the restURL variable.
P.P.S.: the easiest way to run Wordpress for testing stuff like this is Mamp Pro

1 Like

Thank you very much for these instructions. I will give it a try. I always wanted to be able to publish to WordPress (and also edit previously posted notes). Adding a Tinderbox plugin on the WordPress side seems to be much smarter than trying to go through xmlrpc.php.

I have been trying to find a solution for this for years!!! I VERY much want this. Please keep up the good work. :slight_smile:

Very nice!

I believe the new fetch operator in 9.6 will make this simpler. Just wanted to point out that fetch(), though most often used to GET data, can also POST.

1 Like

This is awesome, but I can’t seem to get it to work. I wonder if my ISP is blocking it. Also, I wonder if there is any additional security that we can pt into this concept to ensure that only authorized people can post to it.

Hi,

it was just the idea of a solution :wink:

Here is a tested and working revision.

Download the PlugIn (ZIP) and install it to your Wordpress installation. You can add it in the Wordpress admin area (PlugIns/Add New).

The tinderbox code for a stamp:

var:string restURL     = "https://yourwordpressserver.com/wp-json/tinderbox-plugin/v1/create-post";
var:string postContent = $Text;
var:string postTitle   = $Name;
var:string curlRet     = "";
var:string curlCmd     = "";
var:string myPayload   = "";

myPayload = '{' + '"title":"' + $Name + '","content":"' + $Text + '","status":"publish"' + '}';

// Create the string to make the REST call
curlCmd = "curl -X POST -s -k " + restURL + " -H 'Content-Type: application/json' -d '" + myPayload  + "'";

// Execute the command
curlRet = runCommand(curlCmd);
if(curlRet.json["status"] == "success") {
	$URL = "https://yourwordpressserver.com" + curlRet.json["post_id"];
};

tinderbox_rest.zip (1.6 KB)

wordpress_demo.tbx (105.0 KB)

1 Like

I removed some older posts here - too many updates…

Here is the latest version of the Tinderbox sample file and the Wordpress PlugIn.

The PlugIn offers three functions to be used with Tinderbox:

  • create a post in Wordpress from any note in TBX
  • update the title and text of the post from TBX
  • delete the post directly out of TBX

To add a basic amount of security there is a parameter called “secret”. Enter any value you like - but take care you use the same string in Wordpress and TBX. In Wordpress there is an admin area for that task:

There you also can set the user the posts should belong to.

Every new post in Wordpress will get the category “Tinderbox”. This may help filtering the posts in Wordpress. And the delete function will only delete posts with this category.

It works fine here for me. You may run into problems if you use a security plugin like Wordfence. They often block any traffic to the REST interface of Wordpress.

Have fun!

tinderbox_rest.zip (6.2 KB)

WordpressAPIDemo.tbx (132.8 KB)

1 Like

Here is an update of the TBX demo file - using the fetch() command of 9.6 (the former version runs with 9.5 and 9.6).
Also now we have a preference note where you enter your URL to the Wordpress host and your secret.

Have fun!
WPApiDemo.tbx (256.6 KB)

2 Likes

I tried this last version…

I get a message back saying something to the effect of “500 internal server error”.

I get this error in my error_log on my wordpress site:

Got error 'PHP message: PHP Fatal error: Uncaught Error: Call to undefined function wp_create_category() in /var/www/vhosts/mysite.com/httpdocs/wp-content/plugins/tinderbox_rest/tinderbox_rest.php:28

Stack trace:

#0 /var/www/vhosts/mysite.com/httpdocs/wp-content/plugins/tinderbox_rest/includes/rest-functions.php(44): tinderbox_rest_plugin_create_tbx_category()

#1 /var/www/vhosts/mysite.com/httpdocs/wp-includes/rest-api/class-wp-rest-server.php(1181): tinderbox_plugin_create_post()

#2 /var/www/vhosts/mysite.com/httpdocs/wp-includes/rest-api/class-wp-rest-server.php(1028): WP_REST_Server->respond_to_request()

#3 /var/www/vhosts/mysite.com/httpdocs/wp-includes/rest-api/class-wp-rest-server.php(442): WP_REST_Server->dispatch()

#4 /var/www/vhosts/mysite.com/httpdocs/wp-includes/rest-api.php(410): WP_REST_Server->serve_request()

#5 /var/www/vhosts/mysite.com/httpdocs/wp-includes/class-wp-hook.php(308): rest_api_loaded…’

Tinderbox is 9.6.0
The Wordpress Plugin version says 0.0.7
The Wordpress Version is 6.2.2

OK. I have been able to workaround this by creating the category “Tinderbox” manually first in the target WordPress site.

It is a good move to check for the existence and trying to create the category as it presents a way to create categories from Tinderbox by adding a “Wordpress_Category” attribute to a note. Somehow the call to wp_create_category() in the tinderbox_rest_plugin_create_tbx_category()-function of your plugin does not work in my environment.

Any idea?

Hi Oliver,

thanks a lot for your log file - I restructured the files of the plugin and placed the wp_create_category() in the wrong place. I will release an update later today!

The category is important - to filter the posts later or, I’m doing this already in the PlugIn, check for the category when deleting an entry.

Best,

Detlef

here is a new version of the plugin with a fix for the wp_create_category() bug (the function got removed from the current WP version).

And I will return to the CURL version of the TBX file. The new fetch() command has a limitation since the length of the called URL is limited. So large notes will not get passed to WP - no problem with CURL. This is not a bug but a feature of fetch().

tinderbox_rest.zip (6.4 KB)

And here is a new version of the demo - with two version of the CreatePostInWP stamp. The one with CURL works fine even with larger notes. The one with fetch only with a limited number of characters.

WPApiDemo.tbx (264.7 KB)

It’s a feature I didn’t know about! How did you learn this?

I tested my Wordpress PlugIn with a TBX file - a note with a long text will not go thru - with CURL it will. POST has no limit itself (as far as I know) but all browser do have one. So it depends on the lib you are using I guess. If I shorten the text the same note will be processed without a problem

  1. RE yesterday’s question, you can use the DELETE method, no problem

  2. We’re using NSURL, which should not have a length limitation (or, rather, the limit is somewhere around a INT_MAX). Might there be an encoding issue?

(1) great!!!
(2) I don’t think it is an encoding problem. I just have to reduce the amount of characters and the fetch() call has no problems. But I don’t get an error message so this is a wild guess only…

the code using fetch():

var:string cleanText = postContent.replace("'","´");

cleanText = cleanText.replace('"','*');

cleanText = cleanText.jsonEncode();

myPayload = "?title=" + urlEncode($Name) + "&content=" + urlEncode(cleanText) + "&status=publish&secret=" + $WordPress_Secret("gWordPressAPI");

// Execute the command

fetch(restURL + myPayload,{Content-Type: application/json},"mySuccess","doProcessWPCallback(mySuccess,1);",POST)

and with CURL (cleanText is identical)

myPayload = '{' + '"title":"' + $Name + '","content":"' + cleanText + '","status":"publish", "secret":"1234"' + '}';

// Create the string to make the REST call
curlCmd = "curl -X POST -s -k " + restURL + " -H 'Content-Type: application/json' -d '" + myPayload  + "'";

// Execute the command
curlRet = runCommand(curlCmd);

and another version of the Tinderbox demo - since DELETE is also an option to fetch() - I change the DeletePostInWP stamp to use fetch too.

WPApiDemo.tbx (263.7 KB)

OK: the problem appears to be encoding after all. Specifically, is “/” allowed in a query? Just at the moment, I have Apple saying "sure!” and Wikipedia saying “absolutely not!”. And another part of the Apple API says “no way!”

Working this…