React hooks and components to asynchronously fetch and display WordPress posts using its most updated API
Installations
npm install wordpress-posts-react
Developer Guide
Typescript
No
Module System
CommonJS
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
validate.email š
Verify real, reachable, and deliverable emails with instant MX records, SMTP checks, and disposable email detection.
Developer
supertypeai
Download Statistics
Total Downloads
183
Last Day
1
Last Week
1
Last Month
6
Last Year
63
GitHub Statistics
1 Stars
19 Commits
1 Forks
1 Watchers
1 Branches
1 Contributors
Updated on Feb 21, 2023
Bundle Size
2.05 kB
Minified
1.04 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.4.0
Package Id
wordpress-posts-react@1.4.0
Unpacked Size
16.70 kB
Size
6.21 kB
File Count
6
Published on
Mar 12, 2023
Total Downloads
Cumulative downloads
Total Downloads
183
Last Day
0%
1
Compared to previous day
Last Week
0%
1
Compared to previous week
Last Month
-45.5%
6
Compared to previous month
Last Year
-47.5%
63
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
WordPress-Posts-React
A lightweight (<3 kb minified!) set of React hooks and components to asynchronously fetch and display WordPress posts using WordPress's most updated REST API.
Why?
Almost all of the other WordPress React libraries I found were either too bloated or too old and unmaintained, which uses outdated mechanisms to retrieve blog articles off WordPress sites. This library is designed to be as lightweight as possible (no dependencies), and provides drop-in components to display posts in a variety of ways directly in your React app.
Installation
You can install the package using yarn
or npm
:
1yarn add wordpress-posts-react 2# or 3npm install wordpress-posts-react
This will install wordpress-posts-react
from the npm registry and add it to your package.json
file.
Usage
Common Patterns
The most common pattern is to use the useWordPressFeed
hook to fetch posts from a WordPress site, and then use the WordPressBlogroll
component to render the posts as a blogroll. The following snippet demonstrates that:
1import { WordPressBlogroll, useWordPressFeed } from 'wordpress-posts-react' 2 3const Blogroll = () => { 4 5 const { feed, loading } = useWordPressFeed('https://supertype.ai') 6 7 if(!loading){ 8 return ( 9 <main> 10 <div> 11 <h1>{feed.length} Articles</h1> 12 <WordPressBlogroll feed={feed} /> 13 </div> 14 </main> 15 )} 16 else{ 17 return <div>Loading...</div> 18 } 19}
This will fetch the latest 10 posts from the WordPress site at https://supertype.ai
and render a beautifully formatted blogroll in your React or Next.js app.
It uses WordPress' officially documented REST API to fetch the posts, so it will always be up to date with the latest WordPress features, returns JSON data (yay! no XML!), ā” fast, and with almost no š§ maintenance required. Weighing at less than 10kb, it's also šŖ¶ featherweight, with no dependencies.
- š Beautifully formatted blogroll
- š Human-friendly dates (e.g. "2 days ago")
- šØ Uses
async
andawait
to fetch posts asynchronously - š Uses WordPress's official REST API to fetch posts
- š¦ No dependencies (Less than 10kb in size)
Hooks and Components
useWordPressFeed(site_url, author_id, number_of_posts = 10)
is a React hook that asynchronously fetches posts from a WordPress site and returns the posts as an array of objects. Only its first argument is required: the URL of the WordPress site you want to fetch posts from.
author_id
: If specified, this narrows down to WordPress posts of the author you want to fetch posts from. If you don't pass in anauthor_id
, it will fetch posts from all authors.number_of_posts
: The number of posts you want to fetch. Defaults to 10.
š” Pro tip: To confirm that the site is in fact a WordPress site, you can append /wp-json
to your site (e.g <your-site>/wp-json/
) and see if it returns a JSON object. If it does, then you can be sure that the site is a WordPress site. This is the same mechanism that wordpress-posts-react
uses to fetch and render the Wordpress posts as an isolated React component.
1import { useWordPressFeed } from 'wordpress-posts-react' 2 3// fetches author id=23's latest 10 posts from https://supertype.ai 4useWordPressFeed('https://supertype.ai', 23) 5 6// fetches author id=17's latest 5 posts from https://supertype.ai 7useWordPressFeed('https://supertype.ai', 17, 5)
WordPressBlogroll({ feed })
is a React component that renders a blogroll of posts. It takes in a feed
prop, which is an array of objects that contain the post's title, date, excerpt, and link. Usually used in conjunction with useWordPressFeed
to fetch posts from a WordPress site.
1import { useWordPressFeed, WordPressBlogroll } from 'wordpress-posts-react' 2 3const { feed, loading } = useWordPressFeed('https://supertype.ai') 4{ 5 !loading && 6 <WordPressBlogroll feed={feed} /> 7}
There are also a few utility functions that you can use to format dates, parse HTML and decode HTML entities. You are not required to directly use them in your React project, as they are used internally by wordpress-posts-react
to format the posts. However, they are exported so you can use them elsewhere in your own React application or roll your own blog components (e.g <YourOwnCustomBlogRoll />
) after cleaning the returned json data using these utility functions.
1import { useWordPressFeed, timeAgo, 2 parseUrlString, decodeHtml } 3from 'wordpress-posts-react' 4 5const BlogComponent = ({url}) => { 6 const { wp_data, loading } = useWordPressFeed(url) 7 const [feed, setFeed] = useState([]); 8 9 useEffect(() => { 10 if(wp_data && !loading){ 11 const posts = wp_data.map((post) => { 12 const { title, link, date, excerpt } = post; 13 return { 14 id: post.id, 15 title: decodeHtml(title.rendered), 16 link: parseUrlString(link), 17 date: timeAgo(date), 18 excerpt: decodeHtml(excerpt.rendered), 19 // ...other custom attributes post-cleansing 20 }; 21 }); 22 setFeed(posts); 23 } 24 }, [url]); 25 26 return <YourOwnCustomBlogRoll feed={feed} /> 27 28}
Development
This package uses Rollup to bundle the library. To build it, run with either:
1yarn build 2# or 3npm run build
To do this in watch mode, run:
1yarn start 2# or 3npm run start
Please feel free to open an issue or submit a pull request if you have any questions or suggestions!

No vulnerabilities found.

No security vulnerabilities found.
Other packages similar to wordpress-posts-react
react-native-wordpress-editor
React Native Wrapper for WordPress Rich Text Editor. The WordPress-Editor is the text editor used in the official WordPress mobile apps to create and edit pages & posts
react-wordpress-post
Transform your Wordpress Posts in to React components when using the Wordpress REST API
@micyo/react-wp-query
WordPress REST API React implementations
select-posts-dropdown
A react component for listing and selecting wordpress posts using wp-api