Most efficient way to insert a post outside WordPress?

I need to insert a post via PHP outside the WordPress environment (but on the same server) and I’m looking for the most efficient way to do this.

I was thinking about 2 solutions:

Read More

1) Using XML-RPC, as explained here

2) Load the WordPress core requiring wp-load.php and then using wp_insert_post()

Wich one uses less resources? I just need to insert a new post into the database and I don’t need any support for plugins, theming, etc. There is a more hackish way to do this?

Thank you!

Related posts

4 comments

  1. 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.

  2. Which one uses less resources?

    Neither, really.

    I don’t need any support for plugins, theming, etc.

    I think you’ve answered your own question. Go for the latter technique (wp-load.php).

  3. There is a more hackish way to do this?

    According to Wiktionary hackish means:

    poorly designed workarounds

    You could do it directly via SQL or even generate some .sql file and import it directly to your database 😉

    This path will probably be unflexible and even muddy, but hackish yes 😉

    But in general I would go with number 2).

  4. As you are on the same server, why not do a custom sql quary, I think it will use less server resource than loading core file using wp-load.php

Comments are closed.