-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
183 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,14 @@ | ||
# headless-wp | ||
Plugin to enable a headless WP experience | ||
# WP Headless | ||
A lightweight plugin to disable the WP frontend experience. | ||
|
||
This enables you to use Wordpress as a backend service without any risk of security issues. | ||
|
||
## What the plugin actually does | ||
|
||
In simple terms, the plugin removes the frontend of the WordPress site. | ||
|
||
## Why would I want to do that? | ||
|
||
This is definitely not a useful plugin for a public-facing website. | ||
|
||
This is useful if you use Wordpress as a backend service. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
/** | ||
* Main Headless WordPress | ||
* Called from headless-wordpress.php | ||
* | ||
* @package headless-wordpress | ||
*/ | ||
namespace HeadlessWP; | ||
|
||
/** | ||
* Main Headless WordPress class | ||
*/ | ||
final class Headless_WordPress { | ||
|
||
/** | ||
* Class instance | ||
* | ||
* @var object | ||
*/ | ||
private static $instance; | ||
|
||
/** | ||
* Class constructor | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() { | ||
$this->init(); | ||
} | ||
|
||
/** | ||
* Initialize class | ||
* | ||
* @return void | ||
*/ | ||
public static function init() { | ||
$ob_class = get_called_class(); | ||
add_action( 'wp', array( $ob_class, 'headlesswp_frontend_redirect' ) ); | ||
} | ||
|
||
/** | ||
* Die if we try to access a page or the front page | ||
* | ||
* @return void | ||
*/ | ||
public static function headlesswp_frontend_redirect() { | ||
if ( ! is_admin() ) { | ||
|
||
/** | ||
* Fetch the IDs of the post, page or blog page | ||
*/ | ||
$post_ID = get_the_id(); | ||
$homepage_id = get_option( 'page_on_front' ); | ||
$blogpage_id = get_option( 'page_for_posts' ); | ||
|
||
/** | ||
* Do a wp_die so we can't access the site | ||
*/ | ||
if ( $homepage_id === $post_ID || $blogpage_id === $post_ID || is_front_page() ) { | ||
wp_die( 'This site is not accessible' ); | ||
exit; | ||
} else { | ||
|
||
/** | ||
* Else do a redirect back to WP admin again | ||
*/ | ||
|
||
$post_edit_link = admin_url( 'post.php?post=' . $post_ID . '&action=edit' ); | ||
|
||
if ( is_user_logged_in() ) { | ||
/** | ||
* Logged in users go to the post edit screen | ||
*/ | ||
wp_safe_redirect( $post_edit_link ); | ||
exit; | ||
} else { | ||
/** | ||
* Not logged in? Redirect to login page | ||
*/ | ||
wp_safe_redirect( wp_login_url( $post_edit_link ) ); | ||
exit; | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Get class object instance | ||
* | ||
* @return object | ||
*/ | ||
public static function get_instance() { | ||
if ( ! self::$instance ) { | ||
self::$instance = new Headless_WordPress(); | ||
} | ||
return self::$instance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
/*** | ||
Plugin Name: Headless WordPress | ||
Plugin URI: | ||
Description: Disable the WordPress frontend. Useful for sites only using the WordPress API. | ||
Version: 1.0.2 | ||
Author: Daniel F | ||
Author URI: http://www.dfweb.no | ||
License: GPL3 | ||
License URI: https://www.gnu.org/licenses/gpl-3.0.html | ||
Text Domain: headless-wordpress | ||
@package headless-wordpress | ||
Headless WordPress is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
any later version. | ||
Headless WordPress is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with Headless WordPress. If not, see https://www.gnu.org/licenses/gpl-3.0.html. | ||
*/ | ||
|
||
// Abort if this file is called directly. | ||
if ( ! defined( 'ABSPATH' ) ) { | ||
exit; | ||
} | ||
|
||
/** | ||
* Main class file | ||
*/ | ||
require_once plugin_dir_path( __FILE__ ) . '/classes/class-headless-wordpress.php'; | ||
|
||
$headlesswp = \HeadlessWP\Headless_WordPress::get_instance(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
=== WP Headless === | ||
Tags: headless,cms | ||
Requires at least: 5.0 | ||
Tested up to: 5.4.2 | ||
Stable tag: 1.0.1 | ||
Requires PHP: 7.2.0 | ||
License: GPL3 | ||
License URI: https://www.gnu.org/licenses/gpl-3.0.html | ||
|
||
A lightweight plugin to disable the WP frontend experience. | ||
|
||
== Description == | ||
|
||
Logged in users are redirected to the editor screen for the post, while non-logged in users must log in first. This means that you can share a readable link straight to the editor, depending on your permalink settings. | ||
|
||
Good for task management or organisation resource management. | ||
|
||
## What the plugin actually does | ||
|
||
In simple terms, the plugin removes the frontend of the WordPress site. Post permalinks go straight to the editor page, and the theme is mostly redundant. | ||
|
||
## Why would I want to do that? | ||
|
||
This is definitely not a useful plugin for a public-facing website. Instead, this allows you to convert WordPress into a headless CMS for managing tasks, content and more, all protected by the standard user role system. You can share a post permalink with a collaborator and they will be directed straight to the editor screen. | ||
|
||
Maybe you want to go a step further and create an external application to display your WordPress content elsewhere using the API. In this case you definitely don't want the standard WP frontend accessible! |