1. Go to this page and download the library: Download wirefront/wplitecore library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
wirefront / wplitecore example snippets
WPLite\WPLiteCore;
// Option 1: With JWT authentication (for protected content)
$wpLite = WPLiteCore::create(
'https://your-wordpress-site.com/wp-json/wp/v2',
'your-secret-key'
);
// Option 2: Without JWT authentication (for public content)
$wpLite = WPLiteCore::create(
'https://your-wordpress-site.com/wp-json/wp/v2',
null // No secret key needed for public APIs
);
// Get posts (works with or without JWT)
$posts = $wpLite->posts()->getPosts(['per_page' => 5]);
if ($posts->isSuccess()) {
foreach ($posts->getItems() as $post) {
echo $post['title']['rendered'] . "\n";
}
}
// Get a single post with featured image
$post = $wpLite->posts()->getPostById(32, 'medium');
if ($post->isSuccess()) {
$postData = $post->getItems();
echo $postData['title']['rendered'] . "\n";
// Featured image is automatically retrieved
if ($postData['featured_image']) {
echo '<img src="' . $postData['featured_image'] . '" alt="Featured">';
}
}
// You can still use the original procedural functions
$result = wlc_single_post([
'key' => 'your-secret-key',
'api_url' => 'https://your-site.com/wp-json/wp/v2/',
'id' => 32,
'type' => 'posts',
'media_size' => 'medium'
]);
if ($result && isset($result['title'])) {
echo "Title: " . $result['title'] . "\n";
if (isset($result['featured_image']['url'])) {
echo "Featured Image: " . $result['featured_image']['url'] . "\n";
}
}
// Include cached router for high-performance routing
=> true, 'default_ttl' => 3600]);
// Standard route (no caching)
get('/api/posts', function() {
$wpLite = \WPLite\WPLiteCore::getInstance();
return $wpLite->posts()->getAll(); // ~200ms every request
});
// Cached route (massive performance improvement)
cached_get('/api/posts', function() {
$wpLite = \WPLite\WPLiteCore::getInstance();
return $wpLite->posts()->getAll(); // ~200ms first request, ~2ms subsequent requests
}, ['ttl' => 1800]); // Cache for 30 minutes
// WordPress API with automatic caching
wp_api_cached_route('/api/posts/$id', function($id) {
$wpLite = \WPLite\WPLiteCore::getInstance();
return $wpLite->posts()->getById($id);
}, ['ttl' => 3600, 'vary_by_params' => true]);
use WPLite\Core\Config;
Config::load(); // Load from .env file
$apiUrl = Config::getApiUrl(); // Get configured API URL
$hashKey = Config::getHashKey(); // Get configured hash key
$testPostId = Config::get('test_post_id', 32); // Get test post ID
// End users just pass values directly - no Config needed
$wpLite = WPLiteCore::create('https://your-site.com/wp-json/wp/v2', 'your-key');
$wpLite = WPLiteCore::create('https://your-site.com/wp-json/wp/v2', 'your-key');
// Works perfectly! No configuration files needed.
// If no WPLITE_HASH_KEY is set in .env:
$this->markTestSkipped('No API key configured. Please set WPLITE_HASH_KEY environment variable.');
// If Config::getHashKey() is called without setup:
throw new RuntimeException('HASH_KEY is not configured. Please set WPLITE_HASH_KEY environment variable.');
// End User (Simple):
$wpLite = WPLiteCore::create($apiUrl, $secretKey);
// Developer (With Config):
Config::load();
$wpLite = WPLiteCore::create(Config::getApiUrl(), Config::getHashKey());
$posts = $wpLite->posts();
// Get multiple posts
$response = $posts->getPosts(['per_page' => 10]);
// Get single post by ID
$response = $posts->getPostById(32, 'medium');
// Get single post by slug
$response = $posts->getPostBySlug('my-post-slug');
// Search posts
$response = $posts->searchPosts('search term');
if ($response->isSuccess()) {
$items = $response->getItems(); // Get response data
$total = $response->getTotalPosts(); // Get total count
$pages = $response->getTotalPages(); // Get page count
}
// Convert to legacy format
$legacyArray = $response->toArray();
$result = wlc_single_post([
'key' => 'your-secret-key',
'api_url' => 'https://your-site.com/wp-json/wp/v2/',
'id' => 32, // Post ID
'type' => 'posts', // Post type
'media_size' => 'medium' // Featured image size
]);