我正在创建一个自定义插件,插件的用户保存在一个单独的表中,并且有一个表显示所有具有编辑和删除链接的用户。单击“删除链接”时,用户被删除,但返回显示用户的页面时,用户将重新添加到数据库中!!!!
这只发生在最后一个插入的用户身上,rest工作正常,意味着我可以成功删除其他用户。
wordpress版本4.5.2
这似乎是因为register函数是用admin\\u init hook调用的,并且每次用户访问管理区域时都会调用它,但不知道如何解决这个问题。
用户删除
public function _myplugin_user_edit_user_page()
{
global $wpdb;
$table_name = \'myplugin_users\';
if(isset($_GET[\'operation\'])){
if($_GET[\'operation\'] == \'delete\'){
$wpdb->delete( $table_name, array( \'myplugin_user_id\' => $_GET[\'id\']) );
}
}
}
用户注册
public function _myplugin_user_registration()
{
global $wpdb;
$table_name = \'myplugin_users\';
if( get_option(\'myplugin_user_option_name\') != null){
$myplugin_user_name = get_option(\'myplugin_user_option_name\')[\'myplugin_user_first_name\'];
$myplugin_user_first_name = get_option(\'myplugin_user_option_name\')[\'myplugin_user_first_name\'];
$myplugin_user_last_name = get_option(\'myplugin_user_option_name\')[\'myplugin_user_last_name\'];
$myplugin_user_email = get_option(\'myplugin_user_option_name\')[\'myplugin_user_email\'];
$random_password = wp_hash_password( wp_generate_password( $length=12, $include_standard_special_chars=false ) );
$get_user = $wpdb->get_results(\'SELECT myplugin_user_login FROM myplugin_users WHERE myplugin_user_email="\' . $myplugin_user_email . \'" LIMIT 1\', ARRAY_A);
$user_exist = array_column($get_user, \'myplugin_user_login\');
if( !in_array($myplugin_user_name, $user_exist) ){
$wpdb->insert(
$table_name,
array(
\'myplugin_user_login\' => $myplugin_user_name,
\'myplugin_user_first_name\' => $myplugin_user_first_name,
\'myplugin_user_last_name\' => $myplugin_user_last_name,
\'myplugin_user_pass\' => $random_password,
\'myplugin_user_nice_name\' => $myplugin_user_name,
\'myplugin_user_email\' => $myplugin_user_email,
\'myplugin_user_url\' => null,
\'myplugin_user_registrated\' => current_time( \'mysql\' ),
\'myplugin_user_activation_key\' => null,
\'myplugin_user_status\' => 0,
\'myplugin_display_name\' => $myplugin_user_name
)
);
}
} else {
$user_error_msg[\'_myplugin_existing_user_email\'] = \'The user is already registered!\';
}
}
显示用户
function _myplugin_user_display()
{
global $wpdb;
global $wp;
if(!$wp->did_permalink){
$edit_url = admin_url() . \'admin.php?page=myplugin-user-edit-page\';
}
$get_users = $wpdb->get_results(\'SELECT myplugin_user_id, myplugin_user_login, myplugin_user_first_name, myplugin_user_last_name, myplugin_user_email FROM myplugin_users\', ARRAY_A);
$table = \'\';
$table .= \'<table class="wp-list-table widefat fixed striped myplugin-users-table">\';
$table .= \'<thead><tr class="header-row"><td id="cb" class="manage-column column-cb check-column"><label class="screen-reader-text" for="cb-select-all-1">Select All</label><input id="cb-select-all-1" type="checkbox"></td>\';
$table .= \'<th scope="col" class="manage-column header">Username</th>\';
$table .= \'<th scope="col" class="manage-column header">Name</th>\';
$table .= \'<th scope="col" class="manage-column header">Email</th>\';
$table .= \'</tr></thead><tbody>\';
foreach( $get_users as $key=>$user){
$table .= \'<tr class="content-row"><th scope="row" class="check-column"><label class="screen-reader-text" for="cb-select-">Select Nunc eget ultricies libero</label>
<input id="cb-select-\'. $key .\'" class="select-post" type="checkbox" name="post[]" value="\'.$key .\'">
<div class="locked-indicator"></div>
</th>\';
$table = $table .= \'<td class="has-row-actions column-primary report" id="">\'
. \'<strong><span class="post">\' . $user[\'myplugin_user_login\'] . \'</a></span></strong>\'
.\'<a href="\'. $edit_url . \'&operation=edit&id=\' . $user["myplugin_user_id"] . \'"> Edit</a>\'
.\'<a href="\'. $edit_url . \'&operation=delete&id=\' . $user["myplugin_user_id"] . \'"> Delete</a>\'
. \'</td>\';
$table = $table .= \'<td class="has-row-actions column-primary report" id="">\'
. \'<strong><span class="post">\' . $user[\'myplugin_user_first_name\'] . " " . $user[\'myplugin_user_last_name\'] . \'</a></span></strong>\'
. \'</td>\';
$table = $table .= \'<td class="has-row-actions column-primary report" id="">\'
. \'<strong><span class="post">\' . $user[\'myplugin_user_email\'] . \'</a></span></strong>\'
. \'</td>\';
}
$table .= \'</tbody></table>\';
echo "page link " . $edit_url;
echo $table;
}
}
可能与(
Why does “get_option” pull in the older value in options.php, rather than the newer value, on submission of a form?)
但不能解决我的问题。