WPIntell

Source evidence

Blocked by CORS on custom endpoint

Enable CORS · support · 2024-08-05T09:59:00+00:00

mixedsentiment
mediumseverity
0.73relevance
1replies
Evidence linked to opportunitycommercial context

Proof Health

Open evidence

Commercial opportunities need traceable source links before they are treated as build-worthy.

2 / 30 rows with source links

6.7% of this page's analysis has direct source links.

0 build-decision rows missing links

0 rows here require auditable proof before promotion.

28 rows with no attached evidence

0 rows have source counts but still need direct links.

Conversation

support
00digital resolved
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', 'http://localhost:3000', 'http://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(); } 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.

Comments

1 shown
Dev Kabir 2024-08-06T15:44:00+00:00

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.