• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here:Home » How to Add Auto Anchor to WordPress Headings

By Abhishek Ghosh June 7, 2023 7:23 pm Updated on June 7, 2023

How to Add Auto Anchor to WordPress Headings

Advertisement

These days the search engine can understand the meaning of the headings within an article. Whether you add a table of content, anchor text etc or not, they may show the links on the search engine result page. This happens with the content of our site. If there is step 1, step 2, and step 3 headings; Google search engine adds links on the search engine result page. However, adding links to the headers in an automated way can help the search engines more.

The result after following this article will be a visible anchor icon on hover beside the headings with an addressable URL. That is why we commonly see in GitHub readme files or documents of Microsoft. It is easy to link a particular header, apart from being good for SEO.

Manually adding anchors to heading or what WordPress refers to as page jumps are already has a document for the newer WordPress versions and not difficult to use by the users who depend on block editors. However, not everyone uses WordPress in the same way. Particularly the websites in development prefer to use the classic editor and write in text/HTML format. For such users, there are two ways to add an auto anchor to WordPress headings.

Advertisement

---

How to Add Auto Anchor to WordPress Headings

 

Option 1: Use a WordPress Plugin

 

Probably the first plugin was created by Benedikt Bergmann, named Auto Anchor.

There is a similar plugin named Add Anchor Links. Simply activate it, on the settings page, and select posts as the target. You’ll hardly adjust its CSS. It is extremely easy and does the job nicely.

Both of the developers are reputed and you can rely on any of them. As the thing is around the posts, avoid testing any other bloated plugin on your production site.

 

Option 2: Use PHP Snippet and CSS

 

You can directly use a PHP snippet to find the headers and add the links with icons. The rest of the work CSS will do, such as adding the hover effect. The above plugin’s this file will show you the logic of how the automatic thing is happening:

Vim
1
https://github.com/vyskoczilova/add-anchor-links/blob/master/include/class-add-anchor-links.php

The above-mentioned developer Benedikt Bergmann already has a great article on it and a PHP snippet on his GitHub repo. Below is his written code, you have to add this on your theme’s functions.php file or you can use it with our plugin for adding PHP snippets:

Vim
1
2
3
4
5
6
7
8
9
10
11
function auto_id_headings( $content ) {
$content = preg_replace_callback( '/(\<h[1-6](.*?))\>(.*)(<\/h[1-6]>)/i', function( $matches ) {
if ( ! stripos( $matches[0], 'id=' ) ) :
$heading_link = '<a href="#' . sanitize_title( $matches[3] ) . '" class="heading-anchor-link"><i class="fas fa-link"></i></a>';
$matches[0] = $matches[1] . $matches[2] . ' id="' . sanitize_title( $matches[3] ) . '">' . $heading_link . $matches[3] . $matches[4];
endif;
return $matches[0];
}, $content );
return $content;
}
add_filter( 'the_content', 'auto_id_headings' );

Here is his CSS file:

Vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
h2 a.heading-anchor-link,
h3 a.heading-anchor-link,
h4 a.heading-anchor-link,
h5 a.heading-anchor-link,
h6 a.heading-anchor-link{
opacity:0;
font-size: 1rem;
position: absolute;
left: 10px;
transition: opacity 0.2s linear;
}
h2:hover a.heading-anchor-link,
h3:hover a.heading-anchor-link,
h4:hover a.heading-anchor-link,
h5:hover a.heading-anchor-link,
h6:hover a.heading-anchor-link{
opacity:0.7;
}
h2:hover a.heading-anchor-link:hover,
h3:hover a.heading-anchor-link:hover,
h4:hover a.heading-anchor-link:hover,
h5:hover a.heading-anchor-link:hover,
h6:hover a.heading-anchor-link:hover{
opacity:0.9;
}

Facebook Twitter Pinterest

Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to How to Add Auto Anchor to WordPress Headings

  • Measure the value of link building

    One of the most difficult skills for a doing SEO of a website is to estimate the relative value of a link on a web page (page and location on the latter). The subject is complex because it is impossible to define an exact value without knowing the exact algorithm of Google, which of course is kept secret.

  • Nginx WordPress Installation Guide (All Steps)

    This is a Full Nginx WordPress Installation Guide With All the Steps, Including Some Optimization and Setup Which is Compatible With WordPress DOT ORG Example Settings For Nginx.

  • Create mouse hover effect for button using CSS sprite

    You must have noticed buttons in various websites that change its appearance on mouse hover. Here is tutorial on how to create very easily.

  • WordPress Highlighter Marker Effect Hyperlink With CSS

    Here is How to Get WordPress Highlighter Marker Effect Hyperlink With CSS With Realistic Colors Like Faber Castell Highlighter Marker Pen.

performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (24.3K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • Hybrid Multi-Cloud Environments Are Becoming UbiquitousJuly 12, 2023
  • Data Protection on the InternetJuly 12, 2023
  • Basics of BJT TransistorJuly 11, 2023
  • What is Confidential Computing?July 11, 2023
  • How a MOSFET WorksJuly 10, 2023
PC users can consult Corrine Chorney for Security.

Want to know more about us?

Read Notability and Mentions & Our Setup.

Copyright © 2023 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy