Wordpress as a CMS - best way to decouple publishing/front-end

For various reasons, this is important, but I am unsure if their are better ways OR other ramifications:

CMS = Wordpress as a CMS.

WebSite = Static site generator.

Currently when a post publishes on CMS I used API to send the info to WebSite which generates the page. All works.

However, I don't want the CMS to ALSO publish post.

I current handle this by redirecting all traffic to CMS to /wp-admin/ page.

This seems not ideal.

I would rather decouple publish button from actually publishing post.

Is this the best way?:

function change_publish( $post_ID )  
{
// Do something here //
}

add_action( 'publish_post', 'change_publish' ); 

I.e. to override the publish function?

Edits per comment from @alx

  1. I will/would want to utilize the publish post status to indicate the post is published, however to clarify this see below:
  2. You make a question of Rest API vs. Custom API - to clarify as I can here, with WP Remove Get and Post, I am making HTTP Post and Get requests to other resources, but there could be something you are needing information wise that I am not aware of, I am not utilizing Wordpresses REST API endpoints. To clarify further: When a person hits publish on the post, I've hooked into the publish to initiate an HTTP post statement to a separate resource, where the article actually publishes. However, wordpress, natively still does publish that post to the local site.
  3. If I have a wordpress instance hosted at myCMS.com - and I hit publish on a post from myCMS.com/wp-admin/ I don't want that post to be available on the front end of myCMS.com - from a browser or endpoint.

Please let me know that clarifies.

Also as an additional note, I assume I could just have posts which are published have a visibility of private or password protected, but is their a true decoupling from the front end publishing?

Topic headless cms Wordpress

Category Web


To disable WP frontend and WP REST API output, create an empty theme, i.e. 'My Empty Theme' in /wp-content/themes/myemptytheme/, that contains 3 files:

  • style.css
  • index.php
  • functions.php

style.css may contain the theme name only:

/*
Theme Name: My Empty Theme
*/

index.php - leave it empty

functions.php - include the following code to disable REST API output:

<?php
   add_filter( 'rest_authentication_errors', 'no_rest_api' );
   function no_rest_api( $result ) {
          return new WP_Error( 'no_rest_api', 'Rest API disabled', array( 'status' => rest_authorization_required_code()));
   }
?>

When you query a REST API endpoint, the response will be:

{
  "code": "no_rest_api",
  "message": "Rest API disabled",
  "data": {
     "status": 403
  }
}

Remember to switch to the new theme after creating the theme files.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.