wordpress 5.7

While most of the new features for the latest WP versions have been concentrated on the block editor, and this way mostly related to JS dependencies and functionalities, some new functions and hooks offer interesting possibilities.

The next relevant WP release will be the WordPress 5.7, to be released in the next few days. From the developers’ notes we extract some new functions and hooks that can actually provide new opportunities for customizing code and build plugins or useful scripts.

WP 5.7 – parent posts

The parent posts can now be managed with two new functions: a template tag and a conditional tag. Function names changed a bit from 5.7-beta and now will be used as get_post_parent() and has_post_parent(). A combined example has already been issued for this new functions:

<?php if ( has_post_parent( get_the_ID() ) ) : ?>
    <a href="<?php the_permalink( get_post_parent( get_the_ID() ) ); ?>">
        <?php
        echo esc_html( sprintf(
            __( 'Back to parent page: %s', 'text-domain' ),
            get_the_title( get_post_parent( get_the_ID() ) )
        ) );
        ?>
    </a>
<?php endif; ?>

WP 5.7 – checking public visibility of a post

The new function is_post_status_viewable() allows us to check whether a post is publicly viewable or not. A simple example has been provided, showing the functionality is based on the post status to determine the visibility.

<?php
global $post;
$current_post_status = get_post_status( $post );
if ( is_post_status_viewable( $current_post_status ) ) {
    echo 'This post uses a public post status';
} else {
    echo 'This post uses a non public post status';
}

WP 5.7 – targetting specific blocks

A new filter now will allow targetting a specific type of block. Before, hooks were general, which forced a larger amount of code when the coder was looking for change the behavior or views for a particular block type.

The filter render_block_{$this->name} allows a fine tunning. In the new filter, {$this->name} represents the name or slug of the block as it was registered. For example, the paragraph block, registered with a name of core/paragraph can be filtered using render_block_core/paragraph.

/**
 * Wrap all core/paragragh blocks in a <div>.
 *
 * @param string $block_content The block content about to be appended.
 * @param array  $block         The full block, including name and attributes.
 * @return string Modified block content.
 */
function wporg_paragraph_block_wrapper( $block_content, $block ) {
    $content  = '<div class="my-custom-wrapper">' . $block_content . '</div>';
    return $content;
}
add_filter( 'render_block_core/paragraph', 'wporg_paragraph_block_wrapper', 10, 2 );

WordPress 5.7 – security related improvements

WordPress 5.7 adds a handful of new functions that enables passing attributes, such as async or nonce, to both regular and inline script tag attributes. This creates a path forward for enabling a Content-Security-Policy (or CSP) in Core, plugins, and themes. Four new functions deal with this aspect:

A more detailed explanation on these new functions can be found here.

Also, WordPress 5.7 introduces a new function wp_is_using_https(), which returns true if both the “Site Address” (home_url()) and “WordPress Address” (site_url()) are using HTTPS as their scheme. In addition to providing a single function for checking whether HTTPS is being used, a new detection function wp_is_https_supported() can be called to check whether the environment supports HTTPS correctly. 

WordPress 5.7 – Cron API error handling

Almost all the main functions and filters related to the Cron API have their error handling improved. Instead of returning a mere “false”, they now offer more informational behavior. A new $wp_error parameter has been inserted in the functions:

  • wp_schedule_single_event()
  • wp_schedule_event()
  • wp_reschedule_event()
  • wp_unschedule_event()