WordPress
be galvos

WordPress Meetup Kaunas 2021-01

Arūnas Liuiza

WordPress Core Contributor, WordPress Kaunas Meetup co-organizer, WordCamp, WordSesh, TEDx speaker and one of the editors of the Lithuanian WordPress translation team.

 

Free, premium and custom WordPress plugin developer

 

Engineering Team Lead at

/whois WordPress

  • An open source CMS (PHP/MySQL/React)
  • 38.8% of top 10 million websites (W3Techs)
  • 63.6% CMS market share

Architecture

MVC

Event-driven

Pros and Cons

  • Pros
    • High extensibility
    • Weak coupling
    • Apparent simplicity
  • Cons
    • Hidden complexity
    • Difficult debugging

 

David Hayes

Traditional WordPress

Visitor

Theme

Core

Plugins

Headless WordPress

Visitor

API

Core

Plugins

????

Decoupled WordPress

Visitor

API

Core

Plugins

Theme

Mobile App

3rd party client

...

Pros and Cons

 

  • Pros
    • Separation between content and presentation
    • Multi-client/multi-channel support
    • Separated development
    • Less platform/technology lock-in
  • Cons
    • A layer of complexity
    • Losing a significant part of plugin ecosystem
    • Longer intial set-up and development

Whitepaper "What to know before you go decoupled"

How?

API

  • REST API
  • GraphQL

WP REST API

  • Part of WordPress Core since 4.7 (2016)
  • Good coverage of all standard WP Content
  • Easily extendable
  • Good support in plugins

 

  • Okay(ish) performance
  • Authentication

How to use it?

https://example.com/wp-json/
  • On by default on all WP sites
  • The prefix can be changed via filters
  • Core namespace is wp/v2

 

https://example.com/wp-json/wp/v2/posts

- list of latest posts

 

 

More details about using the API - in the handbook

Available endpoints

Full list can be found calling the root endpoint or in the reference.

 

  • Posts, pages, custom post types
  • Categories, tags, custom taxonomies
  • Media
  • Users
  • Plugins and Themes
  • Blocks
  • Settings

How to extend it?

add_action( 'rest_api_init', 'arunas_endpoint' );

function arunas_endpoint () {
  register_rest_route( 
    'myplugin/v1', 
    '/item/(?P<id>\d+)', 
    [
        'methods' => 'GET',
        'callback' => 'arunas_callback',
    ] 
  );
}

function arunas_callback( $request ) {
    $item = $request->get_param( 'id' ); 
    $data = method_to_get_data( $item );
    $response = new WP_REST_Response( $data );
    return $response;
}

GraphQL

  • Achievable by plugins
    • https://www.wpgraphql.com/
    • https://github.com/GraphQLAPI/graphql-api-for-wp

 

  • Single request to get a complex set of data
  • Self documenting

 

  • Less stable (possibility of breaking changes)
  • Less plugin support

How to use it?

https://example.com/grapql/

 

Make sure pretty permalinks are enabled and updated

How to extend it?

add_action( 'graphql_register_types', 'register_dog_type' );

function register_dog_type() {
    register_graphql_object_type( 'Dog', [
      'description' => __( "Man's best friend", 'your-textdomain' ),
      'fields' => [
        'name' => [
            'type' => 'String',
            'description' => __( 'The name of the dog', 'your-textdomain' ),
        ],
        'breed' => [
            'type' => 'String',
            'description' => __( 'The Breed of the dog', 'your-textdomain' ),
        ],
        'age' => [
            'type' => 'Integer',
            'description' => __( 'The age, in years, of the dog', 'your-textdomain' ),
        ],
      ],
    ] );
}

How to extend it?

add_action( 'graphql_register_types', 'register_dog_field' );

function register_dog_field() {

    register_graphql_field( 'RootQuery', 'getDog', [
      'description' => __( 'Get a dog', 'your-textdomain' ),
      'type' => 'Dog',
      'resolve' => function() {

        // Here you need to return data that matches the shape of the "Dog" type. 
        // You could get the data from the WP Database, an external API, or 
        // static values. For example sake, we will just return a hard-coded array.
        return [
            'name' => 'Sparky',
            'breed' => 'Golden Retriever',
            'age' => 8
        ];

      }
    ] );

}

Real-life issues

  • A big part of WordPress plugins do not work as expected.
  • Missing Core features like post previews, password protected posts, template hierarchy, etc.

Questions?

WordPress be galvos

By Arūnas Liuiza

WordPress be galvos

WordPress Meetup Kaunas 2021-01

  • 327
Loading comments...

More from Arūnas Liuiza