15 Code Snippets for GeneratePress

Manuel campos

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.

You can check my lists of code snippets to add Adsense ads units without a plugin

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' );

How to Remove Post Navigation

If you don’t have GeneratePress Premium, you can add this code snippet to your functions.php file and the post navigation will be gone after that.

add_filter( 'generate_show_post_navigation', '__return_false' );

Change the Background Color of the Hamburger Menu

Add this CSS to your site to make the hamburger menu transparent

.mobile-menu-control-wrapper .menu-toggle, .mobile-menu-control-wrapper .menu-toggle:hover, .mobile-menu-control-wrapper .menu-toggle:focus, .has-inline-mobile-toggle #site-navigation.toggled {
    background-color: transparent;
}

How to Insert an Ad Before the Content

This code will add ad units before the content on pages and posts.

The code also helps exclude displaying that ad unit on pages such as the homepage, you just need to add the post ID number within the parenthesis.

//Ad Before main Content
function my_ad1() {
  if ( !is_page(114719) && ( is_single() || is_page() ) ) {
    echo do_shortcode('[ad1]');
  }
}
add_action( 'generate_before_content', 'my_ad1' );
Wanna buy me a Coffee?
JM
About me
I am José Manuel. I am writing about things I know and things that I am learning about WordPress. I hope you find the content of this blog useful.
Linktree
Care for Site Speed?
I think you can’t go wrong with Cloudways, CloudFlare, and Perfmatters.
Perfmatters Cloudways

WPSurfer.com

Resources to host, secure, speed up, rank, and monetize your WordPress sites.
Plugins
Services
Connect
LinkedIn
Facebook
LinkTree