Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to create my first 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|improve this question
add comment

2 Answers

Your syntax is wrong. You have a closing PHP tag where there is no opening PHP tag at the end of the script. This means you are creating output before the headers are being sent.

Change this:

function prodmanager_html_page() {
?>

<div>

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

To this:

function prodmanager_html_page() {
    ?>
    <div>
        <h2>Product Manager</h2>
        <!--usual HTML and PHP Code--> 
        </div>
    <?php
}

You do not need a closing PHP tag at the end of your script. This is a good practice to help safeguard against stray whitespace after the closing tag which will give WP a white screen of death.

share|improve this answer
    
Thanks heaps guys. You are right. I have removed the ?> at the end of my script and it now works fine. –  MeltingDog Mar 3 '12 at 4:26
1  
Make sure to accept the answer so others can tell what solved your issue. –  Brian Fegter Mar 3 '12 at 4:42
add comment

Welcome to WPSE.

Firstly you should do your conditional logic inside the action's callback, for example:

add_action( 'init', 'somefn' );
function somefn() {
    if( is_admin() ) {
        // do something
    }
}

Secondly, you are hooking into admin_menu which is only run in the backend anyways, so the is_admin() check can be removed.

/* 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>

<?php }

And as Brian said, you have a closing PHP tag where it's not needed, and you aren't closing the last function with a }, I added it into the chunk of code above. I think that would pretty much solve your problem!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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