我正在创建显示错误WordPress插件?

时间:2017-08-29 作者:Gulam Hussain Niyazi

错误是:

警告:无法修改标题信息-标题已由C:\\wamp64444\\www\\demoplugin\\wp includes\\class.wp style.php:237)中的C:\\wamp64444\\www\\demoplugin\\wp includes\\pluggable发送。php在线914

创建注册表单和表单登录表单的插件代码

<?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();
}
?>

2 个回复
SO网友:xvilo

如果要添加标题,则只能这样做before 您正在返回数据。标题在浏览器响应开始时发送。

可能出现的问题是,您的文件顶部有一个空行。或者你在某个地方echo (或类似的东西)。然后PHP开始输出数据(从而将内容返回到浏览器)。那么您错过了添加标题的更改。

请检查文件中是否没有以下空行:

.empty first line or any char here.
<?php
   echo \'hi\'; // Echo here BEFORE your setting any headers
?>
.or an empty line/char here?

SO网友:Jeff Mattson

似乎是你的功能complete_registration() 没有任何参数。但当你使用它时,你传递了三个参数,$username,$password,$email.

添加三个参数complete_registration($username, $password, $email)

结束

相关推荐

Virtual Pages plugins

我很难让插件正常工作Virtual Pages (WordPress插件可简化虚拟页面的创建)我确实进行了编辑,根据查询创建了一个循环。add_action( \'gm_virtual_pages\', function( $controller ) { /* Creating virtuals pages for companies */ $args = array( \'post_type\' => array(\'companies\',), \'post_status\'