I want to add a new shortcode with this function in wordpress with flatsome theme and show comment box in a desired location in accardion, but after add this shortcode to page, comment box show in top of the page instead in accardion:
add_shortcode('my_comment_box', 'my_comment_box');
function my_comment_box() {
$commentbox=comment_form();
echo $commentbox;
}
I tested this code with return command instead of echo, But the problem was not solved
what is wrong?
My Answer:
The issue may be related to the way the comment_form() function is being called within the shortcode.
Instead of directly echoing the comment form, try returning the output of the comment_form() function within the shortcode function.
Here is an updated version of your shortcode function:
php
add_shortcode('my_comment_box', 'my_comment_box');
function my_comment_box() {
ob_start();
comment_form();
return ob_get_clean();
}
By using ob_start() and ob_get_clean(), you can capture the output of the comment_form() function and return it as a string within the shortcode. This should help ensure that the comment box is displayed in the desired location within the accordion.
Make sure to place the shortcode within the desired location in the accordion in your WordPress page editor.
Rate this post
4 of 5 based on 9719 votesComments