I am testing a WP installation on my laptop, using local.
PHP 7.4.30, WP 6.4.3
I installed the mc4wp plugin. I configured my Mailchimp account, generated an API key, added it to the plugin, it is connected and happy. It reports that my list has 3 subscribers, which is exact.
In a separate custom plugin, I am adding php code to use the programmatic API in the file mailchimp-for-wp/includes/class_mailchimp.php. I have some success. For instance I can call
$subscriber_count = $mailchimp->get_subscriber_count($list_id);
and I successfully get 3 subscribers, which is exact. However when I call certain APIs such as this one
$has_subscriber = $mailchimp->list_has_subscriber($list_id, “[email protected]”);
the code crashes with the following exception:
[12-Mar-2024 03:47:37 UTC] PHP Fatal error: Uncaught Exception: No service named api was registered. in C:\Users\franc\LocalSites\cac2\app\public\wp-content\plugins\mailchimp-for-wp\includes\class-container.php:38
Stack trace:
#0 C:\Users\franc\LocalSites\cac2\app\public\wp-content\plugins\mailchimp-for-wp\includes\functions.php(28): MC4WP_Container->get('api')
#1 C:\Users\franc\LocalSites\cac2\app\public\wp-content\plugins\mailchimp-for-wp\includes\class-mailchimp.php(557): mc4wp('api')
#2 C:\Users\franc\LocalSites\cac2\app\public\wp-content\plugins\mailchimp-for-wp\includes\class-mailchimp.php(209): MC4WP_MailChimp->get_api()
#3 C:\Users\franc\LocalSites\cac2\app\public\wp-content\plugins\acc-outaouais\acc-outaouais.php(75): MC4WP_MailChimp->list_has_subscriber('20588efba1', 'francois.besset...')
#4 C:\Users\franc\LocalSites\cac2\app\public\wp-content\plugins\acc-outaouais\acc-outaouais.php(93): accou_register_new_user_to_mailchimp('dummy')
#5 C:\Users\franc\LocalSites\cac2\app\public\wp-settings.php(473): include_once('C:\\Users\\franc\\...')
#6 C:\ in C:\Users\franc\LocalSites\cac2\app\public\wp-content\plugins\mailchimp-for-wp\includes\class-container.php on line 38Why would a service inside the mc4wp plugin not be defined? Any idea what is wrong here? I know I am supplying the right API key and list because they are the same as used by the mc4wp plugin. And I tried them using this curl command and it worked:
curl --request GET --url https://us18.api.mailchimp.com/3.0/lists/20588efba1/members/ --user anystring:64xxxxx54a3-us18
I tried deactivation of most plugins, and spent quite a bit of time searching online for an answer, but could not find one. Here is the code I am using. I am also wondering, do I really have to hardcode the API key here? The installed and configured mc4wp has the API key already, why would I have to pass it again programmatically to the plugin?
require_once WP_PLUGIN_DIR . '/mailchimp-for-wp/vendor/autoload.php';
if(!class_exists('MC4WP_MailChimp')) {
wp_die('Please install and activate Mailchimp for WordPress (MC4WP) plugin.');
}
define('MC4WP_API_KEY_FOR_ACCOU', '64xxxxxxxx4a3-us18');
function accou_register_new_user_to_mailchimp($user_id) {
error_log("inside accou_register_new_user_to_mailchimp...");
$api_key = MC4WP_API_KEY_FOR_ACCOU;
error_log("The key is $api_key");
$mailchimp = new MC4WP_MailChimp($api_key);
$list_id = "2xxxxxx1";
$email = "[email protected]";
$subscriber_count = $mailchimp->get_subscriber_count($list_id);
error_log("Mailchimp has $subscriber_count subscribers");
//!!!!! if uncommented, the next line causes an exception !!!!!
$has_subscriber = $mailchimp->list_has_subscriber($list_id, "[email protected]");
error_log("has subscriber = $has_subscriber");
}Other questions I have:
- Is there documentation on the public programmatic functions provided by the plugin?
- Do I have the right way to include dependencies?
- Am I passing the right parameter to new MC4WP_MailChimp()?
