Files
ruady-og-head/ruady-og-head.php

124 lines
3.7 KiB
PHP

<?php
/**
* Plugin Name: Ruady OG Head
* Description: Generates Open Graph tags for Social Media sharing (Facebook, Twitter/X, etc.)
* Version: 0.1.0
* Author: David Madl
*/
// note: install by copying into wp-content/plugins/ruady-og-head/ruady-og-head.php
declare(strict_types=1);
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
final class Ruady_OG_Head {
public static function init(): void {
add_action( 'wp_head', [ __CLASS__, 'render_head_markup' ], 20 );
}
/**
* Echo markup into <head> for the currently displayed singular object.
*/
public static function render_head_markup(): void {
// Adjust this if you only want standard posts:
// if ( ! is_singular( 'post' ) ) { return; }
if ( ! is_singular() ) {
return;
}
$post_id = get_queried_object_id();
if ( ! $post_id ) {
return;
}
$post = get_post( $post_id );
if ( ! $post instanceof WP_Post ) {
return;
}
if ( ! has_category( 'Kurzgeschichten', $post_id ) ) {
return;
}
$html = self::build_markup_for_post( $post );
if ( $html === '' ) {
return;
}
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.
*/
private static function build_markup_for_post( WP_Post $post ): string {
// Example 1: conditionally emit markup only for a specific post type.
if ( $post->post_type !== 'post' ) {
return '';
}
$post_url = get_permalink( $post );
$post_title = get_the_title( $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:
// - add a meta tag for every post
// - add JSON-LD only if the post is in category "news"
$is_news = has_category( 'news', $post );
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 ); ?> &#8211; <?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 ); ?> &#8211; <?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 -->
<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 ); ?>">
<link rel="canonical" href="<?php echo esc_url( $post_url ); ?>">
<?php
return trim( (string) ob_get_clean() );
}
}
Ruady_OG_Head::init();