2

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:

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!

5
  • The answer has already been given, so don't take this as a rep-steal, but for those who don't know how to do this, I have a blog article explaining how. Commented Mar 10, 2014 at 18:22
  • what is the PHP code you're running? Custom PHP files? A plugin or themes AJAX handler? A drupal install? A symfony/zend/laravel applicaiton?
    – Tom J Nowell
    Commented Mar 11, 2014 at 10:35
  • @TomJNowell a custom PHP application that programmately insert new posts into WordPress. Commented Mar 11, 2014 at 11:10
  • what framework does it use?
    – Tom J Nowell
    Commented Mar 11, 2014 at 14:48
  • @TomJNowell no PHP frameworks involved at all! Commented Mar 11, 2014 at 23:06

4 Answers 4

10
+50

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.

3
  • +1 for recap all the options available. However, I think I'm enough sane to looking for a way to minimize all the resources I can even if I'm not using any PHP framework. Commented Mar 11, 2014 at 23:26
  • I would still look into symfony bundles, and other libraries, you don't have to buy into the framework to use the nice parts
    – Tom J Nowell
    Commented Mar 11, 2014 at 23:33
  • @achairapart note my edits
    – Tom J Nowell
    Commented Mar 12, 2014 at 17:12
3

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

1
  • 1
    One involves a http request and login details, the other doesn't, they're not equivalant
    – Tom J Nowell
    Commented Mar 10, 2014 at 18:58
2

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

0

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

2
  • 1
    Yes, this is the crazy option one (quoting @TomJNowell), but it actually will miss very important things like sanitizing inputs, future-proof compatibility, etc. Commented Mar 11, 2014 at 23:15
  • My bad, I did not think this way.
    – Sabbir H
    Commented Mar 12, 2014 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.