I have created a child theme, and I am trying to add a shortcode to its functions.php file, by doing this:
// functions.php of child theme
<?php
include(WP_CONTENT_DIR . '/graduates_functions.php');
?>
// graduates_functions.php
<?php
// [bartag foo="foo-value"]
function sayhi_func( $atts ) {
extract( shortcode_atts( array(
'graduate_type' => 'seo',
'bar' => 'something else',
), $atts ) );
echo 'test';
}
add_shortcode( 'sayhi', 'sayhi_func' );
?>
But when doing [sayhi] from a page I don’t see the echo, I see [sayhi]. It works if I add the same in the functions.php of the theme itself, not the child.
Any thoughts?
Shortcodes don’t ‘echo’, they need to ‘return’ something. So I guess it will work if you put your functions content between ob_start and ob_get_clean so you return everything inbetween:
function yourfunction(){ ob_start(); /* insert here the code of your function */ $output = ob_get_clean(); return $output; }