使用表单提交‘init’挂钩插入新用户

时间:2013-08-16 作者:user195257

我正在发布一个表单来插入一个新用户,并将我的函数挂接到“init”。此代码位于我的mu插件文件夹中。

用户可以很好地插入,但页面不会在wards之后加载。

我的表单操作是空的,因此表单应该发布相同的页面(我想要),但是,我只得到一个空白页面(带有页眉和页脚),标题显示找不到页面。

页面的帖子似乎有问题?

下面是我的功能代码

/*
*   Register a new reminder_service user
*/
function reminder_register(){
    if(@$_POST[\'reminder_register\']){
        $user["user_login"] = $_POST["email"];
        $user["user_pass"] = $_POST["password"];
        $user["user_email"] = $_POST["email"];
        $user["display_name"] = $_POST["name"];
        $user["user_nicename"] = strtolower(str_replace(" ", "-", $_POST["name"]));
        $user["role"] = \'reminder_service\';
        $user["user_registered"] = date(\'Y-m-d H:i:s\');
        $user["show_admin_bar_front"] = false;

        $user_id = wp_insert_user($user);
        if(is_int($user_id)){
            echo \'yes\';
        }else{
            foreach($user_id->errors as $error){
                foreach($error as $e)
                    echo $e."<br />";
            }
        }
    }
}
add_action(\'init\', \'reminder_register\');
这是我的表单代码

<form action="" id="reminder_register_form" method="post">
                        <fieldset class="blue">
                            <span><legend><div class="circle c_1"></div> Your Details</legend></span>
                            <ul class="form floated">
                                <li>
                                    <label for="name">Your Name<span class="required">*</span></label>
                                    <input type="text" name="name" id="name" class="required">
                                </li>
                                <li class="right">
                                    <label for="postcode">Postcode<span class="required">*</span></label>
                                    <input type="text" name="postcode" id="postcode" class="required">
                                </li>
                                <li>
                                    <label for="email">Your Email<span class="required">*</span></label>
                                    <input type="email" name="email" id="email" class="required">
                                </li>
                                <li class="right">
                                    <label for="mobile">Mobile No<span class="required">*</span></label>
                                    <input type="text" name="mobile" id="mobile" class="required">
                                </li>
                                <li>
                                    <label for="password">Password<span class="required">*</span></label>
                                    <input type="password" name="password" id="password" class="required">
                                </li>
                                <li class="right">
                                    <label for="password_confirm">Confirm Password<span class="required">*</span></label>
                                    <input type="password" name="password_confirm" id="password_confirm" class="required">
                                </li>
                                <li>
                                    <label for="terms" class="radio_label">I agree with the Terms of Service, the Privacy Policy and Terms &amp; Conditions of the Good Dealer Scheme Reminder Service</label>
                                    <input type="checkbox" name="terms" id="terms" class="required" value="yes">
                                </li>
                                <li class="right">
                                    <label for="communication" class="radio_label">Please select if you are happy to receive further communication from the Good Dealer Scheme.  </label>
                                    <input type="checkbox" name="communication" id="communication" value="yes">
                                </li>
                            </ul>
                        </fieldset>
                        <div class="submit_button_container">                               
                            <input type="submit" value="Register" name="reminder_register" class="orange">
                            <label><span class="required">*</span> Please complete these fields before clicking the register button</label>
                        </div>
                    </form>
我希望这样做的方式是将表单提交到同一页面,并根据用户插入的结果显示适当的内容

1 个回复
SO网友:Charles Clarkson

表单字段名称中的404页问题。WordPress使用的公共查询变量列表。如果其中一个表单字段使用其中一个名称,则无法保证结果。

该列表在Codex中的某个位置可用,但您可以在页面模板文件中打印该列表。在顶部附近添加此行:

wp_die( \'<pre>\' . var_export( $wp->public_query_vars, true ) . \'</pre>\' );
$wp 是WordPress全局变量。wp_die() 应该打断您的页面,并为您提供类似以下内容的列表:

array (
  0 => \'m\',
  1 => \'p\',
  2 => \'posts\',
  3 => \'w\',
  4 => \'cat\',
  5 => \'withcomments\',
  6 => \'withoutcomments\',
  7 => \'s\',
  8 => \'search\',
  9 => \'exact\',
  10 => \'sentence\',
  11 => \'calendar\',
  12 => \'page\',
  13 => \'paged\',
  14 => \'more\',
  15 => \'tb\',
  16 => \'pb\',
  17 => \'author\',
  18 => \'order\',
  19 => \'orderby\',
  20 => \'year\',
  21 => \'monthnum\',
  22 => \'day\',
  23 => \'hour\',
  24 => \'minute\',
  25 => \'second\',
  26 => \'name\',
  27 => \'category_name\',
  28 => \'tag\',
  29 => \'feed\',
  30 => \'author_name\',
  31 => \'static\',
  32 => \'pagename\',
  33 => \'page_id\',
  34 => \'error\',
  35 => \'comments_popup\',
  36 => \'attachment\',
  37 => \'attachment_id\',
  38 => \'subpost\',
  39 => \'subpost_id\',
  40 => \'preview\',
  41 => \'robots\',
  42 => \'taxonomy\',
  43 => \'term\',
  44 => \'cpage\',
  45 => \'post_type\',
  46 => \'post_format\',
)
不要将任何字段命名为这些名称,您应该进入正确的页面。

例如,您的一个字段命名为name.

<label for="name">Your Name<span class="required">*</span></label>
<input type="text" name="name" id="name" class="required">
项目26 表示此name 将被WordPress劫持。使用其他工具:

<label for="user_name">Your Name<span class="required">*</span></label>
<input type="text" name="user_name" id="user_name" class="required">

结束

相关推荐

Hooks are not executing

根据我对钩子的理解,您可以通过do\\u action(“hook\\u name”)创建一个钩子;然后向所述钩子中添加一些内容,并在希望它执行钩子的位置调用该方法,因此:public function hook_name(){ do_action(\'hook_name\'); } 有些地方你会做类似的事情:add_action(\'hook_name\', \'some_hook\'); 然后在主题中的一些地方,你称之为:hook_name();