# wpdirectorykit/1.5.9/rest_api.php

WP Directory Kit, version 1.5.9. 60 lines.

- Page: https://pluginprobe.com/plugins/wpdirectorykit/1.5.9/code/rest_api.php
- Raw: https://pluginprobe.com/plugins/wpdirectorykit/1.5.9/raw/rest_api.php
- Modified: 2026-10-04T19:48:12+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/wpdirectorykit/1.5.9/code/rest_api.php#L10-L20`.

```php
<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

add_action('rest_api_init', function () {
    register_rest_route('wdk/v1', '/listing/autosuggestion', [
        'methods'  => 'POST',
        'callback' => 'wdk_listing_search_rest_callback',
        'permission_callback' => function () {
            return current_user_can('edit_posts');
        },
    ]);
});

function wdk_listing_search_rest_callback($request)
{
    $params = $request->get_json_params();
    $keyword = '';

    if ($params && isset($params['keyword'])) {
        $keyword = sanitize_text_field($params['keyword']);
    } 

    if (strlen($keyword) < 2) {
        return rest_ensure_response([
            'success' => true,
            'data' => []
        ]);
    }

    $args = [
        'post_type'      => 'wdk-listing',
        'posts_per_page' => 10,
        's'              => $keyword,
    ];

    // Optionally exclude the currently executed ID if provided
    if (isset($params['executedId'])) {
        $args['post__not_in'] = [intval($params['executedId'])];
    }

    $posts = get_posts($args);

    $result = [];
    foreach ($posts as $post) {
        $result[] = [
            'id'    => $post->ID,
            'title' => $post->post_title,
            'edit'  => urlencode(admin_url('admin.php?page=wdk_listing&id=' . $post->ID)),
        ];
    }

    return rest_ensure_response([
        'success' => true,
        'data' => $result
    ]);
}

```
