Integrate wpDiscuz Comments in Laravel Project

Approaches:

  1. Direct Integration via REST API:
    • Fetch comments using the WordPress REST API and display them in Laravel.
    • Post new comments from Laravel pages to the WordPress site.
  2. Database Sharing:
    • Directly query the WordPress database from Laravel to fetch and post comments.
  3. Iframe Embedding:
    • Embed the WordPress comment section on Laravel pages using iframes.

Recommended Approach: Direct Integration via REST API

Step-by-Step Solution:

1. Enable WordPress REST API

  1. Ensure the WordPress REST API is enabled (default in WordPress).
  2. Add custom endpoint in WordPress (if necessary).

Add to functions.php in WordPress:

phpCopy codeadd_action('rest_api_init', function () {
    register_rest_route('wpdiscuz/v1', '/comments', array(
        'methods' => 'GET',
        'callback' => 'get_wpdiscuz_comments',
    ));
});

function get_wpdiscuz_comments(WP_REST_Request $request) {
    $post_id = $request['post_id'];
    $comments = get_comments(array('post_id' => $post_id));
    return rest_ensure_response($comments);
}

2. Fetch Comments in Laravel

  1. Create a Service to Fetch Comments
phpCopy code// app/Services/WordPressCommentService.php

namespace App\Services;

use Illuminate\Support\Facades\Http;

class WordPressCommentService
{
    protected $baseUrl;

    public function __construct()
    {
        $this->baseUrl = 'https://yourwordpresssite.com/wp-json/wpdiscuz/v1/';
    }

    public function getComments($postId)
    {
        $response = Http::get($this->baseUrl . 'comments', ['post_id' => $postId]);
        return $response->json();
    }
}
  1. Create Controller Method to Display Comments
phpCopy code// app/Http/Controllers/CommentController.php

namespace App\Http\Controllers;

use App\Services\WordPressCommentService;
use Illuminate\Http\Request;

class CommentController extends Controller
{
    protected $commentService;

    public function __construct(WordPressCommentService $commentService)
    {
        $this->commentService = $commentService;
    }

    public function showComments($postId)
    {
        $comments = $this->commentService->getComments($postId);
        return view('comments.show', compact('comments'));
    }
}
  1. Create Blade Template to Display Comments
bladeCopy code<!-- resources/views/comments/show.blade.php -->

@foreach($comments as $comment)
    <div class="comment">
        <p><strong>{{ $comment['author_name'] }}</strong></p>
        <p>{{ $comment['content']['rendered'] }}</p>
    </div>
@endforeach
  1. Add Routes to Laravel
phpCopy code// routes/web.php

use App\Http\Controllers\CommentController;

Route::get('/comments/{postId}', [CommentController::class, 'showComments']);

3. Integrate wpDiscuz Comment Form

  1. Embed wpDiscuz Form Using an Iframe
bladeCopy code<!-- resources/views/comments/form.blade.php -->

<iframe src="https://yourwordpresssite.com/wpdiscuz-form-url?post_id={{ $postId }}" width="100%" height="400"></iframe>
  1. Ensure CORS Settings in WordPress

Modify wp-config.php to allow CORS:

phpCopy code// wp-config.php

header("Access-Control-Allow-Origin: *");

Conclusion

By following these steps, you can seamlessly integrate the wpDiscuz comment system from your WordPress site into your Laravel application, allowing you to display and manage comments across both platforms. This method ensures real-time synchronization and a unified user experience.