1

My theme has a top menu bar with accessibility shortcuts (jump to content, high contrast mode, etc). I need to include a link to an informational page describing the accessibility features available to visitors.

This is a project for an University multisite network, with a lot of hosted sites, so I need a solution that doesn't require manually creating the page on each site. I also want to be able to easily update this content on future versions of the theme (i.e., inserting the content in the database of several sites may not be a good idea).

I'm considering linking to the site's home page with some URL parameter, e.g. site1.example.com/?info=accessibility and addressing the content in the index.php, but that doesn't look too elegant.

Any advice on a cleaner way to implement this?

Thanks in advance!

2 Answers 2

1

I am assuming the following setup from reading your question:

site.network.com

site-2.network.com

site-3.network.com

where the main site exists on network.com (i.e. blog id 1). All sites (or at least all subdomain sites) share the same theme.

You need to be able to create a single content page and have that content available on all sites, i.e. *.network.com/some-page/


The below covers using a page on the main site (id 1) as the content source (allowing you to update/edit that content via the editor), and a template file for a matching page title.

You could include static content the same way. (see Notes on Step 1 at the end).


Step 1

You can add a page template to the theme for a specific page, say, named Accessibility by duplicating the page.php and renaming it page-accessibility.php

Step 2

In page-accessibility.php, we will remove the normal loop, and replace it with some code to get a different page's content: the Accessibility page from the main site; using switch_to_blog().

If the main site id is 1, the template page page-accessibility.php might look something like this:

get_header(); 

switch_to_blog(1);
$post = get_page_by_title( 'Accessibility' );

if ($post) {
    $content    = do_shortcode( html_entity_decode( $post->post_content ) );
}
restore_current_blog();

?>

    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">

        <?php echo $content; ?>

        </main><!-- #main -->
    </div><!-- #primary -->

<?php
get_sidebar();
get_footer();

Step 3

Create an empty page named Accessibility for all sites. Rather than run something that would use switch_to_blog() to create a page for each one, we could instead check for the existence of the page on init of the site, and only create it if it doesn't exist.

functions.php of theme

add_action( 'init', 'set_default_page' );

function set_default_page() {

  check_exists_create_page( 'Accessibility' ); //again, checking by title
}


function check_exists_create_page( $title ) {

  if ( get_page_by_title( $title ) == NULL ) {

      $args = array(
              'post_title'      => $title,
              'post_content'    => '',
              'post_status'     => 'publish',
              'post_author'     => 1,
              'post_type'       => 'page'
        );

        wp_insert_post( $args ); 
  }

}

Notes on Step 1

You could also use this template hierarchy to include static content. Remove the normal loop, and add your content directly or include another php file, etc. Then skip to Step 3 and create a page with a name that matches the page-{your-page-name}.php pattern for each site.

Notes on Step 2

If you are wanting to use a site other than id1, you can always retrieve the id of the site you wish to use via get_current_blog_id()

get_page_by_title() uses title not slug. To get a page titled My New Page:

get_page_by_title( 'My New Page' )

Here I am just linking more info on do_shortcode() and html_entity_decode(). And for switch_to_blog() and restore_current_blog().

Notes on Step 3

There may be a better hook than init. You could also add a conditional check if you wish to exclude some sites from this page check/creation. See my answer on a different question if you need that.

Be aware during testing that a page in Trash still exists.

1
  • Thanks for the comprehensive and didactical answer! I'll use the page template with my static content to keep everything contained inside the theme. Commented Jul 31, 2017 at 3:22
0

If a plugin would work - see the WP Accessibility plugin. In Settings, 'Add Skiplinks' section, there's an option to add your own link (link or container ID). Could point to a central page?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.