-1

The error is:

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp64444\www\demoplugin\wp-includes\class.wp-styles.php:237) in C:\wamp64444\www\demoplugin\wp-includes\pluggable.php on line 914

code of my plugin that create form for registration and form login

<?php
   /*
   Plugin Name: Form test
   Plugin URI: http://co.com
   Description: a plugin to create a form
   Version: 1.2
   Author: Ghn
   Author URI: http://co.com
   License: GPL2
   */

function registration_form($username, $password, $email)
{

    echo'<form method="post" action="' . $_SERVER['REQUEST_URI'] . '" >
    UserName: <input type="text" name="username" value="' . ( isset( $_POST['username'] ) ? $username : null ) . '"><br>
    Email: <input type="email" name="email" value="' . ( isset( $_POST['email']) ? $email : null ) . '"><br>
    Password: <input type="password" name="password" value="' . ( isset( $_POST['password'] ) ? $password : null ) . '"><br>
    <input type="submit" name="submit" value="Register">
        </form>';
}



function registration_validation( $username, $password, $email )  
{
    global $reg_errors;
    $reg_errors = new WP_Error;
   if ( empty( $username ) || empty( $password ) || empty( $email ) ) 
   {
     $reg_errors->add('field', 'Required form field is missing');
   }
   if ( 4 > strlen( $username ) ) 
   {
     $reg_errors->add( 'username_length', 'Username too short. At least 4 characters is required' );
   }
   if ( username_exists( $username ) )
        $reg_errors->add('user_name', 'Sorry, that username already exists!');

   if ( ! validate_username( $username ) ) 
   {
       $reg_errors->add( 'username_invalid', 'Sorry, the username you entered is not valid' );
   }

   if ( 5 > strlen( $password ) ) 
   {
       $reg_errors->add( 'password', 'Password length must be greater than 5' );
   }

   if ( !is_email( $email ) ) 
   {
       $reg_errors->add( 'email_invalid', 'Email is not valid' );
   }

   if ( email_exists( $email ) ) 
   {
        $reg_errors->add( 'email', 'Email Already in use' );
   }

   if ( is_wp_error( $reg_errors ) ) 
   {

       foreach ( $reg_errors->get_error_messages() as $error ) 
       {
           echo '<div>';
           echo '<strong>ERROR</strong>:';
           echo $error . '<br/>';
           echo '</div>';    
        }
    }
}

function complete_registration() 
{
  global $reg_errors, $username, $password, $email;
  if ( 1 > count( $reg_errors->get_error_messages() ) ) 
  {
    $userdata = array(
    'user_login'    =>   $username,
    'user_email'    =>   $email,
    'user_pass'     =>   $password,
    );
    $user = wp_insert_user( $userdata );
    echo 'Registration complete. ';   
  }
}

function custom_registration_function() 
{
  if ( isset($_POST['submit'] ) ) 
  {
     registration_validation(
     $_POST['username'],
     $_POST['password'],
     $_POST['email']
     );

    // sanitize user form input
    global $username, $password, $email;
    $username   =   sanitize_user( $_POST['username'] );
    $password   =   esc_attr( $_POST['password'] );
    $email      =   sanitize_email( $_POST['email'] );


    // call @function complete_registration to create the user
    // only when no WP_error is found
    complete_registration(
    $username,
    $password,
    $email
    );
  }

 registration_form(
    $username,
    $password,
    $email
    );
}

add_shortcode('test', 'form_creation'); 
// The callback function that will replace [book]
function form_creation() {
    ob_start();
    custom_registration_function();
    return ob_get_clean();
}



function login_form()
{
   echo'<form method="post" action="' . $_SERVER['REQUEST_URI'] . '" >
    Email: <input type="email" name="email" value="' . ( isset( $_POST['email']) ? $email : null ) . '"><br>
    Password: <input type="password" name="password" value="' . ( isset( $_POST['password'] ) ? $password : null ) . '"><br>
     <input type="submit" name="submit" value="Register">
     </form>';
}

function dlf_auth( $username, $password ) 
{
  global $user;
  $creds = array();
  $creds['user_login'] = $username;
  $creds['user_password'] =  $password;
  $user = wp_signon( $creds, false );
  if ( is_wp_error($user) ) 
  {
      echo $user->get_error_message();
  }
 if ( !is_wp_error($user) ) 
  {
    echo 'login success';
  }
}


function dlf_process() 
{
  if (isset($_POST['submit'])) 
  {
    dlf_auth($_POST['email'], $_POST['password']);
  } 
  login_form();
}


add_shortcode('testlogin', 'login_test'); 
// The callback function that will replace [book]
function login_test() {
    ob_start();
    dlf_process();
    return ob_get_clean();
}

?>

4
  • 1
    First learn how to ask a question on WPSE here
    – DHL17
    Commented Aug 29, 2017 at 13:06
  • Also, this issue seems to hang more to the 'off topic' side. since this looks like a generic question and not an issue with WordPress or it's plugin system.
    – xvilo
    Commented Aug 29, 2017 at 13:19
  • have you modified WP Core? Both of the files in the warning are Core WordPress files, not parts of your plugin. You should also prefix your POST variables, email or password are super generic and will cause clashes
    – Tom J Nowell
    Commented Aug 29, 2017 at 13:40
  • 1
    wp_signon sets auth cookies, you can't log a user in after output has already started.
    – Milo
    Commented Aug 29, 2017 at 17:11

2 Answers 2

0

If you want to add headers this can only be done before you are returning data. Headers are send at the start of a response from the browser.

What probably goes wrong is that you have an empty line on the top of your file. Or you do somewhere an echo (or something a like). Then PHP starts outputting data (thus returning stuff to the browser). Then you've missed your change of adding headers.

Please check if you don't have an empty line in your file like this:

.empty first line or any char here.
<?php
   echo 'hi'; // Echo here BEFORE your setting any headers
?>
.or an empty line/char here?
2
  • not useful your answer Commented Aug 29, 2017 at 13:05
  • I'm not sure what to react to your comment. But you cannot expect we can help you out with a complete answer 100% of the times. Also, your question was very poorly written. No markup. Not correct English (Or even a bit in that way). For example, I can not assess if you have an empty line before in your php file. I tried to tell you what the error means and how it sometimes happens. This way you can look at the execution of your full code and see if something weird happens, but it could be a part of the issue
    – xvilo
    Commented Aug 29, 2017 at 13:16
0

Is seems your function complete_registration() is written without taking any arguments. But when you use it you are passing three arguments, $username,$password,$email.

Add three argumentscomplete_registration($username, $password, $email)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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