{"id":922,"date":"2024-07-24T13:19:30","date_gmt":"2024-07-24T13:19:30","guid":{"rendered":"https:\/\/www.cmsgalaxy.com\/blog\/?p=922"},"modified":"2024-07-24T13:19:32","modified_gmt":"2024-07-24T13:19:32","slug":"integrate-wpdiscuz-comments-in-laravel-project","status":"publish","type":"post","link":"https:\/\/www.cmsgalaxy.com\/blog\/integrate-wpdiscuz-comments-in-laravel-project\/","title":{"rendered":"Integrate wpDiscuz Comments in Laravel Project"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Step-by-Step Instructions<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. Set Up the WordPress REST API<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Ensure that the WordPress REST API is enabled. WordPress has the REST API enabled by default.<\/li>\n\n\n\n<li>Verify that the wpDiscuz plugin supports REST API endpoints for comments.<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">2. Create a Custom Endpoint in WordPress (if necessary)<\/h4>\n\n\n\n<p>Add the following code to your WordPress theme\u2019s <code>functions.php<\/code> or create a custom plugin to add a custom REST API endpoint:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">phpCopy code<code>\/\/ functions.php\n\nadd_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   \n<\/code><\/pre>\n\n\n\n<p>4o<\/p>\n\n\n\n<p>I meant everything must be in of COde Pane which can be embeded in MDChatGPT<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Integrate wpDiscuz Comments in Laravel Project<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. Set Up the WordPress REST API<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Ensure the WordPress REST API is enabled and wpDiscuz plugin supports REST API for comments.<\/li>\n\n\n\n<li><strong>Add Custom Endpoint in WordPress (if necessary)<\/strong><\/li>\n<\/ol>\n\n\n\n<p>Add the following code to <code>functions.php<\/code> or create a custom plugin:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">phpCopy code<code>\/\/ functions.php\n\nadd_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<p>Create <code>WordPressCommentService.php<\/code>:<\/p>\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<p>Create <code>CommentController.php<\/code>:<\/p>\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<p>Create <code>show.blade.php<\/code>:<\/p>\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<p>Add the following route in <code>web.php<\/code>:<\/p>\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<p>Create <code>form.blade.php<\/code>:<\/p>\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>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>By following these steps, you can integrate the wpDiscuz comment system from your WordPress site into your Laravel application, allowing you to display and manage comments seamlessly across both platforms.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Step-by-Step Instructions 1. Set Up the WordPress REST API 2. Create a Custom Endpoint in WordPress (if necessary) Add the<\/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-922","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts\/922","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=922"}],"version-history":[{"count":1,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts\/922\/revisions"}],"predecessor-version":[{"id":923,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts\/922\/revisions\/923"}],"wp:attachment":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/media?parent=922"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/categories?post=922"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/tags?post=922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}