π Introduction
WordPress is the most popular content management system (CMS) worldwide, powering millions of websites. However, sometimes developers need to display WordPress content within a custom PHP website. This integration allows users to fetch and show WordPress posts, pages, or other content dynamically. In this blog, we will explore different methods to achieve this, whether WordPress is installed on the same server or hosted separately.
π οΈ Methods to Display WordPress Content in a PHP Website
1οΈβ£ Using WordPress Database in a Custom PHP Website
If your PHP website has access to the WordPress database, you can directly fetch posts and display them.
πΉ Steps to Connect to the WordPress Database:
β Connect to the Database: You need to establish a connection to your WordPress database. β Query the Posts Table: Use SQL queries to retrieve published posts. β Display the Content: Fetch and format the content for display.
π Example Code:
<?php
$conn = new mysqli("localhost", "db_user", "db_password", "wordpress_db");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT post_title, post_content FROM wp_posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date DESC LIMIT 5";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "<h2>" . $row["post_title"] . "</h2>";
echo "<p>" . substr(strip_tags($row["post_content"]), 0, 200) . "...</p>";
}
} else {
echo "No posts found.";
}
$conn->close();
?>
This method is useful if you have direct access to the WordPress database but does not support shortcodes or themes.
2οΈβ£ Embedding WordPress in Your PHP Website
If your WordPress installation is on the same server, you can load WordPress functions directly.
πΉ Steps to Embed WordPress:
β
Include wp-load.php: This file gives access to WordPress functions. β
Fetch Post Data: Use get_post()
to retrieve the required content. β
Apply Filters for Formatting: Ensure shortcodes and formatting are applied properly.
π Example Code:
<?php
define('WP_USE_THEMES', false);
require('/path-to-wordpress/wp-load.php');
$post_id = 1; // Change to the desired post ID
$post = get_post($post_id);
echo "<h2>{$post->post_title}</h2>";
echo apply_filters('the_content', $post->post_content);
?>
This method is effective when you want full WordPress functionality, including shortcodes and theme styles.
3οΈβ£ Using the WordPress REST API (For Remote WordPress Sites)
If WordPress is installed on another server, you can fetch content using the WordPress REST API.
πΉ Steps to Use the REST API:
β Make a GET Request: Fetch the latest posts using the WordPress REST API. β Decode JSON Response: Convert API response into a readable format. β Display the Content: Extract post title and content.
π Example Code:
<?php
$json = file_get_contents('https://your-wordpress-site.com/wp-json/wp/v2/posts?per_page=5');
$posts = json_decode($json);
foreach ($posts as $post) {
echo "<h2>{$post->title->rendered}</h2>";
echo "<p>" . substr(strip_tags($post->content->rendered), 0, 200) . "...</p>";
}
?>
Alternatively, you can use cURL
for better performance:
<?php
$ch = curl_init('https://your-wordpress-site.com/wp-json/wp/v2/posts?per_page=5');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
curl_close($ch);
$posts = json_decode($json);
foreach ($posts as $post) {
echo "<h2>{$post->title->rendered}</h2>";
echo "<p>" . substr(strip_tags($post->content->rendered), 0, 200) . "...</p>";
}
?>
This method is ideal for displaying WordPress content on a separate PHP website without direct database access.
π§ Additional Tips
β Formatting the Content Properly
When retrieving content, ensure that WordPress filters are applied for proper formatting:
echo apply_filters('the_content', $post->post_content);
β Handling WordPress Shortcodes
If a post contains shortcodes, process them using:
echo do_shortcode($post->post_content);
β Displaying Featured Images
You can fetch and display the featured image of a post using:
$thumbnail = get_the_post_thumbnail_url($post->ID);
echo "<img src='{$thumbnail}' alt='Featured Image'>";
For the REST API method, featured images can be accessed from the _embedded
field if enabled.
π Summary
Integrating WordPress content into a PHP website can be done using: β Direct Database Queries β Fast but lacks WordPress features. β Embedding wp-load.php β Provides full WordPress functionality. β Using the REST API β Best for external WordPress installations.
Each method has its use cases depending on your project requirements. If your PHP website and WordPress installation are on the same server, wp-load.php
is a powerful option. If they are separate, the REST API is the best choice.
By following these methods, you can dynamically display WordPress posts, pages, or even custom post types on your PHP site. π