1
votes

why my shortcode of easytimetable always BEFORE all content? How can i display it where I want? I tried to change from echo,print to return but im not coding much so can u help me?

function easytimetable( $atts, $content = null ){
extract(shortcode_atts(array(
    'id' => 1
), $atts));
$id = (int)$id;
$nonce = wp_create_nonce('displayPlanning');
$content = do_shortcode($content);
require_once SYET_PATH . 'public/class-easy-timetable-public.php';
$display = Easy_Timetable_Public::syet_displayPlanning($id, $nonce, $content);
//var_dump($content);
return $display;

}
function register_easytimetable_shortcodes(){
   add_shortcode('easytimetable', 'easytimetable');
}
add_action( 'init', 'register_easytimetable_shortcodes');
1
I don't understand $content = do_shortcode($content); - Stender

1 Answers

2
votes

To make absolutely sure that the content is returned and not printed, use output buffers:

function easytimetable( $atts, $content = null ){
    ob_start();
    extract(shortcode_atts(array(
        'id' => 1
    ), $atts));
    $id = (int)$id;
    $nonce = wp_create_nonce('displayPlanning');
    $content = do_shortcode($content);
    require_once SYET_PATH . 'public/class-easy-timetable-public.php';
    $display = Easy_Timetable_Public::syet_displayPlanning($id, $nonce, $content);
    echo $display; // might be unnecessary
    //var_dump($content);
    return ob_get_clean();

}
function register_easytimetable_shortcodes(){
    add_shortcode('easytimetable', 'easytimetable');
}
add_action( 'init', 'register_easytimetable_shortcodes');

ob_start will instruct PHP to buffer (save) all the output instead of sending it to the browser directly. ob_get_clean() will get the content of that buffer, clean the buffer and turn off output buffering. If that's actually the code responsible for the early output you see, this should take care of it. echoing $display might be (is probably) not needed, but that really depends on whether Easy_Timetable_Public::syet_displayPlanning($id, $nonce, $content) only outputs content or does also return some (hopefully in the correct order).