root@devinsight-hub: ~/wordpress-rest-api-cors-issues-solved
┌──(rootdevinsight-hub)-[~/blog]
└─#
cat solving_rest_api_cors_issues_in_wordpress_as_a_headless_cms.md

Solving Rest API CORS Issues in WordPress as a Headless CMS

┌──[ TABLE OF CONTENTS ]

Solving Rest API CORS Issues in WordPress as a Headless CMS

If you are using WordPress as a Headless Cms then you must be faced these find of header errors like

  • “Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.”
  • “X has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: It does not have HTTP ok status.”
  • “Request header field x-wp-nonce is not allowed by Access-Control-Allow-Headers in preflight response.”

So, when you faced this kind of problem you need to add this solution in your theme function.php . Here is the Solution For this header error

function add_custom_headers() {

    add_filter( 'rest_pre_serve_request', function( $value ) {
        header( 'Access-Control-Allow-Headers: Authorization, X-WP-Nonce,Content-Type, X-Requested-With');
        header( 'Access-Control-Allow-Origin: *' );
        header( 'Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE' );
        header( 'Access-Control-Allow-Credentials: true' );

        return $value;
    } );
}
add_action( 'rest_api_init', 'add_custom_headers', 15 );


Hopefully this solves your WordPress CORS issue!

WordPress REST API CORS Issues Solved

Read More: 4 Step to Create a WordPress Child Theme, WordPress walker_nav_menu: Basic Usage of walker_nav_menu, 5 Steps to Enhance WordPress Search with Autocomplete and AJAX, Easy Floating Share Button – Social Share WordPress Plugin, How to Create WordPress Plugin from Scratch – Step-by-Step Guide

┌──(rootdevinsight-hub)-[~/blog/comments]
└─#
tail -f comments.log

2 Comments

  • [00:56:26] http://boyarka-inform.com/ msg:

    I truly love your site.. Great colors & theme.
    Did you develop this site yourself? Please reply back as I’m hoping to create my own personal blog
    and would love to find out where you gott this from or exactly
    what the theme iss named. Kudos! http://boyarka-inform.com/

  • [00:56:49] http://boyarka-inform.com/ msg:

    I truly love your site.. Great colorts & theme.
    Did you develop this site yourself? Please relly back as
    I’m hoping to create mmy own personal blog and would love
    to find out whee you got this fropm or exactly what the thewme is named.
    Kudos! http://boyarka-inform.com/

Leave a Comment

>
>
>

[SYSTEM] Navigation Initialization v1.2.5-GUIDE

Welcome, Agent. This interface is fully interactive. Here is how to navigate:

  • Type Commands: Use the input at the bottom. Try typing /work or /about and hit Enter.
  • Point & Click: Don't want to type? Hover and click any blue text or folders/ to view content.
  • HUD Controls: Use the + buttons at the top to close, minimize, or expand the terminal.
[PRO-TIP] Press Tab for command suggestions or type /help at any time.
root@kali: ~/exit
$ kill -9 portfolio
✕ Process terminated.
But great code never really stops.
Let's keep the conversation going.
> LinkedIn > GitHub