Jump to content

WP Plugin - 'Headers Already Sent' error


melting_dog

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!

Link to comment
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

Link to comment
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

Link to comment
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

Link to comment
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.

Link to comment
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

Link to comment
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!

Link to comment
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.