Support

Account

Home Forums Backend Issues (wp-admin) Use of acf/init causing server overload of child processes

Helping

Use of acf/init causing server overload of child processes

  • With the recent updates with ACF/Init being required. I am using a hook into Yoast to load my custom sitemaps. When I upload to a production server, the server overloads with child processes that cant exit. This results in the server locking up having to be restarted.

    add_action('acf/init', 'sitemap_acf_init');
    function sitemap_acf_init() {
    
    	if ( function_exists('acf_add_options_page') ) {
    		 
    
    			acf_add_options_sub_page(array(
    				'page_title' 	=> 'Sitemap Settings',
    				'menu_title'	=> 'Sitemap Settings',
    				'menu_slug' 	=> 'sitewide-general-settings',
    				'capability'	=> 'edit_posts',
    				'parent_slug' => 'cybermark_page',
    				'redirect'		=> false
    			));
    		
    	}
    
    	add_filter('acf/settings/save_json', function ( $path = '' ) {
    		$path = get_stylesheet_directory() . '/acf-json';
    		return $path;
    	});
    
    	add_filter('acf/settings/load_json', function ( $paths = array()) {
    		$paths = array( get_stylesheet_directory() . '/acf-json' );
    		return $paths;
    	});
    
    	if ( $_SERVER['HTTP_HOST'] == 'localhost' ) {
    		add_filter( 'wpseo_enable_xml_sitemap_transient_caching', '__return_false');
    	}
    
    	function get_subsites_sitemap_field( $field ) {
    
    		$field_slug = 'subsites';
    		$field_post_type = [];
    		$field_slugs_to_exclude = [];
    
    		if ( have_rows( 'subsites_sitemap', 'option' ) ) :
    			while ( have_rows( 'subsites_sitemap', 'option' ) ) : the_row();				
    				$sitemap_slug = get_sub_field( 'slug' );
    
    				if ( $sitemap_slug !== '' ) {
    					$field_slug  = $sitemap_slug;
    				}
    
    				$post_type_array = get_sub_field( 'post_type' );
    				$post_type_array_length = count($post_type_array);
    				$count = 0;
    
    				if ( $post_type_array ):
    					foreach ( $post_type_array as $post_type_item ):
    						$field_post_type[$count] = $post_type_item['value'];
    						$count++;
    					endforeach;
    				endif;
    								
    
    				if ( have_rows( 'slugs_to_exclude' ) ) : 
    					while ( have_rows( 'slugs_to_exclude' ) ) : the_row();
    						array_push( $field_slugs_to_exclude, get_sub_field( 'slug' ) );		
    					endwhile;
    				endif;
    			endwhile;
    		endif;
    
    		if ( $field === 'slug' ) {
    			return $field_slug;
    		}	
    
    		if ( $field === 'post_type') {
    			return $field_post_type;
    		}
    
    		if ( $field === 'slugs_to_exclude') {
    			return $field_slugs_to_exclude;
    		}	
    	}
    
    	$subsites_sitemap_is_enabled = get_field( 'subsites_sitemap_is_enabled', 'option' );
    	$subsites_sitemap_field_slug = get_subsites_sitemap_field( 'slug' );
    	$subsites_sitemap_field_post_type = get_subsites_sitemap_field( 'post_type' );
    	$subsites_post_slugs_to_exclude = get_subsites_sitemap_field( 'slugs_to_exclude' );
    
    	add_filter( 'wpseo_sitemap_index', function ( $subsites_sitemap_list ) use ( $subsites_sitemap_field_slug, $subsites_sitemap_field_post_type, $subsites_sitemap_is_enabled ) {	
    		if( $subsites_sitemap_is_enabled == 1 ) {
    
    			global $wpseo_sitemaps;
    			$date = $wpseo_sitemaps->get_last_modified($subsites_sitemap_field_post_type);
    			$subsites_sitemap_list .= 
    			'<sitemap>
    			<loc>' . site_url('/') . $subsites_sitemap_field_slug . '-sitemap.xml</loc>
    			<lastmod>' . htmlspecialchars( $date ) . '</lastmod>
    			</sitemap>';
    
    			return $subsites_sitemap_list;
    
    		}	
    	});
    
    	function strpos_in_array( $haystack, $needle, $offset = 0 ) {
    
    		if( !is_array( $needle) ) {
    			$needle = array($needle);
    		}
    
    		foreach( $needle as $query ) {
    			if( strpos($haystack, $query, $offset) !== false ){
    				return true; 
    			}
    		}
    
    		return false;
    	}
    
    	add_action( 'init', function () use ( $subsites_sitemap_field_slug, $subsites_sitemap_field_post_type, $subsites_sitemap_is_enabled, $subsites_post_slugs_to_exclude ) {	
    		if ( $subsites_sitemap_is_enabled == 1 ) {
    
    			global $wpseo_sitemaps;
    			
    			if ( isset( $wpseo_sitemaps ) && ! empty ( $wpseo_sitemaps ) ) {
    				$wpseo_sitemaps->register_sitemap( $subsites_sitemap_field_slug, function () use ( $subsites_sitemap_field_post_type, $subsites_post_slugs_to_exclude ) {
    				
    				
    							global $wpseo_sitemaps;
    
    							$args = array(
    								'numberposts' => -1,
    								'post_status' => array('publish'),
    								'post_type' => $subsites_sitemap_field_post_type
    							);			
    							
    							$posts = get_posts( $args );				
    			
    							if ( !empty( $posts ) ) {
    			
    								$chf = 'weekly';
    								$pri = 1.0;
    			
    								foreach ( $posts as $post ) {
    									setup_postdata( $post );
    
    									if ( !strpos_in_array( get_the_permalink( $post->ID ), $subsites_post_slugs_to_exclude, 0) ) {
    
    										$url = array();
    				
    										if ( isset( $post->post_modified_gmt ) && $post->post_modified_gmt != '0000-00-00 00:00:00' && $post->post_modified_gmt > $post->post_date_gmt ) {
    											$url['mod'] = $post->post_modified_gmt;
    										} 
    										else {
    											if ( '0000-00-00 00:00:00' != $post->post_date_gmt ) {
    												$url['mod'] = $post->post_date_gmt;
    											} 
    											else {
    												$url['mod'] = $post->post_date;
    											}
    										}
    										$url['loc'] = get_the_permalink( $post->ID );
    										$url['chf'] = $chf;
    										$url['pri'] = $pri;
    						
    										$subsites_sitemap .= $wpseo_sitemaps->renderer->sitemap_url( $url );
    					
    									}
    								}
    									wp_reset_postdata();
    							}				
    				
    					$sitemap  = '<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd http://www.google.com/schemas/sitemap-image/1.1 http://www.google.com/schemas/sitemap-image/1.1/sitemap-image.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . PHP_EOL;
    					$sitemap .= $subsites_sitemap . '</urlset>';
    					$wpseo_sitemaps->set_sitemap($sitemap);
    				
    				} );
    
    			}
    		}
    	}, 10, 3);
    }
  • I have no idea why this is causing your problem. But I can say that you have put far too much inside of your acf/init function.

    For example, nested functions, these do not need to be in there. Also addding other filters like acf/settings/load_json does not need to be added in this way.

    The only thing that you can’t do is use get_field() and other function to get values from ACF fields before acf/init

    As far as I can see the only things that you need to worry about calling after acf/init are the following lines.

    
    $subsites_sitemap_is_enabled = get_field( 'subsites_sitemap_is_enabled', 'option' );
    $subsites_sitemap_field_slug = get_subsites_sitemap_field( 'slug' );
    $subsites_sitemap_field_post_type = get_subsites_sitemap_field( 'post_type' );
    $subsites_post_slugs_to_exclude = get_subsites_sitemap_field( 'slugs_to_exclude' );
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.