Search code examples
phpwordpressshortcode

shortcode content is showed twice or at the bottom of the page


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;

*The print_table function just echoes some HTML code, so I also tried to put all the strings with HTML code into a single variable and then return it, but still nothing.


Solution

  • Got it! So the problem was your element in print_table was missing an ending </table> tag.

    Try this for your function (note the 2nd to last line):

            </tr>
        <?php
    
            endforeach;
    
            front_table_footer($total_incasari, $total_plati, $sold_init);
    
            echo '</table>'; // needs a closing tag
    }