• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
NameHero Blog

NameHero Blog

Web Hosting Tips & Resources From NameHero

  • Hosting
    • Web Hosting
    • VPS Hosting
    • WordPress Hosting
    • WooCommerce Hosting
  • Reseller
  • Enterprise
  • Domains
  • Account
  • Blog Home
  • Categories

How To Put An Image In The WordPress Title

Bhagwad Park

Published on: April 8, 2019

Categories: WordPress 7

Most blog titles will be plain text. That’s what users expect generally and it’s what they like to see on Google. It’s a fine line between standing out in the SERPS and being seen as spam. It’s possible if you do it carefully. Sometimes you may have seen additional character sets in the titles of some blog posts. Emoticons for example, or emojis. ASCII art etc, are all possible.

In this article, I’ll show you how to put an image in the WordPress title – either before or after. You’ll have to take care of the sizing of the image and make sure that it’s not too large or too small compared to the font of the title. So let’s get started!

1. Upload the Appropriate Image

First, make sure that the image you choose is compact and will fit nicely next to the title. This code won’t automatically resize the image to fit your title. You can size it either before or after you upload it. But if you choose the latter via the WordPress interface, the URL will change.

Use the media editor to upload your image and make a note of the URL at which you can access it. We’ll need this later in step 3.

2. Enable Custom Fields in WordPress Gutenberg

Gutenberg switched up a few interface elements. One of them is the old “Screen Options” bar that used to display at the top of every WordPress editor. This used to control the display of various meta boxes below the post. Now that’s been replaced by a new menu.

Go to the post for which you want the image in the title. Click the three vertical dots on the top righthand side and choose “Options” as shown here:

WordPress Gutenberg Options

In the pop up dialog box, make sure that you check the “Custom Fields” box like this:

Custom Fields in Gutenberg

That’s it – we’ve now enabled custom fields in Gutenberg.

3. Specify the Image URL for the Post with a Custom Field

Custom fields are made up of two pieces of information – a name, and a value. In the screenshot below, you can see that I’m using the name of “title_image”, and the value is the URL we got in step 1:

Name and Value Custom Fields

The “name” is the value we’re going to use to retrieve the image URL. Make sure that it’s the same for all future posts where you need an image in the title as well. Save your post changes.

4. Add the Custom PHP Code

To add custom code to WordPress, follow this tutorial. You can also just add it to your theme’s functions.php file, but I don’t recommend that for several reasons mentioned in the tutorial.

Here’s the code you need to paste:

function image_in_title( $title) {
if(get_post_meta(get_the_ID(), 'title_image', true)) {
$img_source = get_post_meta(get_the_ID(), 'title_image', true);
$title = '' . $title;
}
return $title;
}

add_filter( 'the_title', 'image_in_title');

This code takes the “title_image” value and adds it in front of the title. Save your PHP changes and when you preview your post, you’ll see the image appear in front of it like this:

Image before post Title

If you want, you can also make it appear after the image. Change this line:

        $title = ‘<img class=”icon_title” src=”‘. $img_source .'” />’ . $title;

Into this line:

        $title = $title. ‘<img class=”icon_title” src=”‘. $img_source .'” />’;

And you’re done!

If you know a bit of PHP coding, you can also add conditional statements so that the image only shows up on single pages and not on the home page, or archives. Or you can use this as an inspiration to add image tags next to your category names. The possibilities are endless!

Bhagwad Park Profile Picture
Bhagwad Park

I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!

Reader Interactions

Comments

  1. Shail says

    October 11, 2019 at 3:06 am

    It’s not working for me in WordPress 5.2.3

    Reply
  2. Lad says

    December 21, 2019 at 1:25 pm

    Nope, not working.

    Reply
  3. anup singh says

    April 2, 2020 at 7:46 am

    thanks for this

    Reply
  4. Anup Singh says

    April 8, 2020 at 1:53 am

    Very Nice Post, Please Make Also One More Post On,

    How To Add [Text] After OR Before Title In The WordPress.

    Please Add, Css Class For Help To Change Colour, Font Size, Etc…

    Reply
    • Anup Singh says

      May 6, 2020 at 7:17 am

      Please Add This Post

      Reply
  5. Stephen Blower says

    May 13, 2020 at 1:41 am

    You say Change this line:

    $title = ‘’ . $title;

    Into this line:

    $title = $title. ‘’;

    The first line isn’t in the php snippet?

    Reply
  6. MARC CLAYTON says

    July 28, 2020 at 7:59 am

    Thanks for the post. I suspect this code has a big mistake in it. Please check!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow & Subscribe

Exclusive promos, content and more!

Most Popular Posts

Speed up your site: solving the WordPress slowdown

NameHero’s Recommended WordPress Plugin and Theme Setup (2023)

How To Increase The InnoDB Buffer Pool Size

How To Fix A Stuck All-in-One WP Migration Import

How To Add A Subdomain In Cloudflare

Top Categories

  • WordPress
  • Website Performance
  • Web Hosting
  • Resellers
  • Website Development
  • Website Security
  • VPS Hosting
  • SEO Tips
  • Announcements
  • Domain Registration
NameHero

NameHero proudly provides web hosting to over 40,000 customers with 99.9% uptime to over 750,000 websites.

  • Master Card
  • Visa
  • American Express
  • Discover
  • Paypal
Products
  • Web Hosting
  • VPS Hosting
  • WordPress Hosting
  • WooCommerce Hosting
  • Reseller Hosting
  • Enterprise Hosting
  • Domains
Help & Support
  • NameHero Blog
  • Knowledgebase
  • Announcements
  • Affiliates
Company
  • About Us
  • Contact Sales
  • Reviews
  • Uptime
  • We're Hiring

Copyright © 2023 NameHero, LLC. All rights reserved.

  • Privacy Policy
  • Terms of Use
  • Acceptable Use Policy
  • Payment Policy
  • DMCA