2

I need to put an include inside a shortcode. Can any one help me to do it, please?

Example:

echo do_shortcode( '[student]' . include 'incMac.php' . '[/student]' );
3
  • Calling a different shortcode would be safer. I take it your include returns something? If so, you'll need to use the right path to the file. So instead of just include 'incMac.php' you'll need to do something more like include '/public_html/wp-content/plugins/yourpluginname/incMac.php.
    – WebElaine
    Dec 12, 2019 at 22:00
  • Hi, im using a learndash plugin, they give us the shortcode [student], and inside the shortcode we what to put some data that is inside by calling the include, we check the include and work fine ,but when we try to put it inside the shortcode fails. Dec 12, 2019 at 22:10
  • we try to do something like this but return "1" in the echo: <?php $z = include 'attendance/includeAttendance.php'; echo do_shortcode( '[student]'. $z .'[/student]'); ?> Dec 12, 2019 at 22:14

1 Answer 1

5

You won't be able to concatenate an include as it doesn't return a string. What you could do is store that include content in a variable, and then concatenate.

ob_start();
include 'incMac.php';
$include_content = ob_get_clean();
echo do_shortcode( '[student]' . $include_content . '[/student]' );

ob_start tells php to store the output from the script in an internal buffer, instead of printing it.

ob_get_clean returns the content of the buffer at the moment and deletes it, turning off the output buffering. We store this content in the $include_content variable, which can be concatenated.

1
  • Well sir, thank you , it work like a charm!! i just add and echo to see it. ob_start(); include 'incMac.php'; $include_content = ob_get_clean(); echo do_shortcode( '[student]' . $include_content . '[/student]' ); thank you for your help Dec 13, 2019 at 1:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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