WordPress is a great platform for creating a website or blog. It has been around for over a decade and it’s still growing in popularity. It’s one of the most popular content management systems on the internet, used by millions of people and thousands of companies.

But did you know that WordPress can also be used as an API for a separate frontend?

In this article, I will present a set of plugins and configuration to easily use WordPress as an API for a frontend or mobile application.

In my test project, I placed the frontend application on the main domain, while WordPress was installed on the api.domain.com subdomain.

For the api, it is worth either in the .htaccess file abo also in one of the php files (e.g. functions.php of the template used) to make a redirect to the main domain for paths other than /wp-admin, /wp-login and /index.php?graphql.

Plugins and tools

WP GraphQL

With the WP GraphQL plugin, you can access all of WordPress’ data using GraphQL API. It provides easier and wider access to data compared to the default REST API.

GraphQL code generator

The GraphQL code generator can be used to generate code like typed queries, mutations, subscriptions and typed GraphQL resolvers. Here is great article on how to set it up.

Polylang

Polylang is a multilingual plugin for WordPress that lets you create a website in multiple languages.

WP GraphQL Polylang

This plugin exposes Polylang languages and translations in the GraphQL schema.

WPGraphQL Cors

You should also consider to use WPGraphQL Cors allowing you to set CORS headers that GraphQL will accept, which means WordPress default auth cookies can be accepted.

SQLite Database Integration

If you want to simplify your setup, you can use SQLite instead of MySQL database.

Custom fields:

If you need to use custom fields, you can enable it in: New post => Preferences => Panel => Additional => Custom fields.

Then register the custom type to make it retrievable via GraphQL:

add_action('graphql_register_types', function() {
    register_graphql_field('Post', 'CUSTOM_FIELD_NAME', [
       'type' => 'String',
       'description' => __('CUSTOM_FIELD_DESCRIPTION', 'wp-graphql'),
       'resolve' => function($post) {
         $field = get_post_meta($post->ID, 'CUSTOM_FIELD_NAME', true);
         return ! empty($field) ? $field : null;
       }
    ]);
});

Overall, the WordPress API is a powerful and versatile tool that allows developers to create a completely custom frontend for their WordPress website. It is easy to use and understand, and highly extensible, making it a interesting choice for developers who are looking to create a custom user experience for their website. With the WordPress API, developers can easily add new features and functionality, and integrate other services and applications into the WordPress platform.