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'];
$og_image_url = user_trailingslashit( trailingslashit( $post_url ) . 'og-image' );
$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();
?>
ID, $accent );
*/
//////////////////////////////////
$excerpt = get_the_excerpt( $post );
$description = html_entity_decode(wp_strip_all_tags( $excerpt ));
$fontFile = __DIR__ . '/fonts/EBGaramond-VariableFont_wght.ttf';
//$size = 28;
$size = 36;
//$x = 60;
$x = (int) (($width - $height) / 2 + 30);
$y = 120;
$lineHeight = (int) ($size * 1.9);
//$maxWidth = 1080;
$maxWidth = $height - 2*60; // prepare text for 1:1 ratio cropping
$maxHeight = $height - 60;
// wrap_text_ttf(string $text, int $maxWidth, string $fontFile, float $size)
$lines = self::wrap_text_ttf($description, $maxWidth, $fontFile, $size);
foreach ($lines as $line) {
imagettftext($image, $size, 0, $x, $y, $fg, $fontFile, $line);
$y += $lineHeight;
if ($y > $maxHeight) break;
}
//////////////////////////////////
status_header( 200 );
header( 'Content-Type: image/png' );
header( 'Cache-Control: public, max-age=3600' );
imagepng( $image );
imagedestroy( $image );
exit;
}
private static function render_text_error( int $status, string $message ): void {
status_header( $status );
header( 'Content-Type: text/plain; charset=utf-8' );
echo $message;
exit;
}
private static function truncate( string $text, int $max_len ): string {
if ( mb_strlen( $text ) <= $max_len ) {
return $text;
}
return mb_substr( $text, 0, $max_len - 1 ) . '…';
}
}
Ruady_OG_Head::init();