• dan_m2k

    (@dan_m2k)


    Hi all,

    My first post as I can’t find a solution via search, so please be gentle πŸ™‚

    I’ve written a very simple plugin to take some postdata from a manually created form in a WP page, validate it and send it off to me in an email. A trivial task in PHP but the plugin I’ve written for it doesn’t get on well with wordpress.

    The crux of it is that when my plugin is active/installed, logging into the admin interface does not work (it simply redirects back to the login form with out error/warning) and the save/auto save for pages breaks with the error “Notice: Undefined variable: _POST in /home/foo/bar-wp/www/wp-admin/admin-ajax.php on line 67”

    The process of ellimination has allowed me to track it down to a function which is intended to process the form’s POSTDATA. I’ve added an action as per:

    add_action(‘init’,’mypluginname_process’);

    And the part of the function causing the trouble is:

    if( ( isset($_POST[‘mypluginname-process-form’]) ) &&
    ( isset($_POST[‘mypluginname-key’]) ) ) { ….

    // do stuff.

    I’ve identified that all my problems dissapear if I comment this and the code it contains out, but return as soon as they’re reinstated.

    Have I missed somethng in the way WP processes postdata? Any help appreciated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    The reason logging in and other stuff doesn’t work is the error message appearing breaks those. So, fix the error message, and the problem will go away.

    The root reason for your problem is that you’re checking for a $_POST variable even when the page itself was not gotten with a POST request. $_POST only gets set when something actually gets posted to the page, but your plugin loads every time. And now, on init, it’s looking for $_POST variables when it’s not even sure that $_POST is defined.

    If you have error_reporting set too high, baldly checking for a $array[‘key’] like this, when you aren’t even sure if $array exists, will generate that notice. It’s a common practice, but most people don’t leave on all their error_reporting either.

    So, possible fixes:

    1. Lower your error reporting level. Downside: if you’re using this plugin elsewhere (or distributing it), you’ll be dependent on them having it lowered too.

    2. Hook somewhere other than init. Why are you checking on every single page load anyway? Isn’t there somewhere more specific you could hook to? Somewhere where you’ll know that they’re posting to your form before you look for the data?

    3. Check to see if $_POST itself is set first. Then, if it is, you can check to see if the array keys exist. Note: Checking $_POST[‘random_key’] will also generate an Undefined Index notice if you have the error_reporting set that high, causing the same issue. Use array_key_exists() instead.

    Thread Starter dan_m2k

    (@dan_m2k)

    I tried:

    if( isset($_POST) ) { … and as an alternative

    if( $_POST ) { …

    which both produce the same error as I came to the same conclusion.

    Where would you suggest hooking it in?

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    That doesn’t make any sense. What’s the exact error you’re getting? Is it always that error in admin-ajax.php? Have you checked to make sure that that file hasn’t been altered in some way?

    Anyway, without seeing your whole code, it’s very hard to point out the specific error. Could you post a larger section of code to http://wordpress.pastebin.com and then post the link back here?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Stuck! My plugin breaks the admin interface!’ is closed to new replies.