Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add required meta tags for social media #34

Merged
merged 2 commits into from Feb 7, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
70 changes: 69 additions & 1 deletion source/wp-content/themes/wporg-developer-blog/functions.php
Expand Up @@ -5,7 +5,8 @@
/**
* Actions and filters.
*/
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\enqueue_assets' );
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\enqueue_assets' );
add_action( 'wp_head', __NAMESPACE__ . '\output_head_tags', 2 );
add_filter( 'render_block_core/search', __NAMESPACE__ . '\search_block_add_search_action', 10, 2 );

/**
Expand Down Expand Up @@ -41,3 +42,70 @@ function search_block_add_search_action( $block_content, $block ) {

return $block_content;
}

/**
* Outputs tags for the page head.
*/
function output_head_tags() {
$fields = [
// FYI: 'description' and 'og:description' are set further down.
'og:title' => wp_get_document_title(),
'og:site_name' => get_bloginfo( 'name' ),
'og:url' => home_url( '/' ),
'twitter:title' => wp_get_document_title(),
'twitter:site' => '@WordPress',
];

$desc = '';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disregard if I'm misunderstanding, but can we not just use the excerpt?

Copy link
Contributor Author

@renintw renintw Feb 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing this out!

Originally this part was referred to a snippet in wporg-developer. The reason it parsed desc and trimmed it down to <150 chars with regex from scratch is that developer.w.org doesn’t store data within the post_content in a way that get_the_excerpt would handle well.

get_the_excerpt() would work well for the is_singular() if branch, but other parts it wouldn’t, so in the snippet the original author might just figure it was better to process all of the excerpts for SEO headers the same way.

I've changed to use get_the_exceprt() here as at the moment only is_singular is used and I'd like to get this PR merged first to see if the original issue is fixed.
Whether it needs some specific descriptions on different pages (front page, archive page, etc.), I'll have another discussion afterward to figure out the requirements.

Thanks to @dd32 for the clarification!


if ( is_singular() ) {
$post = get_queried_object();
if ( $post ) {
$desc = $post->post_content;
}
}

// Actually set field values for description.
if ( ! empty( $desc ) ) {
$desc = wp_strip_all_tags( $desc );
$desc = str_replace( '&nbsp;', ' ', $desc );
$desc = preg_replace( '/\s+/', ' ', $desc );

// Trim down to <150 characters based on full words.
if ( strlen( $desc ) > 150 ) {
$truncated = '';
$words = preg_split( "/[\n\r\t ]+/", $desc, -1, PREG_SPLIT_NO_EMPTY );

while ( $words ) {
$word = array_shift( $words );
if ( strlen( $truncated ) + strlen( $word ) >= 141 ) { /* 150 - strlen( ' &hellip;' ) */
break;
}

$truncated .= $word . ' ';
}

$truncated = trim( $truncated );

if ( $words ) {
$truncated .= '&hellip;';
}

$desc = $truncated;
}

$fields['description'] = $desc;
$fields['og:description'] = $desc;
}

// Output fields.
foreach ( $fields as $property => $content ) {
$attribute = 0 === strpos( $property, 'og:' ) ? 'property' : 'name';
printf(
'<meta %s="%s" content="%s" />' . "\n",
esc_attr( $attribute ),
esc_attr( $property ),
esc_attr( $content )
);
}
}