melting_dog Posted March 3, 2012 Share Posted March 3, 2012 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! Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted March 3, 2012 Share Posted March 3, 2012 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 Quote Link to comment Share on other sites More sharing options...
trq Posted March 3, 2012 Share Posted March 3, 2012 The last 7 lines of your script send output to the browser. Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted March 3, 2012 Share Posted March 3, 2012 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 Quote Link to comment Share on other sites More sharing options...
trq Posted March 3, 2012 Share Posted March 3, 2012 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. Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted March 3, 2012 Share Posted March 3, 2012 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 Quote Link to comment Share on other sites More sharing options...
trq Posted March 3, 2012 Share Posted March 3, 2012 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. Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted March 3, 2012 Share Posted March 3, 2012 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 Quote Link to comment Share on other sites More sharing options...
melting_dog Posted March 3, 2012 Author Share Posted March 3, 2012 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! Quote Link to comment Share on other sites More sharing options...
trq Posted March 3, 2012 Share Posted March 3, 2012 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.