Most efficient way to insert a post outside WordPress?

If you’re on a remote server, XMLRPC would be best, but requires login details

If you’re in a PHP script on the same server, wp-load.php would be best ( XMLRPC will involve a request of sorts )

If you’re in a bash or CLI script, WP CLI would be best, e.g.:

wp post create --post_type=page --post_status=publish --post_title="A future post" --post-status=future --post_date="2020-12-01 07:00:00"

More on WP CLIs create post command

If you’re crazy, a raw SQL insert, fast, cheap, doesnt fire off all the hooks and API calls needed, most incompatible option with plugins, caches, etc

If you’re even crazier, you could write out a WXR file, then run the WordPress Importer

If you’re patient, provide it as an RSS and have WordPress use an aggregation plugin to pull it in

If you’re Sane

Then no external script will be there to begin with, and you will have built a plugin, used the WP AJAX API, not created a dedicated file for a form handler etc.

Unless you’re in some Symfony or Zend setup, your question indicates you’re doing something horribly wrong.

If you are in such a situation though, there are libraries for that, libraries such as:

https://github.com/kayue/KayueWordpressBundle

Amongst others.

But For You achairapart

I would say that wp-load.php is probably the safest and most compatible way that doesn’t require expensive http requests. It’s also how most of the libraries will implement it, it’s also how I would do it.

But keep in mind, inserting/creating a post in WordPress is an inherently expensive thing to do. You can reduce the load but it will never be a fast and quick operation unless you’re running a site that has no plugins or posts and contains only the hello world sample post and page.

Leave a Comment