Memory Leak issue with Monarch social sharing plugin

I’ve battled with Elegant Themes and WPEngine over this issue now for the past 2 weeks, and I’m getting absolutely nowhere with either of them.

Here is just one of the errors from the log:

[Wed Apr 26 20:37:29.797099 2017] [:error] [pid 9802] [client] PHP Fatal error: Allowed memory size of 1073741824 bytes exhausted (tried to allocate 218817933 bytes) in /nas/content/live/amberrein/wp-content/plugins/monarch/monarch.php on line 4279, referer:

I’ve already upped the memory limit via wp-config.php:

define( 'WP_MEMORY_LIMIT', '1024M' );
define( 'WP_MAX_MEMORY_LIMIT', '1024M' );

It’s very hit or miss with the memory leak. Some blog posts are fine, while others are not. Then, I go back to visit a working post, and then it’s not loading because of the leak, so for whatever reason, it changes or something. I don’t know. And the memory leak amount is always different, but never higher than what I have the limit set to. But, it also seems that the higher the limit, the longer it takes for a post to load if it does load, which is strange.

here is the entire function that i believe is causing the error. line 4279 starts with $content:

function display_media( $content ) {
$monarch_options = $this->monarch_options;

if ( $this->check_applicability( $monarch_options[ 'sharing_media_post_types' ], 'media' ) ) {
    preg_match_all( '/<img [^>]*>/s', $content, $images_array );
    foreach ( $images_array[0] as $image ) {
        if ( false !== strpos( $image, 'class="ngg_' ) ) {

        preg_match( '@src="([^"]+)"@' , $image , $image_src );

        $icons       = $this->generate_media_icons( $image_src[1] );
        $replacement = '<div class="et_social_media_wrapper">' . $image . $icons . '</div>';
        $content     = str_replace( $image, $replacement, $content );

return $content;


What can I do to resolve this issue, since Elegant Themes and WPEngine are no help? I don’t want to have to disable the plugin as the client wants to keep it.

Read more here: Memory Leak issue with Monarch social sharing plugin

Leave a Reply

Your email address will not be published. Required fields are marked *