WordPress Multiste, Subsites, Sitemap, Yoast, ACF Pro YouTube
if ( function_exists('acf_add_options_page') ) {
if ( get_current_blog_id() == 1 ) {
acf_add_options_page(array(
'page_title' => 'Sitewide Settings',
'menu_title' => 'Sitewide Settings',
'menu_slug' => 'sitewide-general-settings',
'capability' => 'edit_posts',
'redirect' => false
));
acf_add_options_sub_page(array(
'page_title' => 'Sitewide Head Settings',
'menu_title' => 'Head',
'parent_slug' => 'sitewide-general-settings',
));
acf_add_options_sub_page(array(
'page_title' => 'Sitewide Header Settings',
'menu_title' => 'Header',
'parent_slug' => 'sitewide-general-settings',
));
acf_add_options_sub_page(array(
'page_title' => 'Sitewide Footer Settings',
'menu_title' => 'Footer',
'parent_slug' => 'sitewide-general-settings',
));
acf_add_options_page( array(
'page_title' => 'Mainsite Settings',
'menu_title' => 'Mainsite Settings',
'menu_slug' => 'mainsite-settings',
'capability' => 'edit_posts',
'redirect' => false
));
} else {
acf_add_options_page( array(
'page_title' => 'Subsite Settings',
'menu_title' => 'Subsite Settings',
'menu_slug' => 'subsite-settings',
'capability' => 'edit_posts',
'redirect' => false
));
}
}
add_filter('acf/settings/save_json', function ( $path = '' ) {
$path = get_template_directory() . '/acf-json';
return $path;
});
add_filter('acf/settings/load_json', function ( $paths = array()) {
$paths = array( get_template_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_front_page_only = false;
$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 ( get_sub_field( 'front_page_only' ) == 1 ) {
$field_front_page_only = true;
}
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 === 'front_page_only') {
return $field_front_page_only;
}
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_front_page_only = get_subsites_sitemap_field( 'front_page_only' );
$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>' . network_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_front_page_only, $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_front_page_only, $subsites_post_slugs_to_exclude ) {
$subsites = get_sites( array(
'site__not_in' => get_main_network_id(),
));
if ( ! empty ( $subsites ) ) {
$subsites_sitemap = '';
foreach( $subsites as $subsite ) {
$subsite_blog_id = get_object_vars( $subsite )['blog_id'];
switch_to_blog( $subsite_blog_id );
global $wpseo_sitemaps;
$args = array(
'numberposts' => -1,
'post_status' => array('publish'),
'post_type' => $subsites_sitemap_field_post_type,
'page_id' => $subsites_front_page_only ? get_option( 'page_on_front' ) : get_option( '' )
);
$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;
$attachment_id = get_post_thumbnail_id($post);
if ($attachment_id) {
$image_url = wp_get_attachment_url($attachment_id);
$image_title = get_post_meta($attachment_id, "_wp_attachment_image_alt", true);
$image_caption = wp_get_attachment_caption($attachment_id);
} else {
$image_url = "";
$image_title = "";
$image_caption = "";
}
if ($image_url) {
$url["images"] = array(
array(
"src" => $image_url,
"title" => $image_title,
"alt" => $image_caption,
),
);
}
$subsites_sitemap .= $wpseo_sitemaps->renderer->sitemap_url( $url );
}
}
wp_reset_postdata();
}
restore_current_blog();
}
}
$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);
{
"key": "group_5e83849bd2f38",
"title": "Sitewide Settings",
"fields": [
{
"key": "field_5ea251270754b",
"label": "Sitemap",
"name": "",
"type": "tab",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"placement": "top",
"endpoint": 0
},
{
"key": "field_5ea2eaf8ad3ad",
"label": "Enable Subsites",
"name": "subsites_sitemap_is_enabled",
"type": "true_false",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"message": "",
"default_value": 0,
"ui": 1,
"ui_on_text": "",
"ui_off_text": ""
},
{
"key": "field_5ea2513c0754c",
"label": "Subsites",
"name": "subsites_sitemap",
"type": "group",
"instructions": "",
"required": 0,
"conditional_logic": [
[
{
"field": "field_5ea2eaf8ad3ad",
"operator": "==",
"value": "1"
}
]
],
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"layout": "block",
"sub_fields": [
{
"key": "field_5ea251670754d",
"label": "Slug",
"name": "slug",
"type": "text",
"instructions": "slug-sitemap.xml (Default: subsites-sitemap.xml)",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": ""
},
{
"key": "field_5ea251d30754e",
"label": "Post Type",
"name": "post_type",
"type": "checkbox",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"choices": {
"post": "Post",
"page": "Page"
},
"allow_custom": 0,
"default_value": [],
"layout": "vertical",
"toggle": 0,
"return_format": "array",
"save_custom": 0
},
{
"key": "field_5ea9c94da1853",
"label": "Front page only?",
"name": "front_page_only",
"type": "true_false",
"instructions": "Settings > Reading > A static page (select below) > Front Page",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"message": "",
"default_value": 0,
"ui": 1,
"ui_on_text": "",
"ui_off_text": ""
},
{
"key": "field_5ea9cb01a1854",
"label": "Posts with slugs to exclude",
"name": "slugs_to_exclude",
"type": "repeater",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"collapsed": "",
"min": 0,
"max": 0,
"layout": "table",
"button_label": "",
"sub_fields": [
{
"key": "field_5ea9cb65a1855",
"label": "slug",
"name": "slug",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": ""
}
]
}
]
}
],
"location": [
[
{
"param": "options_page",
"operator": "==",
"value": "sitewide-general-settings"
}
]
],
"menu_order": 0,
"position": "normal",
"style": "default",
"label_placement": "top",
"instruction_placement": "label",
"hide_on_screen": "",
"active": 1,
"description": "",
"modified": 1588187998
}
Hi @brandon-cybermark I am not sure, it has been a while since I worked on a WordPress site, sorry