admin管理员组

文章数量:1303670

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?

Share Improve this question asked Jan 15, 2021 at 17:50 Matthew Brown aka Lord MattMatthew Brown aka Lord Matt 1,0683 gold badges13 silver badges34 bronze badges 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 Commented 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. – Matthew Brown aka Lord Matt Commented 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 Commented Jan 15, 2021 at 18:04
Add a comment  | 

1 Answer 1

Reset to default 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/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 ).

本文标签: Is wpinsertpost exactly the same as publishing a post through the core UI