适用于 WordPress 插件的 React 套件
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.⚠️
yarn (or npm install)yarn build (or npm run build)yarn prod (or npm run prod)This boilerplate plugin provides three different WordPress views in which an independant React app can be rendered:
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'),
},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 `````` 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 = '
';
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 (
);
}
}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 `````` tag in the rendered html.
includes/Widget.php
…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.
| 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.
Building a WordPress plugin with React - Part 1, Part 2
Made by Pangolin
暂无开放 Issues,或尚未同步最近议题。