• Resolved andrewf12

    (@andrewf12)


    Hi,

    Does anyone know if it’s possible to create a shortcode for the following function: <?php comments_template(); ?> ?

    I would add this code to a shortcode plugin or something if that’s possible.

    Thanks everyone for your help!

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Sorry, no. We define shortcodes with add_shortcode(), passing as an argument a function name to execute when the shortcode is encountered. But it cannot be any function, it must be a function that returns a single string to be output in place of the shortcode.

    comments_template() does not return a single string, it includes another file in the current template. It would be conceivable to rewrite template output as a shortcode handler function, if you had a specific template file. But to handle any particular theme’s comment template you would not know what to rewrite.

    Just an idea, completely untested: It may be possible to capture template file output using PHP’s output buffer, then assign the buffer contents to a variable. Your shortcode handler function would then return that variable. Not as simple as just calling comment_template(), but making something that works might be possible.

Viewing 1 replies (of 1 total)

The topic ‘how to create shortcode for php function?’ is closed to new replies.