PHP code example of mrdulal / laravel-wp-connector

1. Go to this page and download the library: Download mrdulal/laravel-wp-connector 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/ */

    

mrdulal / laravel-wp-connector example snippets


return [
    'enabled' => true,
    'host' => env('WP_DB_HOST', '127.0.0.1'),
    'port' => env('WP_DB_PORT', '3306'),
    'database' => env('WP_DB_DATABASE', 'wordpress'),
    'username' => env('WP_DB_USERNAME', 'root'),
    'password' => env('WP_DB_PASSWORD', ''),
    'prefix' => env('WP_DB_PREFIX', 'wp_'),
];

use MrDulal\WpConnector\Facades\Wp;

// Get recent published posts with authors
$posts = Wp::posts()->published()->with('author')->latest()->take(5)->get();

// Get posts by category
$categoryPosts = Wp::postsByCategory('Technology');

// Get posts by tag
$tagPosts = Wp::postsByTag('Laravel');

// Get user count
$userCount = Wp::userCount();

// Get comment count
$commentCount = Wp::commentCount();

use MrDulal\WpConnector\Models\WpPost;
use MrDulal\WpConnector\Models\WpUser;
use MrDulal\WpConnector\Models\WpComment;

// Get a post with its author and comments
$post = WpPost::with(['author', 'comments'])->find(1);

// Get user's posts
$user = WpUser::find(1);
$userPosts = $user->posts()->published()->get();

// Get post comments
$post = WpPost::find(1);
$comments = $post->comments()->approved()->get();

// Get post terms (categories/tags)
$post = WpPost::find(1);
$categories = $post->terms()->categories()->get();
$tags = $post->terms()->tags()->get();

// Get post meta
$post = WpPost::find(1);
$featuredImage = $post->getMeta('_thumbnail_id');
$customField = $post->getMeta('custom_field', 'default_value');

// Set post meta
$post->setMeta('custom_field', 'custom_value');

// Get user meta
$user = WpUser::find(1);
$firstName = $user->getMeta('first_name');
$lastName = $user->getMeta('last_name');

use MrDulal\WpConnector\Facades\Wp;

// Get WordPress options
$siteName = Wp::option('blogname');
$siteDescription = Wp::option('blogdescription');

// Set WordPress options
Wp::setOption('custom_option', 'custom_value');

'filament' => [
    'navigation_group' => 'WordPress',
    'navigation_sort' => 1,
    'enable_widgets' => true,
],

// Get posts with specific meta
$posts = Wp::posts()
    ->whereHas('meta', function ($query) {
        $query->where('meta_key', 'featured')
              ->where('meta_value', '1');
    })
    ->published()
    ->get();

// Get users with specific capabilities
$admins = Wp::users()
    ->whereHas('meta', function ($query) {
        $query->where('meta_key', 'wp_capabilities')
              ->where('meta_value', 'like', '%administrator%');
    })
    ->get();

// Eager load relationships
$posts = Wp::posts()
    ->with(['author', 'comments', 'terms'])
    ->published()
    ->get();

// Load specific relationships
$post = WpPost::with(['author.meta', 'comments.user'])->find(1);
bash
php artisan vendor:publish --tag=wp-connector-config