October 22, 2024
Chicago 12, Melborne City, USA
PHP

How to create a custom REST API endpoint for publishing posts with specific permalinks in WordPress?


I’m a new with php and I am working on a WordPress project where I need to create a custom REST API endpoint that allows publishing posts with specific permalinks. Here’s what I’m trying to achieve:

  1. Create a new REST API endpoint, something like /wp-json/custom/post
  2. This endpoint should accept POST requests with the following parameters:
    • title: The title of the post
    • content: The content of the post
    • custom_permalink: The specific permalink I want to use for this post
  3. The endpoint should create a new post with the provided title and content, and set its permalink to the specified custom permalink
  4. It should return a success message with the post ID if the creation is successful, or an error message if it fails

The main reason I need this custom solution is that the standard slug parameter in the WordPress REST API doesn’t support forward slashes (/) and automatically converts them to hyphens (-). For example:

  • If I set slug to ndd/test/test2, WordPress converts it to ndd/test-test2
  • What I actually want is to keep the structure as ndd/test/test2

I don’t understand why this isn’t possible natively in WordPress for posts, which is why I’m seeking a custom solution.

I’ve tried using the register_rest_route() function to create the endpoint, but I’m not sure how to properly set up the rewrite rules to allow for these custom permalinks.

Here’s a basic structure I’ve started with:

add_action('rest_api_init', function () {
    register_rest_route('custom/v1', '/post', array(
        'methods' => 'POST',
        'callback' => 'create_custom_post_callback',
        'permission_callback' => function() {
            return current_user_can('publish_posts');
        }
    ));
});

function create_custom_post_callback($request) {
    $title = $request->get_param('title');
    $content = $request->get_param('content');
    $custom_permalink = $request->get_param('custom_permalink');
    
    // Create post
    $post_id = wp_insert_post(array(
        'post_title' => $title,
        'post_content' => $content,
        'post_status' => 'publish',
    ));

    if (is_wp_error($post_id)) {
        return new WP_Error('failed-to-create', 'Failed to create post', array('status' => 500));
    }

    // How do I set the custom permalink here?
    // What rewrite rules do I need to modify?
    
    return new WP_REST_Response(array('post_id' => $post_id), 200);
}

My main questions are:

  1. What rewrite rules do I need to modify to allow for these custom permalinks with multiple levels (e.g., ndd/test/test2)?
  2. How can I set the custom permalink for the newly created post within the callback function, preserving the desired structure?
  3. Are there any security considerations I should keep in mind when implementing this, especially considering the ability to create arbitrary URL structures?
  4. Is there a way to achieve this using WordPress’s native functionality that I might be overlooking?

Thank you in advance for your help!



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video