{"id":924,"date":"2024-07-24T13:21:44","date_gmt":"2024-07-24T13:21:44","guid":{"rendered":"https:\/\/www.cmsgalaxy.com\/blog\/?p=924"},"modified":"2024-07-24T13:21:47","modified_gmt":"2024-07-24T13:21:47","slug":"integrate-wpdiscuz-comments-in-laravel-project-2","status":"publish","type":"post","link":"https:\/\/www.cmsgalaxy.com\/blog\/integrate-wpdiscuz-comments-in-laravel-project-2\/","title":{"rendered":"Integrate wpDiscuz Comments in Laravel Project"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Approaches:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Direct Integration via REST API:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Fetch comments using the WordPress REST API and display them in Laravel.<\/li>\n\n\n\n<li>Post new comments from Laravel pages to the WordPress site.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Database Sharing:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Directly query the WordPress database from Laravel to fetch and post comments.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Iframe Embedding:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Embed the WordPress comment section on Laravel pages using iframes.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Recommended Approach: Direct Integration via REST API<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">Step-by-Step Solution:<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. Enable WordPress REST API<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Ensure the WordPress REST API is enabled<\/strong> (default in WordPress).<\/li>\n\n\n\n<li><strong>Add custom endpoint in WordPress<\/strong> (if necessary).<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Add to <code>functions.php<\/code> in WordPress:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">phpCopy code<code>add_action('rest_api_init', function () {\n    register_rest_route('wpdiscuz\/v1', '\/comments', array(\n        'methods' =&gt; 'GET',\n        'callback' =&gt; 'get_wpdiscuz_comments',\n    ));\n});\n\nfunction get_wpdiscuz_comments(WP_REST_Request $request) {\n    $post_id = $request['post_id'];\n    $comments = get_comments(array('post_id' =&gt; $post_id));\n    return rest_ensure_response($comments);\n}\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2. Fetch Comments in Laravel<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Create a Service to Fetch Comments<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">phpCopy code<code>\/\/ app\/Services\/WordPressCommentService.php\n\nnamespace App\\Services;\n\nuse Illuminate\\Support\\Facades\\Http;\n\nclass WordPressCommentService\n{\n    protected $baseUrl;\n\n    public function __construct()\n    {\n        $this-&gt;baseUrl = 'https:\/\/yourwordpresssite.com\/wp-json\/wpdiscuz\/v1\/';\n    }\n\n    public function getComments($postId)\n    {\n        $response = Http::get($this-&gt;baseUrl . 'comments', ['post_id' =&gt; $postId]);\n        return $response-&gt;json();\n    }\n}\n<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><strong>Create Controller Method to Display Comments<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">phpCopy code<code>\/\/ app\/Http\/Controllers\/CommentController.php\n\nnamespace App\\Http\\Controllers;\n\nuse App\\Services\\WordPressCommentService;\nuse Illuminate\\Http\\Request;\n\nclass CommentController extends Controller\n{\n    protected $commentService;\n\n    public function __construct(WordPressCommentService $commentService)\n    {\n        $this-&gt;commentService = $commentService;\n    }\n\n    public function showComments($postId)\n    {\n        $comments = $this-&gt;commentService-&gt;getComments($postId);\n        return view('comments.show', compact('comments'));\n    }\n}\n<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li><strong>Create Blade Template to Display Comments<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">bladeCopy code<code>&lt;!-- resources\/views\/comments\/show.blade.php --&gt;\n\n@foreach($comments as $comment)\n    &lt;div class=\"comment\"&gt;\n        &lt;p&gt;&lt;strong&gt;{{ $comment['author_name'] }}&lt;\/strong&gt;&lt;\/p&gt;\n        &lt;p&gt;{{ $comment['content']['rendered'] }}&lt;\/p&gt;\n    &lt;\/div&gt;\n@endforeach\n<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\">\n<li><strong>Add Routes to Laravel<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">phpCopy code<code>\/\/ routes\/web.php\n\nuse App\\Http\\Controllers\\CommentController;\n\nRoute::get('\/comments\/{postId}', [CommentController::class, 'showComments']);\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3. Integrate wpDiscuz Comment Form<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Embed wpDiscuz Form Using an Iframe<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">bladeCopy code<code>&lt;!-- resources\/views\/comments\/form.blade.php --&gt;\n\n&lt;iframe src=\"https:\/\/yourwordpresssite.com\/wpdiscuz-form-url?post_id={{ $postId }}\" width=\"100%\" height=\"400\"&gt;&lt;\/iframe&gt;\n<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><strong>Ensure CORS Settings in WordPress<\/strong><\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Modify <code>wp-config.php<\/code> to allow CORS:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">phpCopy code<code>\/\/ wp-config.php\n\nheader(\"Access-Control-Allow-Origin: *\");\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Approaches: Recommended Approach: Direct Integration via REST API Step-by-Step Solution: 1. Enable WordPress REST API Add to functions.php in WordPress: phpCopy codeadd_action(&#8216;rest_api_init&#8217;, function () { register_rest_route(&#8216;wpdiscuz\/v1&#8217;, &#8216;\/comments&#8217;,&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-924","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts\/924","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/comments?post=924"}],"version-history":[{"count":1,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts\/924\/revisions"}],"predecessor-version":[{"id":925,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts\/924\/revisions\/925"}],"wp:attachment":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/media?parent=924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/categories?post=924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/tags?post=924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}