0

I am creating a custom plugin, the users for the plugin are saved in a separate table and there is a table that display all users having edit and delete links. while delete link is clicked, the user got deleted but returning back to page displaying users adds the user back in database !!!!

It is happening only with last inserted user, rest working fine, means I can delete other users successfully.

wordpress version 4.5.2

It seems it is because register function is called with admin_init hook and it is called every time user visits the admin area, but no idea how it can be solved .

user deletion

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']) );
        }
    }
}

user registartion

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!';
    }
}

display users

    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;
    }
}

may be related to (Why does “get_option” pull in the older value in options.php, rather than the newer value, on submission of a form?)

but can not solve my problem.

  • 1
    Where's the code that displays the users? You said but returning back to page displaying users adds the user back in database. And what's up with the options? Are you storing same data both in your own table and options table?! WordPress uses options table with every request - it has to be as light as possible. – N00b May 9 '16 at 15:52
  • @N00b edited my code, please check it now. – Prafulla Kumar Sahu May 9 '16 at 17:01
  • I'm going to jump the gun and say the user isn't actually re-added - if you were to re-load the page the user would then be gone, yes? If so, it's because you pull the users table into memory to later display them, but before you run the delete query. – TheDeadMedic May 9 '16 at 17:02
  • @TheDeadMedic what happens is first when I click the delete link, it deletes the user from database, then I have not written any code to redirect you can notice it, now when I come back, and reload the database, I am getting the user back . – Prafulla Kumar Sahu May 9 '16 at 17:23
  • 1
    The delete function isn't working if the user is still there... – TheDeadMedic May 10 '16 at 11:47
0

I solved this by doing something link

$hook = add_menu_page('My Plugin Settings','My Plugin Settings','manage_options','my_plugin_settings','display_plugin_options');
add_action('load-'.$hook,'do_on_my_plugin_settings_save');

function do_on_my_plugin_settings_save()
{
  if(isset($_GET['settings-updated']) && $_GET['settings-updated'])
   {
      //plugin settings have been saved. Here goes your code
   }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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