Files
sportpressa_backend/inc/opengraph.php
2026-01-09 21:46:49 +03:00

81 lines
3.2 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
function custom_open_graph_tags() {
// Дефолтная картинка
$default_image = 'https://sportpressa.ru/wp-content/uploads/2026/01/sportpressa-ru.png';
// Для постов
if (is_single()) {
$title = get_the_title();
$description = wp_trim_words(get_the_excerpt(), 30);
$url = get_permalink();
$type = 'article';
// Картинка поста или дефолтная
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail_url(get_the_ID(), 'full');
} else {
$image = $default_image;
}
}
// Для главной и архивов (категории, теги)
elseif (is_home() || is_archive()) {
$type = 'website';
$url = get_permalink();
$image = $default_image; // Всегда дефолтная картинка
if (is_home()) {
$title = get_bloginfo('name');
$description = get_bloginfo('description');
} elseif (is_category()) {
$title = single_cat_title('', false);
$description = category_description() ? wp_trim_words(category_description(), 30) : get_bloginfo('description');
} elseif (is_tag()) {
$title = single_tag_title('', false);
$description = tag_description() ? wp_trim_words(tag_description(), 30) : get_bloginfo('description');
} else {
$title = get_the_archive_title();
$description = get_bloginfo('description');
}
}
// Для страниц
elseif (is_page()) {
$title = get_the_title();
$description = wp_trim_words(get_the_excerpt(), 30);
$url = get_permalink();
$type = 'website';
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail_url(get_the_ID(), 'full');
} else {
$image = $default_image;
}
}
else {
return; // Не выводим OG теги на других страницах
}
// Вывод мета-тегов
?>
<meta property="og:title" content="<?php echo esc_attr($title); ?>" />
<meta property="og:description" content="<?php echo esc_attr($description); ?>" />
<meta property="og:type" content="<?php echo esc_attr($type); ?>" />
<meta property="og:url" content="<?php echo esc_url($url); ?>" />
<meta property="og:site_name" content="<?php echo esc_attr(get_bloginfo('name')); ?>" />
<meta property="og:image" content="<?php echo esc_url($image); ?>" />
<meta property="og:locale" content="ru_RU" />
<?php if (is_single()) : ?>
<meta property="article:published_time" content="<?php echo get_the_date('c'); ?>" />
<meta property="article:modified_time" content="<?php echo get_the_modified_date('c'); ?>" />
<?php endif; ?>
<!-- Twitter Cards -->
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:title" content="<?php echo esc_attr($title); ?>" />
<meta name="twitter:description" content="<?php echo esc_attr($description); ?>" />
<meta name="twitter:image" content="<?php echo esc_url($image); ?>" />
<?php
}
add_action('wp_head', 'custom_open_graph_tags', 5);
?>