initial: skeleton plugin

This commit is contained in:
2026-03-01 19:44:45 +01:00
commit 3dd4aa6722

103
ruady-og-head.php Normal file
View File

@@ -0,0 +1,103 @@
<?php
/**
* Plugin Name: Per-Post Head HTML
* Description: Inserts generated HTML into the document head for the currently displayed singular post.
* Version: 0.1.0
* Author: Your Name
*/
declare(strict_types=1);
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
final class Per_Post_Head_HTML {
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;
}
$html = self::build_markup_for_post( $post );
if ( $html === '' ) {
return;
}
echo "\n" . $html . "\n";
}
/**
* 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 {
// 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 = has_excerpt( $post ) ? get_the_excerpt( $post ) : '';
// 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();
?>
<!-- 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 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
return trim( (string) ob_get_clean() );
}
}
Per_Post_Head_HTML::init();