1

I am creating a settings page. If I use the default example it works fine.

<form method="post" action="options.php">
    <?php if(isset( $_GET['settings-updated'])) { ?>
    <div class="updated">
        <p>Settings updated successfully</p>
    </div>
    <?php } ?>

    <textarea name="setting_1"><?php echo get_option('setting_1'); ?></textarea>

    <?php 

    settings_fields('my_settings');
    do_settings_sections('my_settings');

    submit_button(); 

    ?>
</form>

However, I am using PHP to more easily manipulate the HTML (start of complex functionality) so I changed submit_button() to get_submit_button() in order to assign the returned value to a variable.

$output .= '</div>'
$output .= get_submit_button();
$output .= '</form>';

This works as expected. However, I cannot seem to find a similar function for settings_fields('my_settings') and do_settings_sections('my_settings').

These functions echo the output rather than return the output. This means these functions echo their output outside of the form I am constructing with PHP.

How is it possible to capture echoed output from such functions and assign it to a variable in PHP?

1

Found it, I think this was suggested to me in another answer.

$output .= ...

ob_start();
settings_fields('my_settings');
do_settings_sections('my_settings');
$output .= ob_get_clean();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.