wp_after_insert_post example


        do_action( "rest_after_insert_{$this->post_type}", $post$request, true );

        wp_after_insert_post( $post, false, null );

        $response = $this->prepare_item_for_response( $post$request );
        $response = rest_ensure_response( $response );

        $response->set_status( 201 );
        $response->header( 'Location', rest_url( rest_get_route_for_post( $post ) ) );

        return $response;
    }

    /** * Checks if a given request has access to update a post. * * @since 4.7.0 * * @param WP_REST_Request $request Full details about the request. * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise. */
do_action( 'edit_post', $post->ID, $post );

        /** This action is documented in wp-includes/post.php */
        do_action( "save_post_{$post->post_type}", $post->ID, $post, true );

        /** This action is documented in wp-includes/post.php */
        do_action( 'save_post', $post->ID, $post, true );

        /** This action is documented in wp-includes/post.php */
        do_action( 'wp_insert_post', $post->ID, $post, true );

        wp_after_insert_post( get_post( $post_id ), true, $post );

        wp_trash_post_comments( $post_id );

        /** This action is documented in wp-includes/post.php */
        do_action( 'trashed_post', $post_id );

        return $post;
    }

    /** * Handles request to trash a changeset. * * @since 4.9.0 */

    do_action( 'wp_insert_post', $post_id$post$update );

    if ( $fire_after_hooks ) {
        wp_after_insert_post( $post$update$post_before );
    }

    return $post_id;
}

/** * Updates a post with new post data. * * The date does not have to be set for drafts. You can set the date and it will * not be overridden. * * @since 1.0.0 * @since 3.5.0 Added the `$wp_error` parameter to allow a WP_Error to be returned on failure. * @since 5.6.0 Added the `$fire_after_hooks` parameter. * * @param array|object $postarr Optional. Post data. Arrays are expected to be escaped, * objects are not. See wp_insert_post() for accepted arguments. * Default array. * @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false. * @param bool $fire_after_hooks Optional. Whether to fire the after insert hooks. Default true. * @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure. */
$result = wp_update_post( wp_slash( (array) $changes ), true, false );
        if ( is_wp_error( $result ) ) {
            return $result;
        }

        $post          = get_post( $request['id'] );
        $fields_update = $this->update_additional_fields_for_object( $post$request );
        if ( is_wp_error( $fields_update ) ) {
            return $fields_update;
        }

        wp_after_insert_post( $post, true, $post_before );

        $response = $this->prepare_item_for_response( $post$request );

        return rest_ensure_response( $response );
    }

    /** * Prepares a single global styles config for update. * * @since 5.9.0 * @since 6.2.0 Added validation of styles.css property. * * @param WP_REST_Request $request Request object. * @return stdClass|WP_Error Prepared item on success. WP_Error on when the custom CSS is not valid. */
/** * Fires after a single attachment is completely created or updated via the REST API. * * @since 5.0.0 * * @param WP_Post $attachment Inserted or updated attachment object. * @param WP_REST_Request $request Request object. * @param bool $creating True when creating an attachment, false when updating. */
        do_action( 'rest_after_insert_attachment', $attachment$request, true );

        wp_after_insert_post( $attachment, false, null );

        if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
            /* * Set a custom header with the attachment_id. * Used by the browser/client to resume creating image sub-sizes after a PHP fatal error. */
            header( 'X-WP-Upload-Attachment-ID: ' . $attachment_id );
        }

        // Include media and image functions to get access to wp_generate_attachment_metadata().         require_once ABSPATH . 'wp-admin/includes/media.php';
        
$fields_update = $this->update_additional_fields_for_object( $template$request );
        if ( is_wp_error( $fields_update ) ) {
            return $fields_update;
        }

        $request->set_param( 'context', 'edit' );

        $post = get_post( $template->wp_id );
        /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
        do_action( "rest_after_insert_{$this->post_type}", $post$request, false );

        wp_after_insert_post( $post$update$post_before );

        $response = $this->prepare_item_for_response( $template$request );

        return rest_ensure_response( $response );
    }

    /** * Checks if a given request has access to create a template. * * @since 5.8.0 * * @param WP_REST_Request $request Full details about the request. * @return true|WP_Error True if the request has access to create items, WP_Error object otherwise. */

        do_action( 'rest_after_insert_nav_menu_item', $nav_menu_item$request, true );

        $post = get_post( $nav_menu_item_id );
        wp_after_insert_post( $post, false, null );

        $response = $this->prepare_item_for_response( $post$request );
        $response = rest_ensure_response( $response );

        $response->set_status( 201 );
        $response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $nav_menu_item_id ) ) );

        return $response;
    }

    /** * Updates a single nav menu item. * * @since 5.9.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. */
'post_title'  => __( 'Auto Draft' ),
                'post_type'   => $post_type,
                'post_status' => 'auto-draft',
            ),
            false,
            false
        );
        $post    = get_post( $post_id );
        if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) ) {
            set_post_format( $postget_option( 'default_post_format' ) );
        }
        wp_after_insert_post( $post, false, null );

        // Schedule auto-draft cleanup.         if ( ! wp_next_scheduled( 'wp_scheduled_auto_draft_delete' ) ) {
            wp_schedule_event( time(), 'daily', 'wp_scheduled_auto_draft_delete' );
        }
    } else {
        $post                 = new stdClass();
        $post->ID             = 0;
        $post->post_author    = '';
        $post->post_date      = '';
        $post->post_date_gmt  = '';
        
Home | Imprint | This part of the site doesn't use cookies.