• Resolved 00digital

    (@00digital)


    Hi,

    The plugin works great, love the UI. When i add the origin to the plugin it works on /wp-json/wp/v2/posts which is the standard posts endpoint for the WP REST API.

    But i have a custom one that i made to pull a specific custom post type, but its still failing the CORS test on the test website (and front end).

    Ive even added the headers into the function itself, but somehow its not passing it through. Console.log shows the blocked by cors error. But the other default endpoints work. Does anyone know any workarounds to this?

    add_action('rest_api_init', function () {
    register_rest_route('site/v1', '/all-events/', array(
    'methods' => 'GET',
    'callback' => 'get_all_events',
    'permission_callback' => '__return_true'
    ));
    });

    function get_all_events($request) {
    // Add CORS headers
    $origin = get_http_origin();
    $allowed_origins = ['https://clublandkl-next.vercel.app', 'https://localhost:3000', 'https://localhost:3001'];

    if ($origin && in_array($origin, $allowed_origins)) {
    header("Access-Control-Allow-Origin: $origin");
    header("Access-Control-Allow-Methods: GET, OPTIONS");
    header("Access-Control-Allow-Headers: Content-Type");
    header("Access-Control-Allow-Credentials: true");
    }

    // Handle preflight requests
    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    status_header(200);
    exit();
    }
Viewing 1 replies (of 1 total)
  • Plugin Author Dev Kabir

    (@devkabir)

    Your code has a syntax error and the flow needs improvement. Adding some hooks and filters will help make it work. It might be best to hire an expert developer to resolve this issue.

Viewing 1 replies (of 1 total)
  • The topic ‘Blocked by CORS on custom endpoint’ is closed to new replies.