0

I'm currently developing a wordpress plugin. I created a shortcode which displays the content of another html/php file this looks like this:

function df_display_form()
{
    // Fetching some data with $wpdb

    // Display the data
    include_once plugin_dir_path(__FILE__) . 'markup/show-dynamic-form.php';
}

My problem is that the shortcode will be showed at the top of the page. So I googled this issus and found a solution. As it is writte there the problem is that return should be used instead of echo.

So my question is how can I return the renderd content from the included file? (not echo).

1

2 Answers 2

1

Try using an output buffer. https://www.php.net/manual/en/function.ob-get-clean.php

function df_display_form()
{
    ob_start(); 
    // Fetching some data with $wpdb

    // Display the data
    include_once plugin_dir_path(__FILE__) . 'markup/show-dynamic-form.php';
    $out = ob_get_clean();
    return $out;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You have to use php fuction to display the form using shortcode.

I think you have currently this type of code in show-dynamic-form.php file.

<?php

// Some codes

?>

<form>

Form elements

</form>

It will just echo your code to the top of your post or page. Correct way is:

<?php

    // Some codes

    $variable = '<form>';
    $variable .= 'form elements';
    $variable .= '</form>';

    return $variable;

?>

Try to use this way in your dynamic-form.php file and it should work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.