I have a problem with my shortcode, it should appear in the post div but instead it is showed at the end of the page content(at the bottom of the page).
This is the code:

add_shortcode('registru', "show_registru");

function show_registru()
{
    global $wpdb;

    $list_inregistrari = $wpdb->get_results('SELECT * FROM ' . $wpdb->prefix . 'wprc_ong_casa ORDER BY data_inregistrare ASC');
    $sold_init = $wpdb->get_var('SELECT valoare FROM ' . $wpdb->prefix . 'wprc_solduri LIMIT 1');

    ob_start();
    print_table($list_inregistrari, $sold_init);
    return ob_get_clean();
}

If I try to use ob_get_content() instead of ob_get_clean() the table will be showed two times once in the post div and once at the bottom of the page.

I also tried doing this(and it is still at the bottom of the page):

ob_start();
print_table($list_inregistrari, $sold_init);
$return = ob_get_contents();
ob_clean();
return $return;

Here is the print_table function:

<?php
    //this creates the footer of the table
function front_table_footer($total_incasari, $total_plati, $sold_init){
    echo "<th>" . __('TOTAL', 'wpregistru-casa') . "</th>";
    echo "<th></th>";
    echo "<th></th>";
    echo "<th>" . $total_incasari . "</th>";
    echo "<th>" . $total_plati . "</th>";
    echo "<th>" . $sold_init . "</th>";

}

//this creates the table
function print_table($list_inregistrari, $sold_init)
{

?>
    <table class="table table-hover">

    <?php
        $curentmonth = 0;
        $curentyear = 0;
        $total_incasari = 0;
        $total_plati = 0;

        foreach ($list_inregistrari as $inregistrare):

            $month = date("m",strtotime($inregistrare->data_inregistrare));
            $year = date("Y",strtotime($inregistrare->data_inregistrare));

            if($month != $curentmonth):

                        //this shows the total for each month
                        if( $curentmonth != null){

                            front_table_footer($total_incasari, $total_plati, $sold_init);
                            $total_incasari = 0;
                            $total_plati = 0;
                        }
                        $monthdelta = (int)$month - (int)$curentmonth;
                        $yeardelta = (int)$year - (int)$curentyear;

                        //difference between the years
                        if($curentyear && $yeardelta > 0){
                            //difference between the months(for years)
                            $monthCount = (int)$month + (12-$curentmonth) + (($yeardelta-1)*12);


                            for($i = 1; $i < $monthCount; $i++){


                                if($curentmonth+$i > 12){
                                    $curentmonth = $curentmonth - 12;
                                    $curentyear++;
                                }

                                echo "<tr>";
                                echo "<th colspan="6" style="text-align: center; font-size: 14px">" . $curentyear . '-' . sprintf('%02d', $curentmonth+$i) . "</th>";
                                echo "</tr>";

                            }

                        }elseif($curentmonth && $monthdelta > 1){
                            for($i = 1; $i < $monthdelta; $i++){
                                echo "<tr>";
                                echo "<th colspan="6" style="text-align: center; font-size: 14px">" .$year. '-' . sprintf('%02d',$curentmonth+$i) . "</th>";
                                echo "</tr>";
                            }
                        }




                        echo "<tr>";
                        echo "<th colspan="6" style="text-align: center; font-size: 14px">" .$year. '-' . $month . "</th>";
                        echo "</tr>";


                        $curentyear = $year;
                        $curentmonth = $month;


    ?>
                        <tr style="text-transform: lowercase;">
                            <th><?php _e('Data Înregistrare','wpregistru-casa') ?></th>
                            <th><?php _e('Proiect','wpregistru-casa') ?></th>
                            <th><?php _e('Detalii Tranzacție','wpregistru-casa') ?></th>
                            <th><?php _e('Încasare','wpregistru-casa') ?></th>
                            <th><?php _e('Plată','wpregistru-casa') ?></th>
                            <th><?php _e('Sold','wpregistru-casa') ?></th>
                        </tr>

            <?php endif; ?>

            <tr>

    <?php
                if(!$inregistrare->inregistrare_interna){
    ?>
                    <td><?php echo $inregistrare->data_inregistrare ?></td>
                    <td><?php echo $inregistrare->proiect ?></td>
                    <td><?php echo $inregistrare->detalii_tranzactie ?></td>

    <?php                   
                    if($inregistrare->tip_inregistrare == __('INCASARE', 'wpregistru-casa')){
                        $total_incasari = $total_incasari + $inregistrare->suma;
                        echo "<td>" . $inregistrare->suma . "</td>";
                        echo "<td></td>";
                    }else{
                        $total_plati = $total_plati + $inregistrare->suma;
                        echo "<td></td>";
                        echo "<td>" . $inregistrare->suma. "</td>";
                    }
                    if($inregistrare->tip_inregistrare == __('INCASARE', 'wpregistru-casa')){

                        $sold_init = $sold_init + $inregistrare->suma;
                        echo "<td>" . $sold_init . "</td>";

                    }else{
                        $sold_init = $sold_init - $inregistrare->suma;
                        echo "<td>" . $sold_init . "</td>";
                    }
                }
    ?>

            </tr>
    <?php

        endforeach;

        front_table_footer($total_incasari, $total_plati, $sold_init);
}

Read more here: shortcode content is showed twice or at the bottom of the page


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: