WP_AJAX未调用该操作

时间:2015-08-10 作者:Rahul Upadhyay

我正在开发一个Ajax不起作用的插件。我正在使用以下代码:

add_action( \'wp_ajax_cpm_add_update\', array( $this, \'edit_added_people\' ) ); 
Theedit_added_people() 调用操作后未调用函数cpm_add_update, 这可能是什么原因?

以下是我的表单代码:

function cpm_add_people_form( $project_id, $people = null ) {    
    $title = $content = \'\';
    $submit = __( \'Add Person\', \'cpm\' );
    $users = array();
    $id = $milestone = 0;
    $action = \'cpm_add_user_new\';   
    if ( !is_null( $people ) ) {
        $id = $people->ID;
        $title = $people->post_title;
        $name = $people->display_name;
        $email = $people->user_email;
        $files = array();
        $users = empty( $project->users ) ? array() : $project->users;
        $submit = __( \'Update New People\', \'cpm\' );
        $action = \'cpm_add_people_update\';
    }   
    ob_start();
    ?>

    <div class="cpm-milestone-form-wrap" id="add_people-form">
        <h3 style="background: none repeat scroll 0 0 #ccc; color: #222;font-size: 1.3em; margin: -0.5em -0.5em 2em;padding: 12px; text-align: center; width: 579px;"><?php _e(\'User Detail Form \', \'cpm\'); ?></h3>
        <form class="cpm-add-people-form" action="" method="post" id="cpm-user-edit-here">
            <table  cellspacing="0" cellpadding="5">
                <?php wp_nonce_field($action); ?>
                <tr>
                    <td><b>User Name : </b></td><td><input name="user_name" style="width:100%;" type="text" id="people_name" value="<?php echo esc_attr($people->user_login); ?>" class="required cpm-jitender display_error_body"></td>
                </tr>
                <tr>
                    <td><b>Email address : </b></td><td><input name="people_email" style="width:100%;" type="text" id="people_email" value="<?php echo esc_attr($people->user_email); ?>" class="required cpm-jitender email"></td>
                </tr> 

                <?php if (!is_null($people)) {

                $first_name = get_user_meta($people->ID, \'first_name\', true);} ?>
                <tr>
                    <td><b>First Name : </b></td><td><input name="people_first_name" style="width:100%;" type="text" id="people_first_name" value="<?php echo esc_attr($first_name); ?>"></td>
                </tr>
                <?php if (!is_null($people)) {
                $last_name = get_user_meta($people->ID, \'last_name\', true);} ?>
                <tr>
                    <td><b>Last Name : </b></td><td><input name="people_last_name" style="width:100%;" type="text" id="people_last_name" value="<?php echo esc_attr($last_name); ?>"></td>
                </tr>
                <tr>
                    <td><b>Display Name : </b></td><td><input name="people_display_name" style="width:100%;" type="text" id="people_display_name" value="<?php echo esc_attr($people->display_name); ?>"></td>
                </tr>
                <tr>
                    <td><b>Website : </b></td><td><input name="people_website" style="width:100%;" class="url" type="text" id="people_website" value="<?php echo esc_attr($people->user_url); ?>"></td>
                </tr>
                <tr>
                    <td><b>New Password : </b></td><td><input name="people_password" style="width:100%;" type="password" id="pass1" value=""></td>
                </tr>
<!--              uncomment this  to generate a password-->
<!--                <tr>
                    <td><b><?php // echo \'New password: \' . wp_generate_password( 8, false ); ?></b></td>
                </tr>--> 
                <tr>
                    <td><b>Confirm Password : </b></td><td><input name="repeat_password" style="width:100%;" type="password" id="pass2" value="" data-rule-equalto="input[name=people_password]" data-msg-required="Please confirm your email address" data-msg-equalto="Email addresses do not match"></td>
                </tr>
                <div id="pass-strength-result"><?php _e(\'Strength indicator\'); ?></div>
                <tr>
                    <td><b>Send Password : </b></td><td><input id="send_password" type="checkbox" value="1" name="send_password">Send this password to the new user by email.</td>
                </tr>
                <tr>

                    <td><b>Role : </b></td><td><select name="role" id="role" class="required cpm-jitender" style="width:50%;">
                            <?php
                           $current_role = $people->roles[0] ;
                            if (!$new_user_role)
                                $new_user_role = !empty($current_role) ? $current_role : get_option(\'default_role\');
                            wp_dropdown_roles($new_user_role);
                            ?>
                        </select>
                    </td>  
                </tr>
            </table>
            <div class="cpm-attachment"> <?php cpm_upload_userimg_field($id, $files); ?></div>


            <?php do_action(\'cpm_add_people_form\', $project_id, $people); ?>
            <div class="submit">
                <input type="hidden" name="action" value="<?php echo $action; ?>" />
                <input type="hidden" name="project_id" value="<?php echo $project_id; ?>" />

                <?php if ($id) { ?>
                    <input type="hidden" name="people_id" value="<?php echo $id; ?>" />
                <?php } ?>

                <input type="submit" name="create_new_people" id="create_new_people" class="button-primary" value="<?php echo esc_attr($submit); ?>">
                <a id="CancleButton" class="button add-people-cancel" href="#"><?php _e(\'Cancel\', \'cpm\'); ?></a>
            </div>

        </form>
    </div>
    <?php
    return ob_get_clean();
}
在此之后ajax.php:

add_action( \'wp_ajax_nopriv_cpm_add_update\', array( $this, \'edit_added_people\' ) );    
function edit_added_people() {
        check_ajax_referer(\'cpm_add_update\');
        $posted = $_POST;
    $people_id= $posted[\'people_id\'];
        $people_obj = CPM_People::getInstance();
        $added_user_id = $people_obj->Add_user_update($people_id);
        if ($added_user_id) {
            $response = array(
                \'success\' => true,
                \'id\' => $added_user_id,
                \'content\' => cpm_task_html($task, $project_id, $list_id),
            );
        } else {
            $response = array(\'success\' => false);
        }
        echo json_encode($response);
        exit;
}

1 个回复
最合适的回答,由SO网友:Brick Yang 整理而成

如果请求来自未经身份验证的用户,则应使用wp_ajax_nopriv_(action) 调用函数,在您的情况下是:

add_action(\'wp_ajax_nopriv_cpm_add_update\', array($this,\'edit_added_people\'));

结束

相关推荐

WordPress AJAX Login Screen

在WordPress的最新版本中,当您登录到仪表板时,页面仍处于打开状态,WordPress会超时并打开一个登录框。此框出现在一个小弹出窗口中,允许您登录,而无需刷新页面。是否有人知道是否有一种内置方式可以利用前端的弹出窗口?我有一个链接,需要用户登录才能执行任何操作。当用户未登录时,我希望有一个弹出式登录表单来帮助实现这一点,我希望已经有了一个简单的方法,因此我不需要遵循这些旧教程中的一些内容,也不需要手动完成所有操作,因为这有时会使维护与未来版本的兼容性变得更加困难。感谢您提供的任何建议!