Skip to content

Commit

Permalink
Published initial version of plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
w3bdesign committed Jul 11, 2020
1 parent 696a7f1 commit 45e20fa
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 7 deletions.
10 changes: 5 additions & 5 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
GNU GENERAL PUBLIC LICENSE
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007

Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.

Expand Down Expand Up @@ -645,7 +645,7 @@ the "copyright" line and a pointer to where the full notice is found.
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
along with this program. If not, see <http://www.gnu.org/licenses/>.

Also add information on how to contact you by electronic and paper mail.

Expand All @@ -664,11 +664,11 @@ might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
<https://www.gnu.org/licenses/>.
<http://www.gnu.org/licenses/>.

The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
<https://www.gnu.org/licenses/why-not-lgpl.html>.
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
16 changes: 14 additions & 2 deletions README.md
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.
98 changes: 98 additions & 0 deletions classes/class-headless-wordpress.php
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;
}
}
40 changes: 40 additions & 0 deletions headless-wordpress.php
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();

26 changes: 26 additions & 0 deletions readme.txt
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!

0 comments on commit 45e20fa

Please sign in to comment.