Skip to content

Commit 72020ee

Browse files
authored
SEO/Social meta: First pass to add meta and opengraph tags (#116)
* SEO/Social meta: First pass to add meta and opengraph tags * Update to use main site default image
1 parent 58fb5bc commit 72020ee

File tree

3 files changed

+198
-23
lines changed

3 files changed

+198
-23
lines changed

source/wp-content/themes/wporg-themes-2024/functions.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
require_once( __DIR__ . '/inc/embed.php' );
1010
require_once( __DIR__ . '/inc/i18n.php' );
1111
require_once( __DIR__ . '/inc/rest-api.php' );
12+
require_once( __DIR__ . '/inc/seo-social-meta.php' );
1213

1314
// Block files
1415
require_once( __DIR__ . '/src/business-model-notice/index.php' );
@@ -69,6 +70,9 @@ function() {
6970

7071
// Remove the "By…" from the author name block.
7172
remove_filter( 'render_block_core/post-author-name', 'WordPressdotorg\Theme\Parent_2021\Gutenberg_Tweaks\render_author_prefix', 10, 2 );
73+
74+
// Re-enable pagination meta tags.
75+
remove_filter( 'wporg_rel_next_pages', '__return_zero' );
7276
}
7377
);
7478

source/wp-content/themes/wporg-themes-2024/inc/block-config.php

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
namespace WordPressdotorg\Theme\Theme_Directory_2024\Block_Config;
77

88
use WP_HTML_Tag_Processor, WP_Block_Supports;
9-
use function WordPressdotorg\Theme\Theme_Directory_2024\{ get_query_tags, get_tag_labels, wporg_themes_get_feature_list };
9+
use function WordPressdotorg\Theme\Theme_Directory_2024\{ get_query_tags, wporg_themes_get_feature_list };
10+
use function WordPressdotorg\Theme\Theme_Directory_2024\SEO_Social_Meta\{get_archive_title};
1011

1112
add_filter( 'wporg_query_total_label', __NAMESPACE__ . '\update_query_total_label', 10, 2 );
1213
add_filter( 'wporg_query_filter_options_layouts', __NAMESPACE__ . '\get_layouts_options' );
@@ -305,39 +306,20 @@ function update_archive_title( $block_content, $block, $instance ) {
305306
return '';
306307
}
307308

308-
$author = isset( $wp_query->query['author_name'] ) ? get_user_by( 'slug', $wp_query->query['author_name'] ) : false;
309309
$current_browse = $wp_query->query['browse'] ?? false;
310-
$tags = get_query_tags();
311310

312311
if ( is_front_page() && ! $current_browse && ! is_paged() ) {
313312
return '';
314313
}
315314

316-
if ( $author ) {
317-
// translators: %s Author name.
318-
$title = sprintf( __( 'Author: %s', 'wporg-themes' ), $author->display_name );
315+
$title = get_archive_title();
319316

317+
$author = isset( $wp_query->query['author_name'] ) ? get_user_by( 'slug', $wp_query->query['author_name'] ) : false;
318+
if ( $author ) {
320319
// Unhide this block on author archives.
321320
if ( isset( $block['attrs']['className'] ) ) {
322321
$block['attrs']['className'] = str_replace( 'screen-reader-text', '', $block['attrs']['className'] );
323322
}
324-
} else if ( is_search() ) {
325-
$title = __( 'Search results', 'wporg-themes' );
326-
} else if ( ! empty( $tags ) ) {
327-
$labels = get_tag_labels( $tags );
328-
$title = wp_sprintf_l( '%l', $labels );
329-
} else if ( 'community' === $current_browse ) {
330-
$title = __( 'Community themes', 'wporg-themes' );
331-
} else if ( 'commercial' === $current_browse ) {
332-
$title = __( 'Commercial themes', 'wporg-themes' );
333-
} else if ( 'new' === $current_browse ) {
334-
$title = __( 'Latest themes', 'wporg-themes' );
335-
} else if ( 'updated' === $current_browse ) {
336-
$title = __( 'Recently updated themes', 'wporg-themes' );
337-
} else if ( 'favorites' === $current_browse ) {
338-
$title = __( 'My favorites', 'wporg-themes' );
339-
} else {
340-
$title = __( 'All themes', 'wporg-themes' );
341323
}
342324

343325
$tag_name = isset( $attributes['level'] ) ? 'h' . (int) $attributes['level'] : 'h1';
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
<?php
2+
/**
3+
* Set up the SEO & social sharing meta tags.
4+
*/
5+
6+
namespace WordPressdotorg\Theme\Theme_Directory_2024\SEO_Social_Meta;
7+
8+
use const WordPressdotorg\Theme\Theme_Directory_2024\THEME_POST_TYPE;
9+
use function WordPressdotorg\Theme\Theme_Directory_2024\{ get_query_tags, get_tag_labels, get_theme_information };
10+
11+
add_filter( 'document_title_parts', __NAMESPACE__ . '\set_document_title' );
12+
add_filter( 'document_title_separator', __NAMESPACE__ . '\document_title_separator' );
13+
add_action( 'jetpack_open_graph_tags', __NAMESPACE__ . '\add_social_meta_tags', 20 );
14+
add_action( 'jetpack_seo_meta_tags', __NAMESPACE__ . '\add_seo_meta_tags', 20 );
15+
16+
add_filter( 'jetpack_enable_open_graph', '__return_true', 100 ); // Enable Jetpack Open Graph tags.
17+
remove_action( 'wp_head', 'wporg_themes_add_meta_tags' ); // Remove the theme-plugin-added meta tags.
18+
19+
/**
20+
* Append an optimized site name.
21+
*
22+
* @param array $title {
23+
* The document title parts.
24+
*
25+
* @type string $title Title of the viewed page.
26+
* @type string $page Optional. Page number if paginated.
27+
* @type string $tagline Optional. Site description when on home page.
28+
* @type string $site Optional. Site title when not on home page.
29+
* }
30+
* @return array Filtered title parts.
31+
*/
32+
function set_document_title( $title ) {
33+
global $wp_query;
34+
35+
$current_browse = $wp_query->query['browse'] ?? false;
36+
37+
if ( is_front_page() && ! $current_browse ) {
38+
$title['title'] = __( 'WordPress Theme Directory', 'wporg-themes' );
39+
$title['tagline'] = __( 'WordPress.org', 'wporg-themes' );
40+
} else {
41+
if ( is_singular( THEME_POST_TYPE ) ) {
42+
if ( isset( $wp_query->query_vars['view'] ) ) {
43+
/* translators: Theme title */
44+
$title['title'] = sprintf( __( '%s Preview', 'wporg-themes' ), $title['title'] );
45+
}
46+
47+
$title['title'] .= ' ' . document_title_separator() . ' ' . __( 'WordPress Theme', 'wporg-themes' );
48+
} elseif ( is_author() ) {
49+
/* translators: Author name */
50+
$title['title'] = sprintf( __( 'WordPress Themes by %s', 'wporg-themes' ), $title['title'] );
51+
} else {
52+
$title['title'] = get_archive_title();
53+
}
54+
55+
// If results are paged and the max number of pages is known.
56+
if ( is_paged() && $wp_query->max_num_pages ) {
57+
$title['page'] = sprintf(
58+
// translators: 1: current page number, 2: total number of pages
59+
__( 'Page %1$s of %2$s', 'wporg-themes' ),
60+
get_query_var( 'paged' ),
61+
$wp_query->max_num_pages
62+
);
63+
}
64+
65+
$title['site'] = __( 'WordPress.org', 'wporg-themes' );
66+
}
67+
68+
return $title;
69+
}
70+
71+
/**
72+
* Set the separator for the document title.
73+
*
74+
* @return string Document title separator.
75+
*/
76+
function document_title_separator() {
77+
return ( is_feed() ) ? '&#8212;' : '&#124;';
78+
}
79+
80+
/**
81+
* Add meta tags for richer social media integrations.
82+
*/
83+
function add_social_meta_tags( $tags ) {
84+
$default_image = 'https://wordpress.org/files/2024/04/wordpress-homepage-ogimage-202404.png';
85+
$site_title = function_exists( '\WordPressdotorg\site_brand' ) ? \WordPressdotorg\site_brand() : 'WordPress.org';
86+
$description = __( 'Find the perfect theme for your WordPress website. Choose from thousands of stunning designs with a wide variety of features and customization options.', 'wporg-themes' );
87+
88+
$tags['og:site_name'] = $site_title;
89+
$tags['og:title'] = __( 'WordPress Theme Directory', 'wporg-themes' );
90+
$tags['og:description'] = $description;
91+
$tags['og:image'] = esc_url( $default_image );
92+
$tags['og:image:alt'] = __( 'WordPress Theme Directory', 'wporg-themes' );
93+
$tags['og:locale'] = get_locale();
94+
$tags['twitter:card'] = 'summary_large_image';
95+
96+
$current_browse = $wp_query->query['browse'] ?? false;
97+
98+
if ( is_front_page() && ! $current_browse ) {
99+
return $tags;
100+
}
101+
102+
$tags['og:title'] = get_archive_title();
103+
104+
if ( is_author() ) {
105+
$tags['og:title'] = sprintf(
106+
/* translators: Author name */
107+
__( 'WordPress Themes by %s', 'wporg-themes' ),
108+
get_the_author()
109+
);
110+
} else if ( is_singular( THEME_POST_TYPE ) ) {
111+
$theme = get_theme_information();
112+
if ( ! $theme ) {
113+
return $tags;
114+
}
115+
116+
$sep = document_title_separator();
117+
118+
$tags['og:title'] = join( ' ', [ $theme->name, $sep, __( 'WordPress Theme Directory', 'wporg-themes' ) ] );
119+
$tags['twitter:text:title'] = join( ' ', [ $theme->name, $sep, __( 'WordPress Theme Directory', 'wporg-themes' ) ] );
120+
$tags['og:description'] = $theme->description;
121+
$tags['twitter:description'] = $theme->description;
122+
123+
if ( $theme->screenshot_url ) {
124+
$tags['og:image'] = $theme->screenshot_url;
125+
$tags['og:image:alt'] = $theme->name;
126+
$tags['twitter:image'] = $theme->screenshot_url;
127+
$tags['twitter:image:alt'] = $theme->name;
128+
}
129+
}
130+
131+
return $tags;
132+
}
133+
134+
135+
/**
136+
* Update the description meta value.
137+
*/
138+
function add_seo_meta_tags( $tags ) {
139+
$description = __( 'Find the perfect theme for your WordPress website. Choose from thousands of stunning designs with a wide variety of features and customization options.', 'wporg-themes' );
140+
$tags['description'] = $description;
141+
142+
if ( is_singular( THEME_POST_TYPE ) ) {
143+
$theme = get_theme_information();
144+
if ( $theme ) {
145+
$tags['description'] = $theme->description;
146+
}
147+
}
148+
149+
return $tags;
150+
}
151+
152+
/**
153+
* Get a human-friendly title for the current view.
154+
*
155+
* @return string
156+
*/
157+
function get_archive_title() {
158+
global $wp_query;
159+
160+
$author = isset( $wp_query->query['author_name'] ) ? get_user_by( 'slug', $wp_query->query['author_name'] ) : false;
161+
$current_browse = $wp_query->query['browse'] ?? false;
162+
$tags = get_query_tags();
163+
164+
if ( is_front_page() && ! $current_browse && ! is_paged() ) {
165+
$title = 'WordPress Themes';
166+
} else if ( $author ) {
167+
// translators: %s Author name.
168+
$title = sprintf( __( 'Author: %s', 'wporg-themes' ), $author->display_name );
169+
} else if ( is_search() ) {
170+
$title = __( 'Search results', 'wporg-themes' );
171+
} else if ( ! empty( $tags ) ) {
172+
$labels = get_tag_labels( $tags );
173+
$title = wp_sprintf_l( '%l', $labels );
174+
} else if ( 'community' === $current_browse ) {
175+
$title = __( 'Community themes', 'wporg-themes' );
176+
} else if ( 'commercial' === $current_browse ) {
177+
$title = __( 'Commercial themes', 'wporg-themes' );
178+
} else if ( 'new' === $current_browse ) {
179+
$title = __( 'Latest themes', 'wporg-themes' );
180+
} else if ( 'updated' === $current_browse ) {
181+
$title = __( 'Recently updated themes', 'wporg-themes' );
182+
} else if ( 'favorites' === $current_browse ) {
183+
$title = __( 'My favorites', 'wporg-themes' );
184+
} else {
185+
$title = __( 'All themes', 'wporg-themes' );
186+
}
187+
188+
return $title;
189+
}

0 commit comments

Comments
 (0)