Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save atazmin/c3303308dc0ac4b934d7c680cab5baa1 to your computer and use it in GitHub Desktop.
Save atazmin/c3303308dc0ac4b934d7c680cab5baa1 to your computer and use it in GitHub Desktop.

WordPress Multisite Modify Title Tags ACF Pro YouTube

functions.php


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
		));		
	}	
}


function get_title_tag_dynamic_field ($field) {
	restore_current_blog();

	switch ($field):
    case "network_title":
			return get_site_option( 'site_name' );
      break;
    case "site_title":
      return get_bloginfo('name');
      break;
    case "page_title":
			return get_the_title();
			break;
		case "brand_site":
			return get_field('brand_site', 'option');
			break;
		case "site_tagline":
			return get_bloginfo('description');
			break;
    default:
			return "Undefined";
			break;
	endswitch;

	switch_to_blog( get_main_network_id() );
}

add_filter( 'pre_get_document_title', function( $title ) {
	$title = '';
	$service_page = get_field('service_page');

	if ( is_front_page() ) {

		switch_to_blog( get_main_network_id() );
		if ( have_rows( 'home_page_title_tag', 'option' ) ):
			while ( have_rows( 'home_page_title_tag', 'option' ) ) : the_row();
				if ( get_row_layout() == 'home_page_title_tag_dynamic_layout' ) :
					$title .= get_title_tag_dynamic_field (get_sub_field( 'home_page_title_tag_dynamic_field' ));
				elseif ( get_row_layout() == 'home_page_title_tag_static_layout' ) :
					$title .= get_sub_field( 'home_page_title_tag_static_field' );
				endif;
			endwhile;
		else:
		endif;
		restore_current_blog();

	} else if ($service_page) {

		switch_to_blog( get_main_network_id() );
		if ( have_rows( 'service_page_title_tag', 'option' ) ):
			while ( have_rows( 'service_page_title_tag', 'option' ) ) : the_row();
				if ( get_row_layout() == 'service_page_title_tag_dynamic_layout' ) :
					$title .= get_title_tag_dynamic_field (get_sub_field( 'service_page_title_tag_dynamic_field' ));
				elseif ( get_row_layout() == 'service_page_title_tag_static_layout' ) :
					$title .= get_sub_field( 'service_page_title_tag_static_field' );
				endif;
			endwhile;
		else:
		endif;
		restore_current_blog();	

	} else {

		switch_to_blog( get_main_network_id() );
		if ( have_rows( 'default_page_title_tag', 'option' ) ):
			while ( have_rows( 'default_page_title_tag', 'option' ) ) : the_row();
				if ( get_row_layout() == 'default_page_title_tag_dynamic_layout' ) :
					$title .= get_title_tag_dynamic_field (get_sub_field( 'default_page_title_tag_dynamic_field' ));
				elseif ( get_row_layout() == 'default_page_title_tag_static_layout' ) :
					$title .= get_sub_field( 'default_page_title_tag_static_field' );
				endif;
			endwhile;
		else:
		endif;
		restore_current_blog();

	}

	return $title;

}, 100, 1 );

add_action( 'wp_head', function () {

	$permalink = esc_url(get_permalink());
	
	switch_to_blog( get_main_network_id() );

		$meta_tags_block_content_attribute = get_field( 'meta_tags_block_content_attribute', 'option' );
		if ( have_rows( 'meta_tag_block_permalink_slugs', 'option' ) ) :
			while ( have_rows( 'meta_tag_block_permalink_slugs', 'option' ) ) : the_row();
				$permalink_slug = get_sub_field( 'permalink_slug' );
				if (strpos($permalink, $permalink_slug) !== false ) :
					echo "\n" .'<meta name="robots" content="' . $meta_tags_block_content_attribute . '" />' . "\n";
				endif;
			endwhile;
		endif;

		restore_current_blog();
	
});


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;
});

acf-json/group_5e7e6f4fcb098.json

{
    "key": "group_5e7e6f4fcb098",
    "title": "Head",
    "fields": [
        {
            "key": "field_5e7e90bfed552",
            "label": "Title Tag",
            "name": "",
            "type": "tab",
            "instructions": "",
            "required": 0,
            "conditional_logic": 0,
            "wrapper": {
                "width": "",
                "class": "",
                "id": ""
            },
            "placement": "top",
            "endpoint": 0
        },
        {
            "key": "field_5e813c82c0cac",
            "label": "Home page",
            "name": "home_page_title_tag",
            "type": "flexible_content",
            "instructions": "Home page (alias Front Page) can be set in Settings > Reading > A static page (select below) Homepage: Page",
            "required": 0,
            "conditional_logic": 0,
            "wrapper": {
                "width": "",
                "class": "acf-title-tag-builder",
                "id": ""
            },
            "layouts": {
                "layout_5e813cc87d792": {
                    "key": "layout_5e813cc87d792",
                    "name": "home_page_title_tag_dynamic_layout",
                    "label": "Dynamic Layout",
                    "display": "block",
                    "sub_fields": [
                        {
                            "key": "field_5e7e9ec4f4e38",
                            "label": "Dynamic Field",
                            "name": "home_page_title_tag_dynamic_field",
                            "type": "select",
                            "instructions": "",
                            "required": 0,
                            "conditional_logic": 0,
                            "wrapper": {
                                "width": "",
                                "class": "",
                                "id": ""
                            },
                            "choices": {
                                "network_title": "Network Title",
                                "site_title": "Site Title",
                                "page_title": "Page Title",
                                "site_tagline": "Site Tagline",
                                "brand_site": "Brand Site (If exists)"
                            },
                            "default_value": [],
                            "allow_null": 0,
                            "multiple": 0,
                            "ui": 1,
                            "ajax": 0,
                            "return_format": "value",
                            "placeholder": ""
                        }
                    ],
                    "min": "",
                    "max": ""
                },
                "layout_5e813d65c0cb6": {
                    "key": "layout_5e813d65c0cb6",
                    "name": "home_page_title_tag_static_layout",
                    "label": "Static Layout",
                    "display": "block",
                    "sub_fields": [
                        {
                            "key": "field_5e7e9ef4f4e39",
                            "label": "Static Field",
                            "name": "home_page_title_tag_static_field",
                            "type": "text",
                            "instructions": "",
                            "required": 0,
                            "conditional_logic": 0,
                            "wrapper": {
                                "width": "",
                                "class": "",
                                "id": ""
                            },
                            "default_value": "",
                            "placeholder": "",
                            "prepend": "",
                            "append": "",
                            "maxlength": ""
                        }
                    ],
                    "min": "",
                    "max": ""
                }
            },
            "button_label": "Add Row",
            "min": "",
            "max": ""
        },
        {
            "key": "field_5e814c3a8c4a6",
            "label": "Service page",
            "name": "service_page_title_tag",
            "type": "flexible_content",
            "instructions": "Service page can be set on each page that is not Home page, the setting is a toggle switch located in sidebar",
            "required": 0,
            "conditional_logic": 0,
            "wrapper": {
                "width": "",
                "class": "acf-title-tag-builder",
                "id": ""
            },
            "layouts": {
                "layout_5e813cc87d792": {
                    "key": "layout_5e813cc87d792",
                    "name": "service_page_title_tag_dynamic_layout",
                    "label": "Dynamic Layout",
                    "display": "block",
                    "sub_fields": [
                        {
                            "key": "field_5e814c3a8c4a7",
                            "label": "Dynamic Field",
                            "name": "service_page_title_tag_dynamic_field",
                            "type": "select",
                            "instructions": "",
                            "required": 0,
                            "conditional_logic": 0,
                            "wrapper": {
                                "width": "",
                                "class": "",
                                "id": ""
                            },
                            "choices": {
                                "network_title": "Network Title",
                                "site_title": "Site Title",
                                "page_title": "Page Title",
                                "site_tagline": "Site Tagline",
                                "brand_site": "Brand Site (If exists)"
                            },
                            "default_value": [],
                            "allow_null": 0,
                            "multiple": 0,
                            "ui": 1,
                            "ajax": 0,
                            "return_format": "value",
                            "placeholder": ""
                        }
                    ],
                    "min": "",
                    "max": ""
                },
                "layout_5e813d65c0cb6": {
                    "key": "layout_5e813d65c0cb6",
                    "name": "service_page_title_tag_static_layout",
                    "label": "Static Layout",
                    "display": "block",
                    "sub_fields": [
                        {
                            "key": "field_5e814c3a8c4a8",
                            "label": "Static Field",
                            "name": "service_page_title_tag_static_field",
                            "type": "text",
                            "instructions": "",
                            "required": 0,
                            "conditional_logic": 0,
                            "wrapper": {
                                "width": "",
                                "class": "",
                                "id": ""
                            },
                            "default_value": "",
                            "placeholder": "",
                            "prepend": "",
                            "append": "",
                            "maxlength": ""
                        }
                    ],
                    "min": "",
                    "max": ""
                }
            },
            "button_label": "Add Row",
            "min": "",
            "max": ""
        },
        {
            "key": "field_5e814c3d8c4a9",
            "label": "Default page",
            "name": "default_page_title_tag",
            "type": "flexible_content",
            "instructions": "Default page will be every page that is not Home page or Service page",
            "required": 0,
            "conditional_logic": 0,
            "wrapper": {
                "width": "",
                "class": "acf-title-tag-builder",
                "id": ""
            },
            "layouts": {
                "layout_5e813cc87d792": {
                    "key": "layout_5e813cc87d792",
                    "name": "default_page_title_tag_dynamic_layout",
                    "label": "Dynamic Layout",
                    "display": "block",
                    "sub_fields": [
                        {
                            "key": "field_5e814c3d8c4aa",
                            "label": "Dynamic Field",
                            "name": "default_page_title_tag_dynamic_field",
                            "type": "select",
                            "instructions": "",
                            "required": 0,
                            "conditional_logic": 0,
                            "wrapper": {
                                "width": "",
                                "class": "",
                                "id": ""
                            },
                            "choices": {
                                "network_title": "Network Title",
                                "site_title": "Site Title",
                                "page_title": "Page Title",
                                "site_tagline": "Site Tagline",
                                "brand_site": "Brand Site (If exists)"
                            },
                            "default_value": [],
                            "allow_null": 0,
                            "multiple": 0,
                            "ui": 1,
                            "ajax": 0,
                            "return_format": "value",
                            "placeholder": ""
                        }
                    ],
                    "min": "",
                    "max": ""
                },
                "layout_5e813d65c0cb6": {
                    "key": "layout_5e813d65c0cb6",
                    "name": "default_page_title_tag_static_layout",
                    "label": "Static Layout",
                    "display": "block",
                    "sub_fields": [
                        {
                            "key": "field_5e814c3d8c4ab",
                            "label": "Static Field",
                            "name": "default_page_title_tag_static_field",
                            "type": "text",
                            "instructions": "",
                            "required": 0,
                            "conditional_logic": 0,
                            "wrapper": {
                                "width": "",
                                "class": "",
                                "id": ""
                            },
                            "default_value": "",
                            "placeholder": "",
                            "prepend": "",
                            "append": "",
                            "maxlength": ""
                        }
                    ],
                    "min": "",
                    "max": ""
                }
            },
            "button_label": "Add Row",
            "min": "",
            "max": ""
        },
        {
            "key": "field_5e7e6fa906d9d",
            "label": "Meta Tags",
            "name": "",
            "type": "tab",
            "instructions": "",
            "required": 0,
            "conditional_logic": 0,
            "wrapper": {
                "width": "",
                "class": "",
                "id": ""
            },
            "placement": "top",
            "endpoint": 0
        },
        {
            "key": "field_5e7e6fcc06d9e",
            "label": "Pages",
            "name": "meta_tag_block_permalink_slugs",
            "type": "repeater",
            "instructions": "If page permalink contains slug, block it it from web search results (example thank-you)",
            "required": 0,
            "conditional_logic": 0,
            "wrapper": {
                "width": "",
                "class": "",
                "id": ""
            },
            "collapsed": "",
            "min": 0,
            "max": 0,
            "layout": "row",
            "button_label": "",
            "sub_fields": [
                {
                    "key": "field_5e7e741f7aa98",
                    "label": "Slug",
                    "name": "permalink_slug",
                    "type": "text",
                    "instructions": "",
                    "required": 0,
                    "conditional_logic": 0,
                    "wrapper": {
                        "width": "",
                        "class": "",
                        "id": ""
                    },
                    "default_value": "",
                    "placeholder": "",
                    "prepend": "",
                    "append": "",
                    "maxlength": ""
                }
            ]
        },
        {
            "key": "field_5e7e73227aa97",
            "label": "Content attribute",
            "name": "meta_tags_block_content_attribute",
            "type": "text",
            "instructions": "",
            "required": 0,
            "conditional_logic": 0,
            "wrapper": {
                "width": "",
                "class": "",
                "id": ""
            },
            "default_value": "",
            "placeholder": "<meta name=\"robots\" content=\"\" \/>",
            "prepend": "",
            "append": "",
            "maxlength": ""
        }
    ],
    "location": [
        [
            {
                "param": "options_page",
                "operator": "==",
                "value": "acf-options-head"
            }
        ]
    ],
    "menu_order": 0,
    "position": "normal",
    "style": "default",
    "label_placement": "top",
    "instruction_placement": "label",
    "hide_on_screen": "",
    "active": 1,
    "description": "",
    "modified": 1585532119
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment