2026-03-01 19:44:45 +01:00
|
|
|
<?php
|
|
|
|
|
/**
|
2026-03-01 20:52:30 +01:00
|
|
|
* Plugin Name: Ruady OG Head
|
|
|
|
|
* Description: Generates Open Graph tags for Social Media sharing (Facebook, Twitter/X, etc.)
|
2026-03-01 19:44:45 +01:00
|
|
|
* Version: 0.1.0
|
2026-03-01 20:52:30 +01:00
|
|
|
* Author: David Madl
|
2026-03-01 19:44:45 +01:00
|
|
|
*/
|
|
|
|
|
|
2026-03-01 20:52:30 +01:00
|
|
|
// note: install by copying into wp-content/plugins/ruady-og-head/ruady-og-head.php
|
|
|
|
|
|
2026-03-01 19:44:45 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-01 20:52:30 +01:00
|
|
|
final class Ruady_OG_Head {
|
2026-03-01 19:44:45 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-01 20:52:30 +01:00
|
|
|
if ( ! has_category( 'Kurzgeschichten', $post_id ) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-01 19:44:45 +01:00
|
|
|
$html = self::build_markup_for_post( $post );
|
|
|
|
|
|
|
|
|
|
if ( $html === '' ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
echo "\n" . $html . "\n";
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-01 20:52:30 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-01 19:44:45 +01:00
|
|
|
/**
|
|
|
|
|
* 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 );
|
2026-03-01 20:52:30 +01:00
|
|
|
$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 ));
|
2026-03-01 19:44:45 +01:00
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
?>
|
2026-03-01 20:52:30 +01:00
|
|
|
<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="">-->
|
|
|
|
|
|
2026-03-01 19:44:45 +01:00
|
|
|
<!-- 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() );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-01 20:52:30 +01:00
|
|
|
Ruady_OG_Head::init();
|