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 uploaded to a WordPress site (3.3.1) a plugin that works perfectly in my other sites (3.3.1 and 3.4), but when try to activate it I get Fatal Error. When I go to check the plugin's main file content to do some changes and identify the error I detect that at the end of my code appears some random code from my file repeated.

If I edit it and save again another random code is placed at the end of the file again.

Any suggestion will be appreciated, even if are suggestions to do more research about it. I really don't know what to check for this issue.

Edition to add information requested:

I test deactivating all the plugins and with Twenty Eleven theme and I obtain the same behaviour

Example of the random code added

<?php
/*
Plugin Name: My plugin
Description: desc
Version: 1.0
*/

/**
 * Version of WordPress
 * @global  string  $wp_version
 */
global $wp_version;

$exit_msg = __('This plugin require WordPress 3.1 or newer','seo-pressor').'. <a href="http://codex.wordpress.org/Upgrading_WordPress">'.__('Please update','seo-pressor').'</a>';
if (version_compare($wp_version, "3.1", "<")) { 
    exit($exit_msg);
}

// Avoid name collisions.
if (!class_exists('WPPostsRateKeys')) {
    class WPPostsRateKeys
    {
(... long piece of code ...)
    }
}

// create new instance of the class
$WPPostsRateKeys = new WPPostsRateKeys();
if (isset($WPPostsRateKeys)) {
    // register the activation function by passing the reference to our instance
    register_activation_hook(__FILE__, array(&$WPPostsRateKeys, 'install'));
    register_deactivation_hook(__FILE__, array(&$WPPostsRateKeys, 'uninstall'));
}][

As you can see at the end was added the "][" that make my code fail.

Another information: when I open my text file using WordPress edit plugin file feature I see the random code at the end that will make my code file, but if I open the same file with the cPanel file manager this utility ask me for the encoding first (assign the file UTF-8) and opens it without any extra code added at the end. Anyway the activation of the plugin continue failing in WordPress due to this extra code.

I already save my files as FTP-8 without BOM and follow all the suggestions from http://www.toddlahman.com/the-plugin-generated-x-characters-of-unexpected-output-during-activation and doesn't works.

When in the WordPress editor I open the file the random text appears but when I open the same file using my FTP client the random text doesn't appears.

How do I need to code or save my PHP file so it works correctly?

Thanks in advanced Leticia

share|improve this question
    
Try disabling all other plugins and switch to Twenty Ten/Eleven. See if the problem persists and update question accordingly. –  mrwweb Jul 24 '12 at 20:49
    
Possibly a UTF vs ASCII issue? Check to see that the file generating the code is saved as UTF-8. –  hereswhatidid Jul 24 '12 at 20:49
1  
In that case could you post a sample of the random code? That would help narrow down the cause. –  hereswhatidid Jul 24 '12 at 21:10
1  
Are you sure about (3.3.1 : doesn't works) & (3.3.1 and 3.4 : works)? . . . If the plugin works in other WordPress installations, wouldn't the problem be with WP instead of the plugin? –  brasofilo Jul 29 '12 at 16:21
1  
As I said: Save the file with Notepad++ (UTF-8 without BOM). Then upload it via FTP only. Then activate. If this doesn't work, go back, delete the parts (with Notepad++ again) that are not in the code you showed above ↑ and upload it again via FTP. Btw: This is highly off topic and should be closed. –  kaiser Jul 29 '12 at 17:22
show 15 more comments

3 Answers

Check if you have a class or function with many echo's, that are called on the functions register_activation_hook or register_deactivation_hook. Also - this functions must use static functions.

Also, is it possible that you have a var_dump() inside your source?

It is important that you not have an output (like echo or var_dump) on activation or deactivation of the plugin. This not a part of the activation process. WP checks this and gives this warning for plugins that generate many strings on the include. Check that your output only works on the right hooks for output, not for activation and so on.

Also, include plugins using the hook plugins_loaded (or init as an alternative) not via new, it's not the same point on stack trace of WordPress. Like the following example:

class WPPostsRateKeys {

    static private $classobj;

    /**
     * Handler for the action 'init'. Instantiates this class.
     *
     * @since   2.0.0
     * @access  public
     * @return  $classobj
     */
    public function get_object() {

        if ( NULL === self :: $classobj ) {
            self :: $classobj = new self;
        }

        return self :: $classobj;
    }

    public function __construct() {

    }

}

if ( function_exists( 'add_action' ) && class_exists( 'WPPostsRateKeys' ) ) {
    add_action( 'plugins_loaded', array( 'WPPostsRateKeys', 'get_object' ) );
}

Also possible, but in other answers and the link from question.

  • a whitespace before the start tag of php <?php
  • Leave the closed php tag at the end of file ?>
  • The file must encoded as Unicode-file, like UTF-8
share|improve this answer
    
thank, now the plugin is activated but seems like an error occurs because I get a blank page for any WordPress page after I activate the plugin with the changes you suggest that I haven't before: - leave the closed php tag on the end of file ?>, - use the plugins_loaded to activate the plugin –  gsc-leticia Jul 31 '12 at 15:06
    
Maybe you copy the plugin on a gist or check in in an open repo to test it? –  bueltge Jul 31 '12 at 17:22
    
I will do some more tests, if fails I will do, thanks. –  gsc-leticia Aug 2 '12 at 21:48
    
OK. I have write a little bid more in my answer, please read them and check your sourece for this hints. –  bueltge Aug 3 '12 at 8:06
add comment

Try upload it with FileZilla option Force UTF-8. Beside you can try with "mc" ftp client if you are on Linux.

share|improve this answer
add comment

looks like a UTF-8 vs ASCII issue, try resaving the file as UTF-8 and uploading via FTP.

share|improve this answer
    
The file is already as UTF-8 and uploaded via FTP. I try resaving it, besides as a UTF-8 without BOM and I get the same problem. thanks –  gsc-leticia Jul 29 '12 at 21:06
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.