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?
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?
1 Answer
Reset to default 2There'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/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/Automattic/jetpack/blob/master/projects/packages/sync/src/modules/class-posts.php#L118 ).
wp_insert_post()
. (But that is only an assumption) – kero Commented Jan 15, 2021 at 17:54wp_insert_post()
does do the actions hooked onsave_post
andpost_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 Commented Jan 15, 2021 at 18:04