Search code examples
wordpresswoocommercehook-woocommerce

Litsing user and remove with ajax at wordpress


This code perfectly doing listing users, and remove users with short code. But i have an author directory plugin, and i can list my user with additional meta data. So i want to remove user on my author directory plugin. At the bellow, there is my author directory plugins codes, how can i combine this funciton.

add_action( 'wp_head', 'my_action_javascript' );

function my_action_javascript() {

        ?>
        <script type="text/javascript" >
                jQuery(document).ready(function() {

                        jQuery(".delete_user").click(function() {

                                var current_element_var = jQuery(this);

                                var data = {
                                        'action': 'delete_user_action',
                                        'user_id': current_element_var.attr('delete-user-id'),
                                        'security': '<?php echo wp_create_nonce( "security-special-string" ) ?>'
                                };
                                jQuery.post('<?php echo admin_url( 'admin-ajax.php' ) ?>', data, function(response) {
                                        if (response == 'deleted_successfully') {
                                                current_element_var.hide();
                                                current_element_var.after('<span> User Deleted Successfully </span>');
                                                current_element_var.remove();
                                        }
                                });

                                return false;

                        });

                });
        </script>
        <?php

}

add_action( 'wp_ajax_delete_user_action', 'delete_user_action_callback' );
add_action( 'wp_ajax_nopriv_delete_user_action', 'delete_user_action_callback' );

function delete_user_action_callback() {
        check_ajax_referer( 'security-special-string', 'security' );
        wp_delete_user( $_POST['user_id'] );
        echo 'deleted_successfully';
        die();

}


add_shortcode('listuser', 'listsubscriber');
function listsubscriber() {
$blogusers = get_users( array(  'fields' => array('display_name','ID' )) );

echo '<ul>';
foreach ( $blogusers as $user ) {

    echo '<li>' . esc_html( $user->display_name )  . '|' . esc_html( $user->ID )  . '<a class="delete_user" delete-user-id="' . $user->ID . '" href="#">Delete User</a></li>';

}
echo '</ul>';

}

Author list plugin codes at here.



require_once("rules/useroles.php");
require_once("admin/admin.php");

function member_directory_loader_scripts()
{
    wp_register_style( 'directorycss', plugin_dir_url( __FILE__ ).'asset/js/jdirectory/directory.css');
    wp_enqueue_style( 'directorycss' );
    
    wp_register_script( 'directoryjs', plugin_dir_url( __FILE__ ).'asset/js/jdirectory/jquery.directory.js', array('jquery'));
    wp_enqueue_script( 'directoryjs' );
}
add_action( 'wp_enqueue_scripts', 'member_directory_loader_scripts' );


function member_directory_shortcode($atts)
{
    global $table_prefix,$wpdb,$post;
    
    $return_content = '';
    $return_content .= '<div class="member_directory_table">';      
        
    $results = get_users();
    
    if ((!(empty($results))) && (is_array($results)) && (count($results) >0))
    {
        $m_single = array();
        foreach ($results as $single)
        {
            $user_allowed_listed = true;
            $memberDirectoryUserRoleSelect = get_option('memberDirectoryUserRoleSelect');
            if (empty($memberDirectoryUserRoleSelect))
            {
                
            }
            else
            {
                $user_allowed_listed = check_user_role_allowed($single);
                
                if ($user_allowed_listed == false)
                {
                    continue;
                }

                
            }
            
            $return_content .= '<div class="tooltips_list">';
            $return_content .= '<span class="tooltips_table_items">';
            $return_content .= '<div class="tooltips_table">';
            
            
            $return_content .= '<div class="tooltips_table_title">';
            $enabGlossaryIndexPage =  get_option("enabGlossaryIndexPage");
            $return_content .=  $single->display_name;
            $return_content .='</div>';
            
            $return_content .= '<div class="billing">';
            $return_content .=  $single->billing_address_1;
            $return_content .='</div>';
            

***** i want to at there remove user link with ajax ******


            $return_content .= '<div class="billing2">';
            $return_content .=  $single->billing_company;
            $return_content .='</div>';
            
            $return_content .= '<div class="billing3">';
            $return_content .=  $single->ID;
            $return_content .='</div>';
            
            $return_content .= '<div class="tooltips_table_content">';

            // old $m_content = $single->user_email;
            // 1.3.1
            $m_content = '';
            $m_content_user_email = $single->user_email;
            $m_content_user_bio_in_wp = get_the_author_meta('description',$single->ID);
            $m_content .= "<div class = 'member_content_user_email'>";
            $m_content .= $m_content_user_email;
            $m_content .= "</div>";
            $m_content .= "<div class = 'member_content_user_description'>";
            $m_content .= $m_content_user_bio_in_wp;
            $m_content .= "</div>";
            
            $return_content .=  $m_content;
            $return_content .='</div>';
            $return_content .='</div>';
            $return_content .='</span>';
            $return_content .='</div>';
        }
    }
    $return_content .= '</div>';
    
    return $return_content;
}
add_shortcode( 'member_directory', 'member_directory_shortcode',10 );



function member_directory_load_footer_js()
{
    global $post;
    ?>
<script type="text/javascript">
var inboxs = new Array();
inboxs['hidezeronumberitem'] = "yes";
inboxs['selectors'] = '.tooltips_list > span';
<?php 
$glossaryNavItemFontSize = '12px';
$glossarySelectedNavItemFontSize = get_option("glossarySelectedNavItemFontSize");
$glossarySelectedNavItemFontSize = '14px';
$glossaryNavItemFontSize = '12px';
?>
inboxs['navitemdefaultsize'] = '<?php echo $glossaryNavItemFontSize; ?>'; 
inboxs['navitemselectedsize'] = '<?php echo $glossarySelectedNavItemFontSize; ?>';
<?php 

$glossaryNumbersOrNot = 'no';

if ($choseLanguageForGlossary == 'custom')
{
    $glossaryLanguageCustomNavLetters = 'a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z';
    ?>
    inboxs['alphabetletters'] = "<?php echo $glossaryLanguageCustomNavLetters; ?>";
    <?php
}
?>
inboxs['number'] = "no";
jQuery(document).ready(function () {
    jQuery('.member_directory_table').directory(inboxs);
    jQuery('.navitem').css('font-size','<?php echo $glossaryNavItemFontSize; ?>');  
})
</script>
<?php
}
add_action('wp_footer','member_directory_load_footer_js');


function check_user_role_allowed($checkuser)
{
    $memberDirectoryUserRoleSelect = get_option('memberDirectoryUserRoleSelect');
    $saved_allowed_user_roles_in_member_directory = get_option('saved_allowed_user_roles_in_member_directory');
    
    
    
    if (empty($memberDirectoryUserRoleSelect))
    {
        return true;
    }
    else
    {
        if ('enableMemberDirectoryUserRolesOption' == $memberDirectoryUserRoleSelect)
        {
            $can_listed = false;
            
            $checking_user_roles = $checkuser->roles;
            
            
            
            
            if (empty($checking_user_roles))
            {
                return false ;
            }
            else 
            {
                foreach ($checking_user_roles as $checking_user_role)
                {
                    if (in_array(strtolower($checking_user_role), $saved_allowed_user_roles_in_member_directory) )
                    {
                        
                        $can_listed = true;
                        
                        
                        
                        return true;
                    }
                }
            }
            
        }
        
        if ('disableMemberDirectoryUserRolesOption' == $memberDirectoryUserRoleSelect)
        {
            $can_listed = true;
            $checking_user_roles = $checkuser->roles;
            if (empty($checking_user_roles))
            {
                return false ;
            }
            else
            {
                foreach ($checking_user_roles as $checking_user_role)
                {
                    if (in_array(strtolower($checking_user_role), $saved_allowed_user_roles_in_member_directory) )
                    {
                        $can_listed = false;
                        return false;
                    }
                }
            }
                
        }
        
        return $can_listed;
    }
    
    
}



like so

Solution

  • Am guessing that $single->ID contains the User ID. If so, in that place where you marked in code, you could add the delete button.

    It would be something like this:

    $return_content .= '<div class="billing">';
    $return_content .=  $single->billing_address_1;
    $return_content .='</div>';
    
    
    /***** i want to at there remove user link with ajax ******/
    
    /*--------- delete button ------------*/
    $return_content .= '<button class="delete_user" delete-user-id="'. $single->ID .'">Delete</button>';
    /*--------- /delete button ------------*/
    
    
    $return_content .= '<div class="billing2">';
    $return_content .=  $single->billing_company;
    $return_content .='</div>';
    

    You are already listening to the click event of the class delete_user. And inside it, you are expecting the delete-user-id to have the user_id of the User whom you want to delete! Hence we are using both in our button.

    One thing I noticed in code is the add_action( 'wp_ajax_nopriv_delete_user_action', 'delete_user_action_callback' ); This will allow non-logged in users to perform this delete operation because of the wp_ajax_nopriv_*

    This hook is functionally the same as wp_ajax_{$action}, except the “nopriv” variant is used for handling AJAX requests from unauthenticated users, i.e. when is_user_logged_in() returns false.