Headless WordPress Tweaks
A few tweaks make a lot of sense when running WordPress headlessly.
Disable Post and Page Previews
Without an actual WordPress front end, one may as well get rid of post and page previews:
add_filter( 'register_post_type_args', function( $args, $post_type ) {
if ( in_array( $post_type, array( 'post', 'page' ) ) ) {
$args['public'] = false; // Disable most front-end magic.
$args['show_ui'] = true; // Leave Edit screens enabled.
}
return $args;
}, 99, 2);
Correct Permalinks
Point links inside WP Admin to the correct front-end domain…
add_filter( 'home_url', function( $url, $path, $orig_scheme, $blog_id = null ) {
return str_replace(
'//backend.somedomain.com',
'//mysite.com',
$url
);
}, 10, 4 );
…but keep WP REST API routes unchanged, so they can still be called, e.g., through the front end.
add_filter( 'rest_url', function( $url, $path, $orig_scheme, $blog_id = null ) {
return str_replace(
'//mysite.com',
'//backend.somedomain.com',
$url
);
}, 10, 4 );
Correct Internal Linking
add_filter( 'wp_link_query_args', function( $args ) {
// Fixes `wp_link_query()` results by again including now non-public (see above) post types.
$args['post_type'] = array( 'post', 'page' );
return $args;
} );
Change Uploads Location
Why not take out ‘wp-content’ from image URLs, when not using WordPress’s front end? (The following would go into wp-config.php
.)
define('UPLOADS', 'uploads'); // Rather than 'wp-content/uploads'.
More to come!