• Hi I want to use a code snippet but I receive the error “Cannot redeclare function wpb_user_count.” How can I fix it?

    Here’s the code:

    // Function to show the user count by role via shortcode
    function wpb_user_count($atts) {
    $atts = shortcode_atts( array(
    ‘role’ => ”
    ), $atts );
    $user_query = new WP_User_Query( array( ‘role’ => $atts[‘role’] ) );
    // Get the total number of users for the current query. I use (int) only for sanitize.
    $result = (int) $user_query->get_total();
    return $result;
    }
    // Creating a shortcode to display user count
    add_shortcode(‘user_count’, ‘wpb_user_count’);
    
    // Use this Shortcode to show user [user_count role=”Subscriber”]
Viewing 1 replies (of 1 total)
  • The message means that there already is a function defined called wpb_user_count

    Why I don’t know, it is probably already defined in plugin.

    The simple solution is use another function name e.g.

    // Function to show the user count by role via shortcode
    function longrandomstring123xyz_user_count($atts) {
    $atts = shortcode_atts( array(
    ‘role’ => ”
    ), $atts );
    $user_query = new WP_User_Query( array( ‘role’ => $atts[‘role’] ) );
    // Get the total number of users for the current query. I use (int) only for sanitize.
    $result = (int) $user_query->get_total();
    return $result;
    }
    // Creating a shortcode to display user count
    add_shortcode(‘user_count’, ‘longrandomstring123xyz_user_count’);
    
    // Use this Shortcode to show user [user_count role=”Subscriber”]

    However, if it was my site I’d first investigate ( by searching the code in the plugin & themes directory ) what the other function wpb_user_count does.

Viewing 1 replies (of 1 total)
  • The topic ‘Cannot Redeclare Function’ is closed to new replies.