How to Display All Subscribers in an Unordered List in WordPress

Home » Snippets » How to Display All Subscribers in an Unordered List in WordPress

Created with:



Creator: WPTurbo Team

Customize with WPTurbo AI

Add Snippet To Project

New Project
Add To Existing Project

Do you want to create a list of all your subscribers on your WordPress website? An easy way to do this is by displaying them in an unordered list. By prominently showcasing your subscribers in this way, you can not only acknowledge their support but also promote a sense of community on your platform. In this article, we will guide you through the process of creating an unordered list of all your subscribers using WordPress. Let’s dive in and learn how to showcase your subscriber base effectively.

					function wpturbo_display_subscribers() {
    $subscribers = get_users( array( 'role' => 'subscriber' ) ); 
    if ( ! empty( $subscribers ) ) {
        echo '<ul>';
        foreach ( $subscribers as $subscriber ) {
            echo '<li>' . esc_html( $subscriber->display_name ) . '</li>';   
        echo '</ul>';
    } else {
        echo '<p>No subscribers found.</p>';
add_shortcode( 'wpturbo_display_subscribers', 'wpturbo_display_subscribers' );

The code snippet provided aims to display a list of all subscribers in an unordered list on a WordPress website.

The first line of the code creates a function called wpturbo_display_subscribers(). This function will be responsible for retrieving and displaying the list of subscribers.

Inside the function, we use the get_users() function to retrieve all users with the role of "subscriber". The get_users() function accepts an array of arguments, and in this case, we pass the argument 'role' => 'subscriber' to specifically target subscribers.

Next, we check if there are any subscribers returned. If the $subscribers array is not empty, we proceed to display the list of subscribers.

We start by echoing an opening <ul> tag to start the unordered list.

Then, we utilize a foreach loop to iterate through each subscriber in the $subscribers array. Within the loop, we echo an opening <li> tag followed by the display name of the subscriber.

To ensure the subscriber’s display name is sanitized and does not contain any malicious code, we make use of the esc_html() function to escape and sanitize the output.

After echoing the subscriber’s display name, we close the <li> tag. This process is repeated for each subscriber in the array.

Once the loop completes, we echo the closing </ul> tag to close the unordered list.

If there are no subscribers found in the $subscribers array, we echo a simple <p> tag with the text "No subscribers found." This message will be displayed when there are no subscribers to be shown.

Finally, we use the add_shortcode() function to register a shortcode called [wpturbo_display_subscribers] that can be used to output the list of subscribers. The shortcode is associated with the wpturbo_display_subscribers() function, ensuring that the function is called when the shortcode is encountered.

This allows users to easily display the list of subscribers on any page or post by simply adding the shortcode [wpturbo_display_subscribers].

Register an account to save your snippets or go Pro to get more features.