Giter VIP home page Giter VIP logo

react-with-wordpress's Introduction

React with WordPress

๐Ÿ”ฅ Example of react application with WordPress REST API

******* PLEASE STAR MY REPO TO SUPPORT MY WORK ๐Ÿ™ ******

Please follow me ๐Ÿ™ on twitter

Dashboard Demo

Pagination ๐Ÿ“น

Post Listings Demo ๐Ÿ“น

Features

  1. CRUD operation with WordPress REST API
  2. Authentication with JWT ( Login Logout )
  3. Accessing public and private routes
  4. Handing WordPress REST API custom end points.
  5. Creating Dashboard with React for CRUD operation.
  6. Pagination

Installation

  1. Clone this repo in git clone https://github.com/imranhsayed/react-with-wordpress

  2. git checkout branchname

  3. Run npm install

git clone [email protected]:imranhsayed/rest-api-endpoints.git

Configure

Add your wordPress siteUrl in src/client-config.js

const clientConfig = {
	siteUrl: 'http://localhost:8888/wordpress'
};

export default clientConfig;

Branches

A React App where you can login using the endpoint provided by JWT Authentication for WP-API WordPress Plugin. So you need to have this plugin installed on WordPress. The plugin's endpoint returns the user object and a jwt-token on success, which we can then store in localstorage and login the user on front React Application

Steps

i. Add the last three lines in your WordPress .htaccess file as shown:

# BEGIN WordPress
   <IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteBase /wordpress/
   RewriteRule ^index\.php$ - [L]
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule . /wordpress/index.php [L]
   
   
   RewriteCond %{HTTP:Authorization} ^(.*)
   RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
   SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
   
   </IfModule>

ii. Add the following in your wp-config.php Wordpress file. You can choose your own secret key.

define('JWT_AUTH_SECRET_KEY', '&BZd]N-ghz|hbH`=%~a5z(`mR=n%7#8-Iz@KoqtDhQ6(8h$og%-IbI#>N*T`s9Dg');
define('JWT_AUTH_CORS_ENABLE', true);

iii. Now you can make a request to /wp-json/jwt-auth/v1/token REST API provided by the plugin. You need to pass username and password and it returns a user object and token . You can save the token in localstorage and send it in the headers of your protected route requests ( e.g. Create Post /wp-json/wp/v2/posts )

iiv. So whenever you send a request to WordPress REST API for your protected routes, you send the token received in the headers of your request

{
	'Accept': 'application/json',
	'Content-Type': 'application/json',
	'Authorization': `Bearer putTokenReceivedHere`
}

This repo also demonstrates how to create posts in React Application by sending request to protected endpoints ( passing the token in the header )

A React(front end) + Node(back end) application. It uses jwt.sign() ( from jwtwebtoken npm package ) to generate a token using the username and password sent from front end( React ) and returns it as a response, which we then store in localstorage to login the user. This token received by frond end, will be sent with all further request for protected routes, which will then be verified in node route using jwt.verify() Besides generating the token, the end point in node also accesses the WordPress rest api to confirm the credentials and returns the user object or errors if any.

It also has functionality to create post where we make a request from front end along with token( React ) to a node end point. The node endpoint verifies the token and then makes a request to WordPress REST API endpoint to create the post and then returns the new post id, or error if any.

Commands

  1. Branch master and build-app-for-heroku
  • start Runs node server for development ( in watch mode ). The server.js sends all front end route request to index.html and then all front end route requests is handled by reach router
  1. Branch jwt-verify-with-node and login-with-jwt-wordpress-plugin
  • dev Runs webpack dev server for development ( in watch mode )

Common

  • prod Runs webpack in production mode

Free Courses

Codeytek

react-with-wordpress's People

Contributors

dependabot[bot] avatar imranhsayed avatar lbonomo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-with-wordpress's Issues

React Hook useEffect has a missing dependency:

React Hook useEffect has a missing dependency: 'store'. Either include it or remove the dependency array. You can also do a functional update 'setStore(s => ...)' if you only need 'store' in the 'setStore' call react-hooks/exhaustive-deps

I am getting this warning. how to fix please help
Screenshot 2021-07-31 at 12 39 16 PM

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.