Unverified

Name

add custom title tags, meta descriptions, and alt tags

About

This code will add custom title tags, meta descriptions, and alt tags to all pages on your website. The title tag will include the most important keywords that you want to rank for, the meta description will describe the content of the page in a way that is both informative and engaging, and the alt tag will describe the image in a way that is both informative and engaging.

Language

PHP

Rating

Voted: 0 by 0 user(s)

Codevault

WP-Snippets-

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

History

Last modified:

31/08/2024

Important Note

This snippet has the following status:

Unverified

This snippet has not been verified, use with caution and at your own risk. See details provided by author in sidebar and click below to find out more.

add custom title tags, meta descriptions, and alt tags

 
                    
1function add_custom_title_tags() {
2 global $post;
3 
4 // Get the title of the page
5 $title = $post->post_title;
6 
7 // Get the content of the page
8 $content = $post->post_content;
9 
10 // Get the keywords that are used on the page
11 $keywords = get_keywords($content);
12 
13 // Generate the title tag
14 $title_tag = sprintf('%s - %s', $title, implode(', ', $keywords));
15 
16 // Generate the meta description
17 $meta_description = sprintf('Learn more about %s at %s', $title, get_permalink($post->ID));
18 
19 // Generate the alt tag for the first image on the page
20 $first_image = get_first_image($content);
21 $alt_tag = sprintf('A photo of %s', $first_image);
22 
23 // Add the title tag, meta description, and alt tag to the page
24 echo '<title>', esc_html($title_tag), '</title>';
25 echo '<meta name="description" content="', esc_attr($meta_description), '" />';
26 echo '<img src="', esc_url($first_image), '" alt="', esc_attr($alt_tag), '" />';
27}
28 
29add_action('wp_head', 'add_custom_title_tags');

0

Related Snippets

Please see some snippets below related to this snippet..

General

AI Verified

0

Hide Update Nags

Added: 11 months ago

Last Updated: 11 months ago

General

AI Verified

1

Smooth scroll for GeneratePress anchor links

Added: 2 years ago

Last Updated: 1 year ago

If you're having problems with 'smooth scroll' while using GeneratePress this PHP snippet will sort it out! It will make it smooth instead of just 'jumping' to the anchor link destination

General

Unverified

0

Highlight-Text

Added: 3 months ago

Last Updated: 3 months ago

Highlight Text within a Text Editor or a Heading

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

General

Unverified

0

add custom title tags, meta descriptions, and alt tags

Added: 1 year ago

Last Updated: 2 weeks ago

This code will add custom title tags, meta descriptions, and alt tags to all pages on your website. The title tag will include the most important keywords that you want to rank for, the meta descripti...