-
Easy to Use
Git clone this repository inwp-content/plugins/andnpm run bootstrap,npm run dev, that's all the steps -
Integrate with WordPress RestFul API
By default, theAPI_URLwould be{site_url}/wp-json}which is set ininc/admin.php -
CRUD utility function
Default withgetPostExample, check more utilities -
Integrate with JWT
We useusefulteam/jwt-authin composer, every time a WordPress user logged in, he will get JWT (for call API, if the user has enough capability) automatically. -
HMR (Hot Module Reload) for PHP
By usingvite-plugin-live-reload, the page will auto reload while PHP files changed -
Multi-React-App in one plugin
By default, we render 2 React App, 1 is for front-end page, and 1 is for admin page. You can add more React App injs/src/main.tsx
Tech stacks (knowledge you need to have)
- Vite - build tool
- React v18
- TypeScript - compile project with type safe
- Tailwind v3 - you can install any UI library, like Ant Design, MUI, Chakra...etc
- SCSS
- React Query v4 - managing API status
- React Router v6 - front-end router
- usefulteam/jwt-auth - get JWT if a wordpress user is logged in
-
Clone this repository into
/wp-content/plugins.cd {your-project}/wp-content/plugins git clone https://github.com/j7-dev/wp-react-plugin.git cd wp-react-plugin
-
Install dependencies:
⭐ You must have Composer installed
npm run bootstrap # This will run `npm install` & `composer install` npm run dev -
Activate the plugin from WordPress admin
/wp-admin.
-
Visit your site's homepage and see the rendered application on the footer 🚀🚀🚀
-
Click
Count,Get Post Examplebutton to ensure the State and the WordPress API works
-
Check the admin page, you will see a new post type
My Appand a new menuMy App
-
Click Add New, you will see a React App in the metabox
-
🎉🎉🎉 Enjoy the dev 🎉🎉🎉
npm run buildAfter you build the project will apply .env.production and enqueue the hashed assets in js/dist folder.
the files in js/dist is EXACT the files of your plugin, you can only upload the js/dist if you don't want to share the src source code
path: js\src\api\resources
{ resource: string, // ↑ WordPress RESTFUL API Endpoint like: posts, users, products args?: { [key: string]: any }, // ↑ Check the WordPress RESTFUL API Endpoint args config?: any // ↑ This is Axios Config, see more info in https://axios-http.com/ // ex: change headers config for special use }The created Resource id
const createPost = await createResource({ resource: 'posts', args: { title: 'Post Created by API', status: 'publish', }, }){ resource: string, // ↑ WordPress RESTFUL API Endpoint like: posts, users, products id: number, args?: { [key: string]: any }, // ↑ Check the WordPress RESTFUL API Endpoint args (url params) }WordPress Object
// get the post with id = 200 const getPost = await getResource({ resource: 'posts', id: 200, }){ resource: string, // ↑ WordPress RESTFUL API Endpoint like: posts, users, products args?: { [key: string]: any }, // ↑ Check the WordPress RESTFUL API Endpoint args (url params) }WordPress Object Array
// get the all posts that author_id = 1 const getPosts = await getResources({ resource: 'posts', args: { author: 1, }, }){ resource: string, // ↑ WordPress RESTFUL API Endpoint like: posts, users, products id: number, args?: { [key: string]: any }, // ↑ Check the WordPress RESTFUL API Endpoint args (url params) }Update Status
// update the title with post_id = 200 const updatePost = await updateResource({ resource: 'posts', id: 200, args: { title: 'Update Title by API', }, }){ resource: string, // ↑ WordPress RESTFUL API Endpoint like: posts, users, products id: number, }Delete Status
// delete the post with id = 200 const deletePost = await deleteResource({ resource: 'posts', id: 200, }){ resource: string // WordPress RESTFUL API Endpoint like: posts, users, products id: number queryOptions?: { // please visit React Query for more detail staleTime?: number cacheTime?: number refetchOnWindowFocus?: boolean refetchOnMount?: boolean refetchOnReconnect?: boolean refetchInterval?: number retry?: boolean | number retryDelay?: number enabled?: boolean } }WordPress Object
// get the post with id = 200 const post = useOne({ resource: 'posts', id: 200, }) // get the user with id = 1 const user = useOne({ resource: 'users', id: 1, }){ resource: string // WordPress RESTFUL API Endpoint like: posts, users, products args?: Record<string, any> // please visit WordPress RESTFUL API Handbook for more detail queryOptions?: { // please visit React Query for more detail staleTime?: number cacheTime?: number refetchOnWindowFocus?: boolean refetchOnMount?: boolean refetchOnReconnect?: boolean refetchInterval?: number retry?: boolean | number retryDelay?: number enabled?: boolean } }WordPress Object Array
// get all posts that author_id = 1 const posts = useMany({ resource: 'posts', args: { author: 1, }, })Welcome to open issue and start a discussion 🎉🎉🎉
If this project is useful for you, please give me a github star ⭐
Help Me to Build More Stunning Project 🤟
- Inspired by Vite for WP
- API design Inspired by Refine
- WordPress REST API Handbook