headers - Using ob_start() in plugin

I'm learning how to build a WordPress plugin. In the sample plugin code, I have this PHPHTML:ob_start(); other p

I'm learning how to build a WordPress plugin. In the sample plugin code, I have this PHP/HTML:

ob_start();

// other plugin code

<?php function plugin_rvce_options_page() { ?>

    <div>

        <form action="options.php" method="post">

            <?php settings_fields('plugin_options'); ?>
            <?php do_settings_sections('plugin'); ?>

            <input name="Submit" type="submit" value="<?php esc_attr_e('Save Changes'); ?>" />

        </form>

    </div>

<?php } ?>

Initially, this was causing the warning "Headers already sent" before I found ob_start. After implementing ob_start I don't get the warnings.. but am I using it correctly by just adding ob_start at the top of my plugin file?

I'm learning how to build a WordPress plugin. In the sample plugin code, I have this PHP/HTML:

ob_start();

// other plugin code

<?php function plugin_rvce_options_page() { ?>

    <div>

        <form action="options.php" method="post">

            <?php settings_fields('plugin_options'); ?>
            <?php do_settings_sections('plugin'); ?>

            <input name="Submit" type="submit" value="<?php esc_attr_e('Save Changes'); ?>" />

        </form>

    </div>

<?php } ?>

Initially, this was causing the warning "Headers already sent" before I found ob_start. After implementing ob_start I don't get the warnings.. but am I using it correctly by just adding ob_start at the top of my plugin file?

Share Improve this question asked May 10, 2016 at 22:28 cpcdevcpcdev 3972 gold badges4 silver badges19 bronze badges 5
  • why do you need to use output buffering? – Milo Commented May 10, 2016 at 22:38
  • I read that it prevents the HTML from outputting.. Im trying to bypass the "headers already sent" warning so I can write HTML inside the plugin file itself – cpcdev Commented May 10, 2016 at 22:40
  • Better to solve the headers error rather than resorting to output buffering, but either way, it doesn't look like you're using it right - if you want to buffer the output in that function, you should start buffering within the function. Also, I assume you're ending buffering somewhere and doing something with the output. – Tim Malone Commented May 10, 2016 at 22:48
  • 3 NEVER EVER hide errors, fix them. If you don't, they will come back to bite you. The one who teached you the output buffering way to hide errors knows nothing about coding. That is a horrible idea to do that – Pieter Goosen Commented May 11, 2016 at 10:55
  • The ob_start call isn't inside PHP and it's being sent directly to the browser in your above code – Tom J Nowell Commented May 11, 2016 at 16:35
Add a comment  | 

3 Answers 3

Reset to default 2

No, this is not a correct use for ob_start(). You're getting the warning because your script is outputting code before the page headers are sent - meaning you're printing output before the html page.

Without knowing what's going on in your // other plugin code it's difficult to say what's happening exactly. I would guess you're calling plugin_rvce_options_page() somewhere in the root of the functions.php file instead of in a function that outputs to an admin page. In any case, try and fix the issue and don't use ob_start() as a work-around.

You definitely shouldn't be using ob_start() to prevent premature outputting of html. Rather you should use add_action() at the top of the plugin to start outputting your code in the right place.

For backend the usual hook is add_action ('admin_init','your_main_function');

For frontend the usual hook is add_action ('wp_head','your_main_function');

Here's a list of all available hooks: https://codex.wordpress/Plugin_API/Action_Reference

The output is happening when you call the function plugin_rvce_options_page, not when you declare it. Try this:

// other plugin code

<?php function plugin_rvce_options_page() {     
ob_start();
?>

<div>

    <form action="options.php" method="post">

        <?php settings_fields('plugin_options'); ?>
        <?php do_settings_sections('plugin'); ?>

        <input name="Submit" type="submit" value="<?php esc_attr_e('Save Changes'); ?>" />

    </form>

</div>

<?php  
return ob_get_clean();
 }
 ?>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745109074a4611737.html

相关推荐

  • headers - Using ob_start() in plugin

    I'm learning how to build a WordPress plugin. In the sample plugin code, I have this PHPHTML:ob_start(); other p

    9小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信