1

I'm building a plugin that will call the wp_insert_post() function. I need to know if it is functionally identical to posting via the UI. For example, would other plugins (I'm thinking particularly of JetPack) share to social media or are there additional steps to replicate manually publishing (or scheduling) a post?

3
  • 2
    Probably not. Have you checked WP source what happens on publishing? I'd assume a couple of actions are probably firing that are not inside wp_insert_post(). (But that is only an assumption)
    – kero
    Jan 15, 2021 at 17:54
  • @kero that was what I was worried about. I don't much fancy crawling around inside JetPack or similar to find out what they hook. Jan 15, 2021 at 17:57
  • 2
    Actually, wp_insert_post() does do the actions hooked on save_post and post_updated. So the answer is yes, although you will need to specify post_data like publish date, sanitize post_name if necessary, post_meta etc if you want something other than the defaults (and the $post->ID when updating, otherwise you'll create new posts every time).
    – tdj
    Jan 15, 2021 at 18:04

1 Answer 1

2

There's a bit more to the answer than just wp_insert_post(). That is the canonical way to insert a post and what the Classic Editor used to submit all of the post information at once.

The REST API (and thus Gutenberg) changes it a tad but running wp_insert_post initially just to setup a post in the db, then separately adds all of the meta data and taxonomies, etc (see https://core.trac.wordpress.org/browser/trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php?rev=49172#L577 ).

In terms of Jetpack's Publicize feature, wp_insert_post will work just fine. We look for the wp_insert_post hook to sync up the post data used for the social media share ( https://github.com/Automattic/jetpack/blob/master/projects/packages/sync/src/modules/class-posts.php#L118 ).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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