Display the total number of subscribers who are in a list

Ludovic

Member
Hi there,
I just stumbled across the PyCoders subscription page and below their form, there is the number of newsletter subscribers displayed
I was wondering if via the API, there would be a way to do the same with MailWizz?
I'm under WordPress, I can imagine a little snippet that would generate a shortcode to place where I want, but I'm afraid it won't be that simple ...
Thank you for your help!
Warm regards,
Ludovic
 

Attachments

  • Screen Shot 2021-11-23 at 15.58.57.png
    Screen Shot 2021-11-23 at 15.58.57.png
    715.4 KB · Views: 19
Do you use any plugin that integrates with MailWizz?
Nope, I have a habit of either embedding the HTML form with a bit of formatting or going through an Elementor form with a WebHook redirect and an automation service like Pabbly
How comfortable are you with adding PHP code to wordpress?
I easily handle snippets in a must-use plugin or in a functions.php file of a child theme
 
Okay, let me c if i can come up with something for you.
Please paste here the list uid you want to count subscribers in and also your app url
 
Here's the shortcode implementation:
PHP:
function mwz_list_subscribers_count_shortcode( array $atts = [], $content = '' ) {
    $atts = shortcode_atts( [
        'list-uid'  => '',
        'api-key'   => '',
        'api-url'   => '',
    ], $atts );
    
    if ( empty( $atts['list-uid'] ) || empty( $atts['api-key'] ) || empty( $atts['api-url'] ) ) {
        return '';
    }

    if ( ! class_exists( '\EmsApi\Base', false ) ) {
        return false;
    }

    static $config;
    if ( $config === null ) {
        $config = new \EmsApi\Config([
            'apiUrl'    => $atts['api-url'],
            'apiKey'    => $atts['api-key'],
        ]);

        // Now inject the configuration and we are ready to make api calls
        \EmsApi\Base::setConfig($config);
    }

    static $count;
    if ( $count === null ) {
        $endpoint = new \EmsApi\Endpoint\ListSubscribers();
        $response = $endpoint->getSubscribers($atts['list-uid'], $pageNumber = 1, $perPage = 1);

        $count = isset($response->body['data']['count']) ? (int)$response->body['data']['count'] : 0;
    }
    
    return sprintf( __( 'Join over %s subscribers in our list' ), number_format( $count ) );
}
add_shortcode( 'mwz_list_subscribers_count', 'mwz_list_subscribers_count_shortcode' );

And you can use the shortcode like below, where you have to add these params to it:
HTML:
[mwz_list_subscribers_count list-uid="" api-key="" api-url=""]

If you don't want to always add the params to it, you can define your defaults in this code in the above function:
PHP:
$atts = shortcode_atts( [
        'list-uid'  => 'your list uid',
        'api-key'   => 'your api key',
        'api-url'   => 'your api url',
    ], $atts );
So that you don't need to pass them each time when you use your short code, so you can just do:
HTML:
[mwz_list_subscribers_count]


One thing you must do, is to install and include the php-client for mailwizz in your wordpress theme, you will have to do it using composer:
Code:
https://api-docs.mailwizz.com/#getting-started

If you can't use composer, just take the attached archive, unzip it, and place it in your theme, at same level with functions.php.
Then include it in your functions.php, before the above code that adds the shortcode functionality:
PHP:
require __DIR__ . '/mwz-sdk/vendor/autoload.php';

Best of luck.
 

Attachments

  • mwz-sdk.zip
    261.9 KB · Views: 6
He did it !
Allow me to warmly and publicly thank Cristian who volunteered his time for something that was neither urgent nor important, but which will allow a whole generation of WordPressians like me to honor him for life.

For the more novices than me, I put the steps in order:
#1 Download to your child theme folder and unzip the mwz-sdk folder above

Screen Shot 2021-11-24 at 21.22.31.png

#2 Copy / paste the following code into the functions.php file of your child theme

PHP:
require __DIR__ . '/mwz-sdk/vendor/autoload.php';

function mwz_list_subscribers_count_shortcode( array $atts = [], $content = '' ) {
    $atts = shortcode_atts( [
        'list-uid'  => '',
        'api-key'   => '',
        'api-url'   => '',
    ], $atts );

    if ( empty( $atts['list-uid'] ) || empty( $atts['api-key'] ) || empty( $atts['api-url'] ) ) {
        return '';
    }

    static $config;
    if ( $config === null ) {
        $config = new \EmsApi\Config([
            'apiUrl'    => $atts['api-url'],
            'apiKey'    => $atts['api-key'],
        ]);

        // Now inject the configuration and we are ready to make api calls
        \EmsApi\Base::setConfig($config);
    }

    static $count;
    if ( $count === null ) {
        $endpoint = new \EmsApi\Endpoint\ListSubscribers();
        $response = $endpoint->getSubscribers($atts['list-uid'], $pageNumber = 1, $perPage = 1);

        $count = isset($response->body['data']['count']) ? (int)$response->body['data']['count'] : 0;
    }

    return sprintf( __( 'Join over %s subscribers in our list' ), number_format( $count ) );
//    return number_format( $count );
}
add_shortcode( 'mwz_list_subscribers_count', 'mwz_list_subscribers_count_shortcode' );

And if you want, like me, to have ONLY the number you can comment out the line "return sprintf (" and uncomment the line "return number_format ("

Thank you again Cristian, you are a legend!

Result:
Screen Shot 2021-11-24 at 21.11.44.png
 
Back
Top