Halloween Sale 15% off Ends in See the plans
PHP

Widget Area Above the Footer

Registers an "Above the Footer" widget area and shows its widgets in a responsive row just above the site footer, in classic and block themes.

Don’t have WPIDE yet? Install it on your site

Code

// Name and ID of the new widget area.
$wpide_prefooter_name = 'Above the Footer';
$wpide_prefooter_id   = 'wpide-above-footer';

add_action( 'widgets_init', function () use ( $wpide_prefooter_name, $wpide_prefooter_id ) {
    register_sidebar( array(
        'name'          => $wpide_prefooter_name,
        'id'            => $wpide_prefooter_id,
        'description'   => 'Shown on every page, just above the site footer.',
        'before_widget' => '<div id="%1$s" class="widget %2$s">',
        'after_widget'  => '</div>',
        'before_title'  => '<h2 class="widget-title">',
        'after_title'   => '</h2>',
    ) );
} );

$wpide_prefooter_html = function () use ( $wpide_prefooter_id ) {
    if ( ! is_active_sidebar( $wpide_prefooter_id ) ) {
        return '';
    }

    ob_start();
    dynamic_sidebar( $wpide_prefooter_id );

    $style = '<style>.wpide-above-footer{display:grid;gap:1.5em;grid-template-columns:repeat(auto-fit,minmax(min(100%,240px),1fr));max-width:var(--wp--style--global--wide-size,1200px);margin:2em auto;padding:0 16px}</style>';

    return $style . '<aside class="wpide-above-footer" aria-label="' . esc_attr( 'Above the footer' ) . '">' . ob_get_clean() . '</aside>';
};

// Classic themes: printed when the theme loads footer.php.
add_action( 'get_footer', function () use ( $wpide_prefooter_html ) {
    if ( ! wp_is_block_theme() ) {
        echo $wpide_prefooter_html(); // phpcs:ignore WordPress.Security.EscapeOutput -- widget output.
    }
} );

// Block themes: added before the footer template part.
add_filter( 'render_block_core/template-part', function ( $content, $block ) use ( $wpide_prefooter_html ) {
    static $done = false;

    $slug = isset( $block['attrs']['slug'] ) ? (string) $block['attrs']['slug'] : '';
    $area = isset( $block['attrs']['area'] ) ? (string) $block['attrs']['area'] : '';

    if ( $done || is_admin() || ( 'footer' !== $area && 0 !== strpos( $slug, 'footer' ) ) ) {
        return $content;
    }

    $done = true;

    return $wpide_prefooter_html() . $content;
}, 10, 2 );

Notes

Classic themes print it when they load footer.php; block themes get it before the footer template part.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.