Action hook 'rest_insert_{$this->post_type}'

in WP Core File wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php at line 980

View Source

rest_insert_{$this->post_type}

Action Hook
Description
Updates a single post. / public function update_item( $request ) { $valid_check = $this->get_post( $request['id'] ); if ( is_wp_error( $valid_check ) ) { return $valid_check; } $post_before = get_post( $request['id'] ); $post = $this->prepare_item_for_database( $request ); if ( is_wp_error( $post ) ) { return $post; } if ( ! empty( $post->post_status ) ) { $post_status = $post->post_status; } else { $post_status = $post_before->post_status; } /* `wp_unique_post_slug()` returns the same slug for 'draft' or 'pending' posts. To ensure that a unique slug is generated, pass the post data with the 'publish' status.

Hook Information

File Location wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php View on GitHub
Hook Type Action
Line Number 980

Hook Parameters

Type Name Description
WP_REST_Request $request Full details about the request.

Usage Examples

Basic Usage
<?php
// Hook into rest_insert_{$this->post_type}
add_action('rest_insert_{$this->post_type}', 'my_custom_function', 10, 1);

function my_custom_function($request) {
    // Your custom code here
}

Source Code Context

wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php:980 - How this hook is used in WordPress core
<?php
 975  		}
 976  
 977  		$post = get_post( $post_id );
 978  
 979  		/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
 980  		do_action( "rest_insert_{$this->post_type}", $post, $request, false );
 981  
 982  		$schema = $this->get_item_schema();
 983  
 984  		if ( ! empty( $schema['properties']['format'] ) && ! empty( $request['format'] ) ) {
 985  			set_post_format( $post, $request['format'] );

PHP Documentation

<?php
/**
	 * Updates a single post.
	 *
	 * @since 4.7.0
	 *
	 * @param WP_REST_Request $request Full details about the request.
	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
	 */
	public function update_item( $request ) {
		$valid_check = $this->get_post( $request['id'] );
		if ( is_wp_error( $valid_check ) ) {
			return $valid_check;
		}

		$post_before = get_post( $request['id'] );
		$post        = $this->prepare_item_for_database( $request );

		if ( is_wp_error( $post ) ) {
			return $post;
		}

		if ( ! empty( $post->post_status ) ) {
			$post_status = $post->post_status;
		} else {
			$post_status = $post_before->post_status;
		}

		/*
		 * `wp_unique_post_slug()` returns the same slug for 'draft' or 'pending' posts.
		 *
		 * To ensure that a unique slug is generated, pass the post data with the 'publish' status.
		 */
Quick Info
  • Hook Type: Action
  • Parameters: 1
  • File: wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
Related Hooks

Related hooks will be displayed here in future updates.