Why would you need to replace the WordPress widget’s title tag?
There are situations when you purchase a theme or get a free theme that was created without any SEO in mind so the widgets title tags are either an h2 or an h3 and you certainly don’t want that.
How can you replace the WordPress widget title tag?
If you want to change the heading h1-h6 tags to a paragraph for SEO purposes in any genesis framework theme then you can use the following php function:
Where to add the php code so change the WordPress widget title tag?
You have a couple of options but here are two that work:
Option 1: Modify your theme or child theme’s functions.php file
Using your preferred method — an FTP program like FileZilla FTP or Transmit App or any other file manager (even cPanel’s file management works fine) — look for your functions.php file which usually is in wp-content/themes/[your-theme or your-child-theme]/functions.php
Edit your theme’s functions.php file and insert the code there
Option 2: Install the Code Snippets plugin from WordPress.org
- Go to Plugins -> Add New and search for Code Snippets, install and activate it;
- In WP Admin go to Snippets -> Add New (in the sidebar on the left);
- Add a snippet name (I chose “Custom widget heading tag”) and paste the code:
- Make sure you run the snippet everywhere (have that option selected)
Change WordPress’ widget title to h2 or other html tag using the php code below
<?php
// php function to replace all widgets titles to paragraphs
add_action( 'after_setup_theme', function() {
add_action( 'register_sidebar, function( $sidebar ) {
global $wp_registered_sidebars;
Sid = $sidebar['id'];
$tag = 'p'; // replace this to anything you wish
$sidebar[ 'before_title' ] = sprintf('<%s class="widget-title subheading heading-size-3">', $tag);
$sidebar[ 'after_title' ] = sprintf('</%s>', $tag);
$wp_registered_sidebars[ $id ] = $sidebar;
)):
));
Now you have to inspect your front-end code to make sure the widget title tag was updated.
Leave a Reply