• Please i want to list all users and their data on a page using shortcode, but the code i have only list one user details but am having over 20 different users on my site. how can i make this to list out all users? and secondly how can i make it list out details of only the current user viewing the page?
    below is my code.

        // add the shortcode [myuser-table], tell WP which function to call
        add_shortcode( 'myuser-table', 'persona_table_shortcode' );
    
        // this function generates the shortcode output
        function persona_table_shortcode( $args ) {
        global $wpdb;
        // Shortcodes RETURN content, so store in a variable to return
        $results = $wpdb->get_results( ' SELECT * FROM wp_users' );
        foreach ( $results AS $row ) {
            
            // Modify these to match the database structure
            $dename = $row->user_login . '</br>';
            $depass = $row->user_pass . '</br>';
            $deemail = $row->user_email . '</br>';
        }
        
        $contents = '<table><tbody><tr><td class="tak">FIRST NAME</td><td class="tak">PASSWORD</td><td class="tak">EMAIL</td></tr><tr><td class="tako">'. $dename .'</td><td class="tako">'. $depass .'</td><td class="tako">'. $deemail .'</td></tr></tbody></table>';
    	
        // return the table
        return $contents;

    }

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Why are you going directly to the database rather than using the API function?

    https://developer.www.ads-software.com/reference/functions/get_users/

    There’s also a cool generator for user queries: https://generatewp.com/wp_user_query/

    Thread Starter pandglobal

    (@pandglobal)

    Thanks @sterndata but my reason for going directly to the database is bcs there are tables too that i wish to include which are not usermeta keys, so i think targetting the data base will be helpfull to me, my only problem is how to return a full list based in user ID

    Thread Starter pandglobal

    (@pandglobal)

    Bcs am working with a custom table i created, since thats the case, is it possible to add new columns inside the user meta table and then this print out the values using get_user meta functions?

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    >> s it possible to add new columns inside the user meta table a <<

    Do not modify core WP tables.

    >> there are tables too that i wish to include which are not usermeta keys <<

    Your own tables or core WP tables?

    Thread Starter pandglobal

    (@pandglobal)

    my own tables, the whole idea was to create a user transaction history log.
    so once the user submits the form it inserts some values into the database, i have that code working, and my problem with the above code which was meant to print out users transactions from the custom data table i created.
    the code only returns details of the last user, not even the entire users is returned. if i can add custom column inside users table, how can i print it based on specific user?

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    OK, so the issue is your code is wrong. I can’t code this for you, but my advice is to work it all out in pseudo-code first then look for WP functions to achieve each step.

    Thread Starter pandglobal

    (@pandglobal)

    thanks alot @sterndata i appreciate your time and useful contributions

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Listing all users through shortcode’ is closed to new replies.