Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use WordPress plugins inside Laravel with Corcel? #617

Open
Kotov584 opened this issue Sep 5, 2022 · 4 comments
Open

How to use WordPress plugins inside Laravel with Corcel? #617

Kotov584 opened this issue Sep 5, 2022 · 4 comments

Comments

@Kotov584
Copy link

Kotov584 commented Sep 5, 2022

So the main my idea is to use installed WordPress plugins inside of Laravel. How I can do that? And if possible (I think not yet) to save visual page builder functionality.

@Kotov584
Copy link
Author

Kotov584 commented Sep 7, 2022

Dead community?

@FSElias
Copy link

FSElias commented Sep 7, 2022

As far as I know, this is not possible. Corcel is connecting to WordPress through database, not API interface

@Kotov584
Copy link
Author

Kotov584 commented Sep 7, 2022

So need just to somehow use WordPress api in Laravel

@josedarci
Copy link

To use WordPress plugins inside Laravel with Corcel, you can follow these steps:

  1. Install Corcel: Start by installing the Corcel package in your Laravel project using Composer. Run the following command in your terminal:

    composer require jgrossi/corcel
    
  2. Configure the Database Connection: Configure the database connection in your Laravel project to connect to the WordPress database. Open the config/database.php file and add a new connection configuration for the WordPress database. Here's an example configuration:

    'wordpress' => [
        'driver' => 'mysql',
        'host' => env('WP_DB_HOST', 'localhost'),
        'database' => env('WP_DB_DATABASE', 'wordpress'),
        'username' => env('WP_DB_USERNAME', 'root'),
        'password' => env('WP_DB_PASSWORD', ''),
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix' => 'wp_',
    ],

    Make sure to update the env values to match your WordPress database credentials.

  3. Create Model Classes: Create model classes in your Laravel project that extend the Corcel models. These model classes will allow you to interact with the WordPress database tables. For example, you can create a Post model like this:

    <?php
    
    namespace App\Models;
    
    use Corcel\Model\Post as Corcel;
    
    class Post extends Corcel
    {
        protected $connection = 'wordpress';
        protected $postType = 'post';
    }

    You can create additional model classes for other WordPress entities like Page, Category, Tag, etc.

  4. Use WordPress Plugins: Once you have set up the Corcel integration, you can use WordPress plugins in your Laravel project by leveraging the power of Corcel. You can interact with WordPress entities, such as posts, pages, categories, and tags, using the model classes you created. You can also access custom fields and metadata associated with WordPress entities.

    Here's an example of fetching posts from WordPress using Corcel:

    use App\Models\Post;
    
    $posts = Post::published()->take(5)->get();
    
    foreach ($posts as $post) {
        echo $post->title;
        // Access other attributes or methods of the post model
    }

    You can also use Eloquent relationships and query scopes with Corcel models to enhance your interactions with WordPress data.

By following these steps, you can effectively use WordPress plugins inside Laravel using Corcel. This integration allows you to leverage the functionality of WordPress while enjoying the development capabilities of Laravel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants