Code Snippets for GeneratePress

Updated on:

GeneratePress has more than 400,000+ active installations and it is one of the best themes for WordPress.

One of the best things about GeneratePress is the outstanding support they have.

This is a collection of snippets that will help you customize different areas of your WordPress site.

Change The Copyright Message

This is the code snippet you need to change the footer copyright message when you don’t have GeneratePress Premium.

Add this snippet of code to your favorite code snippets plugin.

add_filter( 'generate_copyright','tu_custom_copyright' );
function tu_custom_copyright() {
    ?>
    © 2022 - Your Custom Message
    <?php
}

Reserve Space for Ads

If you want to reduce CLS issues caused by Adsense ads, you can reserve the space wrapping the ad units and adding a bit of CSS

For ads that you place in a specific position then you can wrap the ad script in a <div> with a CSS Class like so:

<div class="ad-min-400">
    <!-- ad script here -->
</div>

Then use some CSS to give it a minimum height:

.ad-min-400 {
    min-height: 400px;
}

/* Change height on smaller screens if required */
@media(max-width: 768px) {
    .ad-min-400 {
       min-height: 400px;
   }
}

Keep in mind that the space will be reserved even if your site visitors are using ad blockers.

How to Hide Sidebar on Tablet and Mobile

This is the code necessary to hide the sidebar on Tablet and Mobile

@media (max-width: 1199px) {
    .sidebar {
        display: none;
    }
    .site-content .content-area {
        width: 100%;
    }
}

How to Make Sticky Sidebar on GeneratePress

Add this CSS to your site to make your sidebar sticky

.inside-right-sidebar {
    position: sticky;
    top: 10px;
}

How to Insert Ads After Headings

If you don’t want to use an Ad Management plugin you can use this code to insert ads after every h2

add_filter( 'the_content', 'insert_element', 20 );
function insert_element( $content ) {
    global $post;
    $inserted_element = 'your ads here';
    if ( is_single() && ! is_admin() ) {
        return prefix_insert_after_h2( $inserted_element, 1, $content );
    }
    return $content;
}
function prefix_insert_after_h2( $insertion, $h2_id, $content ) {
  $closing_h2 = '</h2>';
  $h2s = explode( $closing_h2, $content );
  foreach ($h2s as $index => $h2) {
    if ( trim( $h2 ) ) {
     $h2s[$index] .= $closing_h2.$insertion;
    }
  }
  return implode( '', $h2s );
}

The only problem with this code is that the same ad code will be used to show ads after every heading and that could be a problem when tracking the performance.

Plugins like Ad Inserter will let you add different ad codes so you can monitor the performance of individual ad units.

Remove Schema Generated by the Theme

If you are using an SEO Plugin, you might want to run a few tests and validate the schema generated by the plugin and the GeneratePress theme.

If you find duplicates, consider using some and all of these types of schema:

add_filter( 'generate_is_using_hatom', '__return_false' );
add_filter( 'generate_header_microdata', '__return_false' );
add_filter( 'generate_navigation_microdata', '__return_false' );
JM

Sobre Jose Manuel

I am José Manuel. I am writing about things I know and things that I am learning about WordPress. I hope you find this blog useful.