Jump to content

Archived

This topic is now archived and is closed to further replies.

melting_dog

WP Plugin - 'Headers Already Sent' error

Recommended Posts

Hi All

 

I am trying to create my first Wordpress plugin. So far it actually works fine but for one error:

 

I get a 'Headers Already Sent' PHP message when I try to login to wp-admin.

 

If I remove the plugin file from the wpcontent/plugins directory, login as usual, then replace the plugin file it works fine - this just seems to occur with login in and out.

 

My (abridged) code is:

 

<?php
/*
Plugin Name: Product Manager
Plugin URI: http://www.tech-knowledge.com.au
Description: Add, edit and remove products
Version: 1.0
Author: Me
Author URI: http://www.tech-knowledge.com.au
License: Restricted
*/

if ( is_admin() ){
/* Call the html code */
add_action('admin_menu', 'prodmanager_admin_menu');

function prodmanager_admin_menu() {
add_options_page('Product Manager', 'Product Manager', 'administrator',
'product-manager', 'prodmanager_html_page');
}
}

function prodmanager_html_page() {
?>

<div>

<h2>Product Manager</h2>
<!--usual HTML and PHP Code--> 
</div>
?>

Let me know if you would like to see it all.

 

Any advice would be really, really appreciated!

 

Thanks!

Share this post


Link to post
Share on other sites

Not sure if you're familiar with the "headers already sent" error but 99% of the time, you get the error when you've output html to the page(even a space or line break is enough) and then attempt to set headers. Find the part where the headers are sent and I almost guarantee you that before that point, you've output something onto the page

Share this post


Link to post
Share on other sites

The last 7 lines of your script send output to the browser.

Share this post


Link to post
Share on other sites

The last 7 lines of your script send output to the browser.

 

 

the last lines wouldn't be the problem. The problem would be sending headers AFTER the output. OR by sending headers twice.

 

One slight thing(probably unrelated), you have an extra ?> after all your html output. get rid of it

Share this post


Link to post
Share on other sites

Indeed the problem would be trying to send headers after output has been sent, which is why a plugin should not send output until it is asked to do so.

Share this post


Link to post
Share on other sites

Indeed the problem would be trying to send headers after output has been sent, which is why a plugin should not send output until it is asked to do so.

 

obviously in his code, there are headers sent after output(somewhere in the functions that were called), but there's no attempt to send headers after those last 7 lines so I don't see how they are any part of the problem

Share this post


Link to post
Share on other sites

It's a plugin. Plugins are registered as Wordpress loads. If after it has loaded there are any attempts to send headers there will be an error because this plugin outputs data when it is loaded into Wordpress.

Share this post


Link to post
Share on other sites

It's a plugin. Plugins are registered as Wordpress loads. If after it has loaded there are any attempts to send headers there will be an error because this plugin outputs data when it is loaded into Wordpress.

 

Sorry, I don't work with wordpress, I assumed that the code specified was the beginning and end of the page trying to be viewed. I didn't realize the code itself was a plug-in. Oh well, the thought was still there, search for when headers are sent and eliminate all text that comes before it

Share this post


Link to post
Share on other sites

Hi guys,

 

Thanks for the response (this forum is so much better to get answers from then the actual WP forum). After a bit of scrounging around I have found the issue. In WP plugins you cannot have a closing php tag ?> at the end of your script.

 

As the file is 'php_included' into WPs own PHP, having the closing tag rendered all the script appearing below the plugin invalid.

 

Cheers for the help!

Share this post


Link to post
Share on other sites

The reason you can't have a closing ?> is simply a safe guard to ensure that the entire script is PHP (and therefore produces no output besides what PHP itself outputs (echo, print etc etc).

 

Take a look at any PHP framework, they all do the same thing.

 

Your actual issue is exactly what I stated in my first reply.

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.