1

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?

1
  • 1
    Your question is not complete. Do you use WordPress REST API (/wp-json/) to make it available in the front-end or do you have a custom API? How do you define which posts should be published? In standard WP, /wp-json/ publishes posts with post_status field set to 'publish' value. If 'publish' status is not the argument to publish, then what do you expect to separate the posts to publish and to hide? Also, when you say 'I don't want the CMS to ALSO publish post', what do you mean: publish in your custom front-end or in standard REST API? If you do not want to publish, you can use 'Dfaft' status.
    – Oleksii
    Feb 7, 2022 at 4:23

1 Answer 1

1
+50

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.

4
  • I'll test within the day and let you know if anything I run into or not aware of or or. But thank you for your answer so far.
    – Rogelio
    Feb 10, 2022 at 20:01
  • @Rogelio, I skipped the suggested edit because I did not quite understand it. For example, if you apply my above solution, the frontend no longer exists because the theme is empty. In this case, if you still want to utilize the 'publish' status, you do it in the admin dashboard, but the post is not output to the frontend because the theme templates do not exist: the only template is index.php and it is empty. You mentioned that the post is still published, what do you mean? Did you switch to the empty theme?
    – Oleksii
    Feb 11, 2022 at 19:46
  • The only thing that publishing does is saving the post content in the database with post_status field set to 'publish'.
    – Oleksii
    Feb 11, 2022 at 19:56
  • I didn't understand that aspect. Thank you for taking the time to elaborate. There was a bit of a funny cache in place. This totally does work.
    – Rogelio
    Feb 11, 2022 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.