generate Open Graph meta tags for category = Kurzgeschichten
This commit is contained in:
@@ -1,18 +1,20 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Plugin Name: Per-Post Head HTML
|
* Plugin Name: Ruady OG Head
|
||||||
* Description: Inserts generated HTML into the document head for the currently displayed singular post.
|
* Description: Generates Open Graph tags for Social Media sharing (Facebook, Twitter/X, etc.)
|
||||||
* Version: 0.1.0
|
* Version: 0.1.0
|
||||||
* Author: Your Name
|
* Author: David Madl
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// note: install by copying into wp-content/plugins/ruady-og-head/ruady-og-head.php
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
if ( ! defined( 'ABSPATH' ) ) {
|
if ( ! defined( 'ABSPATH' ) ) {
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
final class Per_Post_Head_HTML {
|
final class Ruady_OG_Head {
|
||||||
public static function init(): void {
|
public static function init(): void {
|
||||||
add_action( 'wp_head', [ __CLASS__, 'render_head_markup' ], 20 );
|
add_action( 'wp_head', [ __CLASS__, 'render_head_markup' ], 20 );
|
||||||
}
|
}
|
||||||
@@ -37,6 +39,10 @@ final class Per_Post_Head_HTML {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( ! has_category( 'Kurzgeschichten', $post_id ) ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$html = self::build_markup_for_post( $post );
|
$html = self::build_markup_for_post( $post );
|
||||||
|
|
||||||
if ( $html === '' ) {
|
if ( $html === '' ) {
|
||||||
@@ -46,10 +52,23 @@ final class Per_Post_Head_HTML {
|
|||||||
echo "\n" . $html . "\n";
|
echo "\n" . $html . "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static function make_description( string $text ): string {
|
||||||
|
$target_length = 150;
|
||||||
|
$parts = explode(' ', $text);
|
||||||
|
$lengths = array_map('strlen', $parts);
|
||||||
|
$total_length = 0;
|
||||||
|
$i = 0;
|
||||||
|
for ( ; $i < count($lengths); $i++ ) {
|
||||||
|
if ( $total_length + $lengths[$i] > $target_length ) break;
|
||||||
|
$total_length += $lengths[$i];
|
||||||
|
}
|
||||||
|
$desc = implode(' ', array_slice($parts, 0, $i));
|
||||||
|
$ellipsis = substr($desc, -1, 1) == '.' ? '' : ' ...'; // full sentences with a '.' don't need ellipsis '...'
|
||||||
|
return $desc . $ellipsis;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build the head markup for one post.
|
* Build the head markup for one post.
|
||||||
*
|
|
||||||
* This is the function you will customize.
|
|
||||||
*/
|
*/
|
||||||
private static function build_markup_for_post( WP_Post $post ): string {
|
private static function build_markup_for_post( WP_Post $post ): string {
|
||||||
// Example 1: conditionally emit markup only for a specific post type.
|
// Example 1: conditionally emit markup only for a specific post type.
|
||||||
@@ -59,7 +78,13 @@ final class Per_Post_Head_HTML {
|
|||||||
|
|
||||||
$post_url = get_permalink( $post );
|
$post_url = get_permalink( $post );
|
||||||
$post_title = get_the_title( $post );
|
$post_title = get_the_title( $post );
|
||||||
$excerpt = has_excerpt( $post ) ? get_the_excerpt( $post ) : '';
|
$excerpt = get_the_excerpt( $post );
|
||||||
|
$site_title = get_bloginfo( 'name' );
|
||||||
|
$site_url = site_url();
|
||||||
|
$site_url_p = parse_url($site_url);
|
||||||
|
$site_host = $site_url_p['host'];
|
||||||
|
|
||||||
|
$description = self::make_description(wp_strip_all_tags( $excerpt ));
|
||||||
|
|
||||||
// Example business logic:
|
// Example business logic:
|
||||||
// - add a meta tag for every post
|
// - add a meta tag for every post
|
||||||
@@ -68,36 +93,31 @@ final class Per_Post_Head_HTML {
|
|||||||
|
|
||||||
ob_start();
|
ob_start();
|
||||||
?>
|
?>
|
||||||
|
<meta name="description" content="<?php echo esc_attr( $description ); ?>">
|
||||||
|
|
||||||
|
<!-- Open Graph Meta Tags -->
|
||||||
|
<meta property="og:url" content="<?php echo esc_url( $site_url ); ?>">
|
||||||
|
<meta property="og:type" content="website">
|
||||||
|
<meta property="og:title" content="<?php echo esc_attr( $post_title ); ?> – <?php echo esc_attr( $site_title ); ?>">
|
||||||
|
<meta property="og:description" content="<?php echo esc_attr( $description ); ?>">
|
||||||
|
<!--<meta property="og:image" content="">--><!-- Load error, please check URL -->
|
||||||
|
|
||||||
|
<!-- Twitter Meta Tags -->
|
||||||
|
<meta name="twitter:card" content="summary_large_image">
|
||||||
|
<meta property="twitter:domain" content="<?php echo esc_attr( $site_host ); ?>">
|
||||||
|
<meta property="twitter:url" content="<?php echo esc_url( $site_url ); ?>">
|
||||||
|
<meta name="twitter:title" content="<?php echo esc_attr( $post_title ); ?> – <?php echo esc_attr( $site_title ); ?>">
|
||||||
|
<meta name="twitter:description" content="<?php echo esc_attr( $description ); ?>">
|
||||||
|
<!--<meta name="twitter:image" content="">-->
|
||||||
|
|
||||||
<!-- Per-Post Head HTML plugin -->
|
<!-- Per-Post Head HTML plugin -->
|
||||||
<meta name="x-post-id" content="<?php echo esc_attr( (string) $post->ID ); ?>">
|
<meta name="x-post-id" content="<?php echo esc_attr( (string) $post->ID ); ?>">
|
||||||
<meta name="x-post-type" content="<?php echo esc_attr( $post->post_type ); ?>">
|
<meta name="x-post-type" content="<?php echo esc_attr( $post->post_type ); ?>">
|
||||||
<link rel="canonical" href="<?php echo esc_url( $post_url ); ?>">
|
<link rel="canonical" href="<?php echo esc_url( $post_url ); ?>">
|
||||||
|
|
||||||
<?php if ( $excerpt !== '' ) : ?>
|
|
||||||
<meta name="description" content="<?php echo esc_attr( wp_strip_all_tags( $excerpt ) ); ?>">
|
|
||||||
<?php endif; ?>
|
|
||||||
|
|
||||||
<?php if ( $is_news ) : ?>
|
|
||||||
<script type="application/ld+json">
|
|
||||||
<?php
|
|
||||||
echo wp_json_encode(
|
|
||||||
[
|
|
||||||
'@context' => 'https://schema.org',
|
|
||||||
'@type' => 'NewsArticle',
|
|
||||||
'headline' => $post_title,
|
|
||||||
'url' => $post_url,
|
|
||||||
'datePublished' => get_post_time( 'c', true, $post ),
|
|
||||||
'dateModified' => get_post_modified_time( 'c', true, $post ),
|
|
||||||
],
|
|
||||||
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
|
|
||||||
);
|
|
||||||
?>
|
|
||||||
</script>
|
|
||||||
<?php endif; ?>
|
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
return trim( (string) ob_get_clean() );
|
return trim( (string) ob_get_clean() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Per_Post_Head_HTML::init();
|
Ruady_OG_Head::init();
|
||||||
|
|||||||
Reference in New Issue
Block a user