PHP code example of eslamfaroug / laravel-wordpress-sync

1. Go to this page and download the library: Download eslamfaroug/laravel-wordpress-sync 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/ */

    

eslamfaroug / laravel-wordpress-sync example snippets


namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use EslamFaroug\LaravelWordpressSync\SyncsWithWordpress;
use EslamFaroug\LaravelWordpressSync\WordpressSyncInterface;

class Post extends Model implements WordpressSyncInterface
{
    use HasFactory, SyncsWithWordpress;

    protected $fillable = [
        'id', 'type', 'lang', 'title', 'content', 'excerpt', 'publish', 'user_id', 'views',
        'created_at', 'updated_at'
    ];
    
    /**
     * Define the fields to be sent to WordPress
     */
    public function getWordpressFieldsMapping()
    {
        return [
            'title'   => 'title',       // The 'title' field in WordPress maps to the 'title' field in the model
            'content' => 'content',     // The 'content' field in WordPress maps to the 'content' field in the model
            'excerpt' => 'excerpt',     // The 'excerpt' field in WordPress maps to the 'excerpt' field in the model
            'author'  => 'user_id',     // The 'author' field in WordPress maps to the 'user_id' field in the model
        ];
    }

    /**
     * Define the condition for syncing with WordPress
     */
    public function shouldSyncWithWordpress()
    {
        // Sync only if 'publish' is 'true' and the view count is greater than 100
        return $this->publish === 'true' && $this->views > 100;
    }

    /**
     * Define the name of the status field in the model
     */
    public function getStatusField()
    {
        return 'publish'; // The status field in the model is named 'publish'
    }
}
bash
php artisan vendor:publish --tag=migrations
bash
php artisan migrate