user1159265
user1159265

Reputation: 27

WP php include with shortcode is always on top

I want to include a table into a wordpress page. The table includes some php codes as well so want to try doing this with shortcodes.

I do not want to make a page template for this because I want to easily edit the text (in the WP page editor) above and below the included table.

The problem is that if I include the php file containing the table, the table will always be on top of the page instead in the middle of the article. How can I fix this?

function include_scams_page_template()
{
    $path = get_template_directory() . '/templates/scams.php';
    $output = include $path;
    return $output ;
}
add_shortcode ('include_scams_page_template' , 'include_scams_page_template');

Upvotes: 0

Views: 226

Answers (1)

Andy Tschiersch
Andy Tschiersch

Reputation: 3816

Try this:

function include_scams_page_template()
{
    $path = get_template_directory() . '/templates/scams.php';
    ob_start();
    include $path;
    $output = ob_get_clean();
    return $output ;
}
add_shortcode ('include_scams_page_template' , 'include_scams_page_template');

Explanation:

I guess your code in scams.php produced output that comes to early. ob_start() turn's on output buffering, and ob_get_clean() ends it and returns the buffer-content.

Upvotes: 1

Related Questions