0

Everything is working fine and it's displaying the comments of the current page with this shortcode [wpdiscuz_comments] using the code below.

function my_wpdiscuz_shortcode() {
$html = "";
if (file_exists(ABSPATH . "wp-content/plugins/wpdiscuz/themes/default/comment-form.php")) {
ob_start();
include_once ABSPATH . "wp-content/plugins/wpdiscuz/themes/default/comment-form.php";
$html = ob_get_clean();
}
return $html;
}
add_shortcode("wpdiscuz_comments", "my_wpdiscuz_shortcode");

I'm trying to pass id parameter using the shortcode below. But it's not working.

[wpdiscuz_comments post_id="22"]

Where "post_id" parameter fetching the comments of only post id 22. So I can fetch the comments of a specific post.

Can anyone please help me with this or let me know how can I pass the parameters with shortcode using the above code? Thanks!

0

As the docs for add_shortcode() state,

Every shortcode callback is passed three parameters by default, including an array of attributes ($atts), the shortcode content or null if not set ($content), and finally the shortcode tag itself ($shortcode_tag), in that order.

Use the shortcode_atts() to define what attributes are allowed and set default values for those attributes.

function my_wpdiscuz_shortcode( $_atts ) {
    $defaults = array(
        'post_id' => '',
    );
    $atts = shortcode_atts( $defaults, $_atts );
    // Confirm that $post_id is an integer.
    $atts['post_id'] = absint( $atts['post_id'] );
    // ----
    // Now you can use $atts['post_id'] - it will contain
    // the integer value of the post_id set in the shortcode, or
    // 0 if nothing is set in the shortcode.
    // ----
    $html = "";
    if (file_exists(ABSPATH . "wp-content/plugins/wpdiscuz/themes/default/comment-form.php")) {
        ob_start();
        include_once ABSPATH . "wp-content/plugins/wpdiscuz/themes/default/comment-form.php";
        $html = ob_get_clean();
    }
    return $html;
}
add_shortcode("wpdiscuz_comments", "my_wpdiscuz_shortcode");

Note that, if post_id isn't set in your shortcode call, it will default to 0.

4
  • Hi Pat, Thank you so much for your quick reply. I've tried it with the above code but still it's not working. [wpdiscuz_comments post_id="1"] – HNPSK Feb 22 at 2:04
  • It's still showing the current post comments instead of the post id I'm inserting. – HNPSK Feb 22 at 2:06
  • My code doesn't make use of the $atts['post_id'] anywhere. You'd have to pass that value to whatever you're using to display the comments. Currently it appears you're loading the theme's comments-form.php file; you might want to look at using something like get_comments( array( 'post_id' => $atts['post_id'] ) ) instead. – Pat J Feb 22 at 2:19
  • Okay, thanks. I'm currently using the plugin to display form & comments called Wpdiscuz. And I'm adding this code in the functions.php file. – HNPSK Feb 22 at 2:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.