Genesis 2.6

Posted on Leave a comment
5/5 - (491 votes)

Genesis 2.6 is actively under development by the StudioPress team with contributions from volunteers like Gary Jones, Lee Anthony and several others.

Today they announced the availability of 2.6 beta here: https://studiopress.blog/genesis-2-6-beta/

The ‘Added’ section of the change log reads:

  • Add contextual filter for `content` passed through the Markup API.
  • Add filter for 404 page title.

I installed the beta to give it a spin and want to share my understanding of the above.

genesis/lib/functions/markup.php adds

/**
 * Contextual filter to modify 'content'.
 *
 * @since 2.6.0
 *
 * @param string $content Content being passed through Markup API.
 * @param array  $args  Array with markup arguments.
 *
 * @see genesis_markup $args Array.
 */
$content = apply_filters( "genesis_markup_{$args['context']}_content", $args['content'], $args );

“Content” here does not refer to the main.content element or entry content but the actual content of an HTML element that uses the Markup API.

As an example, let’s take a look inside 404.php.

As of today, it has

genesis_markup( array(
    'open'    => '<h1 %s>',
    'close'   => '</h1>',
    'content' => __( 'Not found, error 404', 'genesis' ),
    'context' => 'entry-title',
) );

It will most likely be updated to

genesis_markup( array(
    'open'    => '<h1 %s>',
    'close'   => '</h1>',
    'content' => apply_filters( 'genesis_404_entry_title', __( 'Not found, error 404', 'genesis' ) ),
    'context' => 'entry-title',
) );

to ensure backward compatibility with those that might be using the static genesis_404_entry_title filter when 2.6 is final.

genesis_markup() function is being invoked and is being passed an array in which one of the keys is content.

Let’s say the objective is to change the default title of 404 pages from “Not found, error 404” to something friendlier like “Oops! This page does not exist.”.

We can use the new dynamic contextual genesis_markup_{$args['context']}_content filter hook for this like so:

add_filter( 'genesis_markup_entry-title_content', 'custom_change_404_title' );
/**
 * Change the title of 404 pages.
 *
 * @param  string $title Current title.
 * @return string Modified title.
 */
function custom_change_404_title( $title ) {
    if ( is_404() ) {
        $title = __( 'Oops! This page does not exist.', 'my-theme-text-domain' );
    }

    return $title;
}

The context in this case is entry-title as can be seen in the call to genesis_markup().

But since it is generic and is also used in other places we need to use the if conditional to check that we are dealing with WordPress’ 404 error pages.

There is no need to pass the second optional parameter of $args since we don’t need that data.

If you are a curious type like I am, you would have by now also gone through the other parts of markup.php.

Here is a sample snippet to change the 404 error pages’ title markup from h1 to h2 (don’t ask me why anyone would ever want to do that. I am only interested in the technicality):

add_filter( 'genesis_markup_entry-title_open', 'custom_change_open_tag' );
/**
 * Change h1 opening tag to h2.
 *
 * @param  string $open HTML tag being processed by the API.
 * @return string       Modified HTML tag.
 */
function custom_change_open_tag( $open ) {
    if ( is_404() ) {
        $open = str_replace( '<h1', '<h2', $open );
    }

    return $open;
}

add_filter( 'genesis_markup_entry-title_close', 'custom_change_close_tag' );
/**
 * Change h1 closing tag to h2.
 *
 * @return string       Modified HTML tag.
 */
function custom_change_close_tag() {
    if ( is_404() ) {
        $close = '</h2>';
    }

    return $close;
}

Final tip:

Here’s the default HTML output of the 404 page title:

<h1 class="entry-title" itemprop="headline">Not found, error 404</h1>

Wondering where class="entry-title" is coming from?

Leave a Reply

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