Elementor与MailerLite集成的群组数量限制问题求助
Elementor Pro MailerLite 群组数量上限问题的持久化解决方案
问题背景
Elementor Pro集成MailerLite时,默认仅拉取100个群组,当账号内群组超过100个时,超出部分无法在表单设置中选择。直接修改Elementor核心文件plugins/elementor-pro/modules/forms/classes/mailerlite-handler.php的方法虽能临时解决,但插件更新后修改会被覆盖,需要更持久的方案。
可行解决方案
方案1:利用WordPress HTTP请求钩子修改API参数
通过WordPress的http_request_args过滤器拦截MailerLite的群组API请求,自动添加limit参数来获取更多群组。这种方法无需依赖Elementor内部类结构,兼容性更强。
将以下代码添加到主题的functions.php文件或自定义插件中:
add_filter( 'http_request_args', 'custom_mailerlite_groups_limit', 10, 2 ); function custom_mailerlite_groups_limit( $args, $url ) { // 匹配MailerLite群组API的请求地址 if ( strpos( $url, 'https://api.mailerlite.com/api/v2/groups' ) !== false ) { // 解析URL并添加limit参数(可根据实际需求调整数值,最大支持1000) $url_parts = wp_parse_url( $url ); parse_str( $url_parts['query'] ?? '', $query_params ); $query_params['limit'] = 250; $url_parts['query'] = http_build_query( $query_params ); $args['url'] = http_build_url( $url_parts ); } return $args; }
方案2:扩展Elementor的MailerLite Handler类
如果需要更精准的控制,可以通过自定义插件扩展Elementor的MailerLite_Handler类,重写get_groups方法来设置更高的群组拉取上限。
- 在
wp-content/plugins/目录下创建新文件elementor-mailerlite-group-fix.php,写入以下代码:
<?php /* Plugin Name: Elementor MailerLite 群组数量修复 Plugin URI: Description: 解决Elementor Pro集成MailerLite时群组数量上限100的问题 Version: 1.0 Author: Author URI: */ add_action( 'elementor_pro/init', 'custom_load_mailerlite_handler' ); function custom_load_mailerlite_handler() { if ( class_exists( '\ElementorPro\Modules\Forms\Classes\MailerLite_Handler' ) ) { // 扩展原Handler类 class Custom_MailerLite_Handler extends \ElementorPro\Modules\Forms\Classes\MailerLite_Handler { public function get_groups() { // 设置拉取250个群组,可根据实际需求调整 $results = $this->rest_client->get( 'groups?limit=250' ); $groups = [ '' => esc_html__( 'Select...', 'elementor-pro' ), ]; if ( 200 === $results['code'] ) { foreach ( $results['body'] as $group ) { $groups[ $group['id'] ] = $group['name']; } } return [ 'groups' => $groups, 'fields' => $this->get_fields(), ]; } } // 替换原类的注册动作 remove_action( 'elementor_pro/forms/register_action', [ \ElementorPro\Modules\Forms\Classes\MailerLite_Handler::class, 'register' ] ); add_action( 'elementor_pro/forms/register_action', [ Custom_MailerLite_Handler::class, 'register' ] ); } }
- 登录WordPress后台,找到该插件并启用即可。
注意事项
- 方案1兼容性更强,即使Elementor后续更新内部代码,只要MailerLite API地址不变就有效;方案2需注意Elementor类结构的变化,若后续原类方法被修改,可能需要同步调整。
- MailerLite API的拉取上限最大为1000,可根据实际群组数量调整
limit数值。 - 所有自定义代码请放在主题functions.php或独立插件中,切勿直接修改Elementor核心文件,避免更新丢失。
内容的提问来源于stack exchange,提问作者Andrei
相关产品推荐
相关产品推荐

