All Projects → gopangolin → Wp Reactivate

gopangolin / Wp Reactivate

Licence: gpl-3.0
React boilerplate for WordPress plugins

Projects that are alternatives of or similar to Wp Reactivate

Wp Vuejs
WordPress VueJS Starter Theme
Stars: ✭ 19 (-93.73%)
Mutual labels:  webpack, wordpress, boilerplate
Awesome Gravity Forms
A collection of third party add-ons for Gravity Forms plugin.
Stars: ✭ 267 (-11.88%)
Mutual labels:  wordpress, wordpress-plugin
Wp Graphql
🚀 GraphQL API for WordPress
Stars: ✭ 3,097 (+922.11%)
Mutual labels:  wordpress, wordpress-plugin
Create Guten Block
📦 A zero-configuration #0CJS developer toolkit for building WordPress Gutenberg block plugins.
Stars: ✭ 3,040 (+903.3%)
Mutual labels:  wordpress, wordpress-plugin
React Bolt
⚡ The most simple & robust boilerplate for your React projects.
Stars: ✭ 298 (-1.65%)
Mutual labels:  webpack, boilerplate
Anspress
AnsPress is the most complete question and answer system for WordPress. AnsPress is made with developers in mind, highly customizable. AnsPress provide an easy to use override system for theme
Stars: ✭ 264 (-12.87%)
Mutual labels:  wordpress, wordpress-plugin
Threejs Webpack Es6 Boilerplate
A basic boilerplate for a Three.js project compiled with Webpack and transpiled via Babel to enable using ES6 syntax.
Stars: ✭ 267 (-11.88%)
Mutual labels:  webpack, boilerplate
Fabrica Dev Kit
A toolkit for faster, smoother WordPress 5 development
Stars: ✭ 256 (-15.51%)
Mutual labels:  wordpress, wordpress-plugin
Admin Page Framework
Facilitates WordPress plugin and theme development.
Stars: ✭ 273 (-9.9%)
Mutual labels:  wordpress, wordpress-plugin
Awesome Woocommerce
Plugins and code snippets to improve your WooCommerce store.
Stars: ✭ 279 (-7.92%)
Mutual labels:  wordpress, wordpress-plugin
Wp Graphiql
GraphiQL IDE, fine tuned for use with WPGraphQL
Stars: ✭ 284 (-6.27%)
Mutual labels:  wordpress, wordpress-plugin
Vue Express Mongo Boilerplate
⭐ MEVN Full stack JS web app boilerplate with NodeJS, Express, Mongo and VueJS
Stars: ✭ 2,814 (+828.71%)
Mutual labels:  webpack, boilerplate
Vue Electron Template
An Electron & Vue.js template with Hot-reloading enabled and common Vue plugins, dev, debug and build scripts configured.
Stars: ✭ 260 (-14.19%)
Mutual labels:  webpack, boilerplate
Rockpack
Rockpack is a simple solution for creating React Application with Server Side Rendering, bundling, linting, testing within 5 minutes
Stars: ✭ 265 (-12.54%)
Mutual labels:  webpack, boilerplate
Papi
🚀 WordPress Page Type API with custom fields
Stars: ✭ 258 (-14.85%)
Mutual labels:  wordpress, wordpress-plugin
Frontend Boilerplates
Collection of Boilerplates with ES6, Vue, React, Nuxt, TypeScript, SCSS, Nodejs. Using good practices and file structures to inspire your real projects.
Stars: ✭ 269 (-11.22%)
Mutual labels:  webpack, boilerplate
Minit
A WordPress plugin to combine CSS and Javascript files.
Stars: ✭ 293 (-3.3%)
Mutual labels:  wordpress, wordpress-plugin
Frontend Webpack Boilerplate
Simple starter webpack 5 project template supporting SASS/PostCSS, Babel ES7, browser syncing, code linting. Easy project setup having multiple features and developer friendly tools.
Stars: ✭ 242 (-20.13%)
Mutual labels:  webpack, boilerplate
Wp Api React
This boilerplate will help you use React JS with Wordpress REST API.
Stars: ✭ 255 (-15.84%)
Mutual labels:  webpack, wordpress
Generator Plugin Wp
Yeoman WordPress Plugin Generator
Stars: ✭ 270 (-10.89%)
Mutual labels:  wordpress, wordpress-plugin

alt-text

WP Reactivate

WP Reactivate is a React boilerplate built specifically for WordPress, allowing you to quickly and easily integrate React into your WordPress plugins.

⚠️Since the release of Block Editor (Gutenberg) the Javascript ecosystem around WordPress has evolved rapidly. This repository, does not represent the best practices that should be used for React development in the WordPress.⚠️

Setup and installation

Usage

  • Install required modules: yarn (or npm install)
  • Build development version of app and watch for changes: yarn build (or npm run build)
  • Build production version of app:yarn prod (or npm run prod)

Quick Start

Introduction

This boilerplate plugin provides three different WordPress views in which an independant React app can be rendered:

  • Shortcode
  • Widget
  • Settings page in the backend (wp-admin)

Each JavaScript root file will correspond to the independant React app to be bundled by Webpack.

webpack.config.js

entry: {
  'js/admin': path.resolve(__dirname, 'app/admin.js'),
  'js/shortcode': path.resolve(__dirname, 'app/shortcode.js'),
  'js/widget': path.resolve(__dirname, 'app/widget.js'),
},

Using the Shortcode

In order to get the shortcode attributes into our Javascript we need to pass them to an object which will be made available to the shortcode.js app via wp_localize_script. Be careful with the security of data you pass here as this will be output in a <script> tag in the rendered html.

includes/Shortcode.php

public function shortcode( $atts ) {
  wp_enqueue_script( $this->plugin_slug . '-shortcode-script' );
  wp_enqueue_style( $this->plugin_slug . '-shortcode-style' );

  $object_name = 'wpr_object_' . uniqid();

  $object = shortcode_atts( array(
    'title'       => 'Hello world',
    'api_nonce'   => wp_create_nonce( 'wp_rest' ),
    'api_url'	  => rest_url( 'wp-reactivate/v1/' ),
  ), $atts, 'wp-reactivate' );

  wp_localize_script( $this->plugin_slug . '-shortcode-script', $object_name, $object );

  $shortcode = '<div class="wp-reactivate-shortcode" data-object-id="' . $object_name . '"></div>';
  return $shortcode;
}

You can access the shortcode attributes via the wpObject prop which is passed into your React container component.

app/containers/Shortcode.jsx

import React, { Component } from 'react';

export default class Shortcode extends Component {
  render() {
    return (
      <div className="wrap">
        <h1>WP Reactivate Frontend</h1>
        <p>Title: {this.props.wpObject.title}</p>
      </div>
    );
  }
}

Using the Widget

In order to get the widget options into our Javascript we need to pass them to an object which will be made available to the widget.js app via wp_localize_script. Be careful with the security of data you pass here as this will be output in a <script> tag in the rendered html.

includes/Widget.php

public function widget( $args, $instance ) {
  wp_enqueue_script( $this->plugin_slug . '-widget-script', plugins_url( 'assets/js/widget.js', dirname( __FILE__ ) ), array( 'jquery' ), $this->version );
  wp_enqueue_style( $this->plugin_slug . '-widget-style', plugins_url( 'assets/css/widget.css', dirname( __FILE__ ) ), $this->version );

  $object_name = 'wpr_object_' . uniqid();

  $object = array(
    'title'       => $instance['title'],
    'api_nonce'   => wp_create_nonce( 'wp_rest' ),
    'api_url'	  => rest_url( 'wp-reactivate/v1/' ),
  );

  wp_localize_script( $this->plugin_slug . '-widget-script', $object_name, $object );

  echo $args['before_widget'];

  ?><div class="wp-reactivate-widget" data-object-id="<?php echo $object_name ?>"></div><?php

  echo $args['after_widget'];
}

You can access the widget options via the wpObject prop which is passed into your React container component.

app/containers/Widget.jsx

import React, { Component } from 'react';

export default class Widget extends Component {
  render() {
    return (
      <div className="wrap">
        <h1>WP Reactivate Widget</h1>
        <p>Title: {this.props.wpObject.title}</p>
      </div>
    );
  }
}

Using REST Controllers

We have included a single base REST controller class in the plugin. You will need to use this controller to create endpoints to interact with your React components. Depending on the complexity of your plugin you may need to have multiple controllers or may want to extend default WordPress REST API endpoints.

We have chosen the custom controller approach for its control and flexibility. Please see the WordPress developer documentation on adding custom endpoints and specifically the controller pattern to familiarise your with our choice of implementation.

It is important to become well versed in using the WordPress REST API as this is how you will be passing data to and from your React applications.

Using the Settings Page

In our admin class we add a sub menu page to the Settings menu using add_options_page and render the React Admin container onto the root DOM node.

includes/Admin.php

public function display_plugin_admin_page() {
 ?><div id="wp-reactivate-admin"></div><?php
}

Using fetchWP

We have provided a utility class called fetchWP which is a simple abstraction over the Fetch API which allows you to easily make requests to the WordPress REST API.

In the React container component we show how to retrieve and update this setting via the example REST controller included in the boilerplate.

First we initialise fetchWP in the ES6 class constructor of our container component. It requires two parameters being the REST URL and the REST nonce which can be supplied from our wpObject.

app/containers/Admin.jsx

  constructor(props) {
    super(props);

    this.state = {
      example_setting: '',
    };

    this.fetchWP = new fetchWP({
      restURL: this.props.wpObject.api_url,
      restNonce: this.props.wpObject.api_nonce,
    });

    this.getSetting();
  }

In the getSetting call you can now see how we use the utility to perform a GET request on the 'example' endpoint.

app/containers/Admin.jsx

  getSetting = () => {
    this.fetchWP.get( 'example' )
    .then(
      (json) => this.setState({
        example_setting: json.value,
        saved_example_setting: json.value
      }),
      (err) => console.log( 'error', err )
    );
  };

We have found this utility covers most of our use cases. If you are looking for something more full featured (especially if you are working with standard WP endpoints) then have a look at node-wpapi.

Technologies

Tech Description
React A JavaScript library for building user interfaces.
Babel Compiles next generation JS features to ES5. Enjoy the new version of JavaScript, today.
Webpack For bundling our JavaScript assets.
ESLint Pluggable linting utility for JavaScript and JSX

The boilerplate has been updated to use PHP namespaces and autoloading. Please see Tom McFarlin's article on the subject if you are not familiar.

Tutorials

Building a WordPress plugin with React - Part 1, Part 2

Credits

Made by Pangolin

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].