/* |-------------------------------------------------------------------------- | Create Main WordPress Post Endpoint |-------------------------------------------------------------------------- | | POST: | https://presalemaster.com/wp-json/custom/v8/post | | Supports: | - title | - content | - excerpt | - status | - slug | - author | - categories | - tags | - featured_media | - featured_image_url | - meta | |-------------------------------------------------------------------------- */ add_action('rest_api_init', function () { register_rest_route( 'custom/v8', '/post', array( 'methods' => 'POST', 'callback' => 'custom_create_main_post', 'permission_callback' => function () { /* |-------------------------------------------------------------------------- | User must be logged in |-------------------------------------------------------------------------- */ if (!is_user_logged_in()) { return new WP_Error( 'rest_forbidden', 'You must be logged in to create a post.', array( 'status' => 401 ) ); } /* |-------------------------------------------------------------------------- | User must have permission to create posts |-------------------------------------------------------------------------- */ if (!current_user_can('edit_posts')) { return new WP_Error( 'rest_forbidden', 'You do not have permission to create posts.', array( 'status' => 403 ) ); } return true; }, ) ); }); /* |-------------------------------------------------------------------------- | Create Main WordPress Post |-------------------------------------------------------------------------- */ function custom_create_main_post(WP_REST_Request $request) { /* |-------------------------------------------------------------------------- | GET JSON DATA |-------------------------------------------------------------------------- */ $data = $request->get_json_params(); /* |-------------------------------------------------------------------------- | VALIDATE DATA |-------------------------------------------------------------------------- */ if (empty($data) || !is_array($data)) { return new WP_Error( 'invalid_data', 'Invalid JSON data.', array( 'status' => 400 ) ); } /* |-------------------------------------------------------------------------- | BASIC POST DATA |-------------------------------------------------------------------------- */ $title = isset($data['title']) ? sanitize_text_field($data['title']) : ''; $content = isset($data['content']) ? wp_kses_post($data['content']) : ''; $excerpt = isset($data['excerpt']) ? wp_kses_post($data['excerpt']) : ''; $status = isset($data['status']) ? sanitize_key($data['status']) : 'draft'; $slug = isset($data['slug']) ? sanitize_title($data['slug']) : ''; $author = isset($data['author']) ? intval($data['author']) : 0; /* |-------------------------------------------------------------------------- | VALIDATE TITLE |-------------------------------------------------------------------------- */ if (empty($title)) { return new WP_Error( 'missing_title', 'Post title is required.', array( 'status' => 400 ) ); } /* |-------------------------------------------------------------------------- | ALLOWED STATUS |-------------------------------------------------------------------------- */ $allowed_statuses = array( 'draft', 'publish', 'pending', 'private', 'future' ); if (!in_array($status, $allowed_statuses, true)) { $status = 'draft'; } /* |-------------------------------------------------------------------------- | AUTHOR |-------------------------------------------------------------------------- */ if ($author > 0) { $author_user = get_user_by( 'id', $author ); if (!$author_user) { return new WP_Error( 'invalid_author', 'Author not found.', array( 'status' => 400 ) ); } } else { /* |-------------------------------------------------------------------------- | Use currently logged-in user |-------------------------------------------------------------------------- */ $author = get_current_user_id(); } /* |-------------------------------------------------------------------------- | CREATE POST |-------------------------------------------------------------------------- */ $post_data = array( 'post_type' => 'post', 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt, 'post_status' => $status, 'post_author' => $author, ); /* |-------------------------------------------------------------------------- | ADD SLUG |-------------------------------------------------------------------------- */ if (!empty($slug)) { $post_data['post_name'] = $slug; } /* |-------------------------------------------------------------------------- | INSERT POST |-------------------------------------------------------------------------- */ $post_id = wp_insert_post( $post_data, true ); /* |-------------------------------------------------------------------------- | CHECK POST CREATION |-------------------------------------------------------------------------- */ if (is_wp_error($post_id)) { return new WP_Error( 'post_creation_failed', $post_id->get_error_message(), array( 'status' => 500 ) ); } /* |-------------------------------------------------------------------------- | CATEGORIES |-------------------------------------------------------------------------- | | Example: | | "categories": [12, 15] | |-------------------------------------------------------------------------- */ $saved_categories = array(); if ( isset($data['categories']) && is_array($data['categories']) ) { $category_ids = array_map( 'intval', $data['categories'] ); $category_ids = array_filter( $category_ids, function ($id) { return $id > 0; } ); if (!empty($category_ids)) { $category_result = wp_set_post_categories( $post_id, $category_ids, false ); if (is_wp_error($category_result)) { wp_delete_post( $post_id, true ); return new WP_Error( 'category_error', 'Failed to assign categories.', array( 'status' => 400, 'details' => $category_result->get_error_message() ) ); } $saved_categories = $category_ids; } } /* |-------------------------------------------------------------------------- | TAGS |-------------------------------------------------------------------------- | | Example: | | "tags": [25, 31] | |-------------------------------------------------------------------------- */ $saved_tags = array(); if ( isset($data['tags']) && is_array($data['tags']) ) { $tag_ids = array_map( 'intval', $data['tags'] ); $tag_ids = array_filter( $tag_ids, function ($id) { return $id > 0; } ); if (!empty($tag_ids)) { $tag_result = wp_set_post_tags( $post_id, $tag_ids, false ); if (is_wp_error($tag_result)) { wp_delete_post( $post_id, true ); return new WP_Error( 'tag_error', 'Failed to assign tags.', array( 'status' => 400, 'details' => $tag_result->get_error_message() ) ); } $saved_tags = $tag_ids; } } /* |-------------------------------------------------------------------------- | FEATURED MEDIA |-------------------------------------------------------------------------- | | Existing WordPress media ID can be supplied. | | Example: | | "featured_media": 1234 | |-------------------------------------------------------------------------- */ $featured_media_id = 0; if ( isset($data['featured_media']) && intval($data['featured_media']) > 0 ) { $featured_media_id = intval( $data['featured_media'] ); /* |-------------------------------------------------------------------------- | Check attachment |-------------------------------------------------------------------------- */ $attachment = get_post( $featured_media_id ); if ( !$attachment || $attachment->post_type !== 'attachment' ) { $featured_media_id = 0; } } /* |-------------------------------------------------------------------------- | FEATURED IMAGE FROM URL |-------------------------------------------------------------------------- | | This is useful for n8n. | | Example: | | "featured_image_url": | "https://example.com/image.jpg" | |-------------------------------------------------------------------------- */ $featured_image_url = isset($data['featured_image_url']) ? esc_url_raw($data['featured_image_url']) : ''; if ( empty($featured_media_id) && !empty($featured_image_url) ) { /* |-------------------------------------------------------------------------- | Load WordPress media functions |-------------------------------------------------------------------------- */ require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/media.php'; require_once ABSPATH . 'wp-admin/includes/image.php'; /* |-------------------------------------------------------------------------- | Download image |-------------------------------------------------------------------------- */ $tmp = download_url( $featured_image_url, 60 ); /* |-------------------------------------------------------------------------- | Check download error |-------------------------------------------------------------------------- */ if (is_wp_error($tmp)) { wp_delete_post( $post_id, true ); return new WP_Error( 'featured_image_download_failed', 'Failed to download featured image.', array( 'status' => 400, 'details' => $tmp->get_error_message() ) ); } /* |-------------------------------------------------------------------------- | Get filename |-------------------------------------------------------------------------- */ $url_path = wp_parse_url( $featured_image_url, PHP_URL_PATH ); $filename = $url_path ? basename($url_path) : 'featured-image.jpg'; /* |-------------------------------------------------------------------------- | Remove query parameters from filename |-------------------------------------------------------------------------- */ $filename = sanitize_file_name( $filename ); /* |-------------------------------------------------------------------------- | Make sure filename has extension |-------------------------------------------------------------------------- */ if (empty(pathinfo($filename, PATHINFO_EXTENSION))) { $filename .= '.jpg'; } /* |-------------------------------------------------------------------------- | Prepare file |-------------------------------------------------------------------------- */ $file_array = array( 'name' => $filename, 'tmp_name' => $tmp, ); /* |-------------------------------------------------------------------------- | Upload image to Media Library |-------------------------------------------------------------------------- */ $attachment_id = media_handle_sideload( $file_array, $post_id, $title ); /* |-------------------------------------------------------------------------- | Check media upload |-------------------------------------------------------------------------- */ if (is_wp_error($attachment_id)) { @unlink($tmp); wp_delete_post( $post_id, true ); return new WP_Error( 'featured_image_upload_failed', 'Failed to upload featured image.', array( 'status' => 400, 'details' => $attachment_id->get_error_message() ) ); } /* |-------------------------------------------------------------------------- | Set featured image |-------------------------------------------------------------------------- */ $featured_media_id = intval( $attachment_id ); } /* |-------------------------------------------------------------------------- | SET FEATURED IMAGE |-------------------------------------------------------------------------- */ if ($featured_media_id > 0) { set_post_thumbnail( $post_id, $featured_media_id ); } /* |-------------------------------------------------------------------------- | CUSTOM META |-------------------------------------------------------------------------- | | Example: | | "meta": { | "developer_name": "ABC Developer", | "project_address": "Toronto, Canada" | } | |-------------------------------------------------------------------------- */ $saved_meta = array(); if ( isset($data['meta']) && is_array($data['meta']) ) { foreach ( $data['meta'] as $meta_key => $meta_value ) { /* |-------------------------------------------------------------------------- | Sanitize meta key |-------------------------------------------------------------------------- */ $meta_key = sanitize_key( $meta_key ); if (empty($meta_key)) { continue; } /* |-------------------------------------------------------------------------- | Convert array/object to JSON |-------------------------------------------------------------------------- */ if ( is_array($meta_value) || is_object($meta_value) ) { $meta_value = wp_json_encode( $meta_value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ); } /* |-------------------------------------------------------------------------- | Save meta |-------------------------------------------------------------------------- */ update_post_meta( $post_id, $meta_key, $meta_value ); /* |-------------------------------------------------------------------------- | Save response data |-------------------------------------------------------------------------- */ $saved_meta[$meta_key] = $meta_value; } } /* |-------------------------------------------------------------------------- | GET POST CATEGORIES |-------------------------------------------------------------------------- */ $category_objects = get_the_category( $post_id ); $category_response = array(); foreach ($category_objects as $category) { $category_response[] = array( 'id' => $category->term_id, 'name' => $category->name, 'slug' => $category->slug, ); } /* |-------------------------------------------------------------------------- | GET POST TAGS |-------------------------------------------------------------------------- */ $tag_objects = get_the_tags( $post_id ); $tag_response = array(); if ($tag_objects && !is_wp_error($tag_objects)) { foreach ($tag_objects as $tag) { $tag_response[] = array( 'id' => $tag->term_id, 'name' => $tag->name, 'slug' => $tag->slug, ); } } /* |-------------------------------------------------------------------------- | FEATURED IMAGE URL |-------------------------------------------------------------------------- */ $featured_image = ''; if ($featured_media_id > 0) { $featured_image = wp_get_attachment_url( $featured_media_id ); } /* |-------------------------------------------------------------------------- | RESPONSE |-------------------------------------------------------------------------- */ return new WP_REST_Response( array( 'success' => true, 'message' => 'Post created successfully.', 'post' => array( /* |-------------------------------------------------------------------------- | Post ID |-------------------------------------------------------------------------- */ 'id' => $post_id, /* |-------------------------------------------------------------------------- | Title |-------------------------------------------------------------------------- */ 'title' => get_the_title( $post_id ), /* |-------------------------------------------------------------------------- | Slug |-------------------------------------------------------------------------- */ 'slug' => get_post_field( 'post_name', $post_id ), /* |-------------------------------------------------------------------------- | Status |-------------------------------------------------------------------------- */ 'status' => get_post_status( $post_id ), /* |-------------------------------------------------------------------------- | Post URL |-------------------------------------------------------------------------- */ 'url' => get_permalink( $post_id ), /* |-------------------------------------------------------------------------- | Author |-------------------------------------------------------------------------- */ 'author' => array( 'id' => intval( get_post_field( 'post_author', $post_id ) ), 'name' => get_the_author_meta( 'display_name', get_post_field( 'post_author', $post_id ) ), ), /* |-------------------------------------------------------------------------- | Categories |-------------------------------------------------------------------------- */ 'categories' => $category_response, /* |-------------------------------------------------------------------------- | Tags |-------------------------------------------------------------------------- */ 'tags' => $tag_response, /* |-------------------------------------------------------------------------- | Featured Media |-------------------------------------------------------------------------- */ 'featured_media' => $featured_media_id, /* |-------------------------------------------------------------------------- | Featured Image URL |-------------------------------------------------------------------------- */ 'featured_image' => $featured_image, /* |-------------------------------------------------------------------------- | Meta |-------------------------------------------------------------------------- */ 'meta' => $saved_meta, ), ), 201 ); } Samuel Palmer - presalemaster.com
  • Agent License: 090-0348-8346
  • Tax Number: HGT-92384-3434
  • Service Areas: Grand Rapids, Forest Hills, Comstock Park, Caledonia, Kentwood
  • Specialties: Property Management, Consulting, Buyer's Agent, Listing Agent, Relocation

About Samuel Palmer

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse luctus ligula ac faucibus faucibus. Cras in nisi in turpis eleifend vehicula at at massa. Vivamus aliquet porttitor odio sed imperdiet.

Language: English, Spanish, Urdu

Sort by:

No listing found.

1 Review

Sort by:
Leave a Review
  • thumb

    Sed do eiusmod tempor incididunt

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate.

Leave a Review

Compare listings

Compare