39292-vm/wp-content/plugins/flatlogic-seo-metadata/flatlogic-seo-metadata.php
2026-03-20 14:34:37 +00:00

84 lines
3.2 KiB
PHP

<?php
/**
* Plugin Name: Flatlogic SEO Metadata
* Description: Automatically adds SEO and Social Media (Open Graph, Twitter) metadata to the site.
* Version: 1.0.0
*/
function fl_get_homepage_social_image_url() {
$front_page_id = (int) get_option('page_on_front');
if ($front_page_id > 0) {
if (has_post_thumbnail($front_page_id)) {
$thumb = wp_get_attachment_image_url(get_post_thumbnail_id($front_page_id), 'full');
if (!empty($thumb)) {
return $thumb;
}
}
$front = get_post($front_page_id);
if ($front instanceof WP_Post && !empty($front->post_content)) {
if (preg_match('/<img[^>]+src=["\\\']([^"\\\']+)["\\\']/i', $front->post_content, $m)) {
return $m[1];
}
}
}
$custom_logo_id = (int) get_theme_mod('custom_logo');
if ($custom_logo_id) {
$logo = wp_get_attachment_image_url($custom_logo_id, 'full');
if (!empty($logo)) {
return $logo;
}
}
return get_site_icon_url(512) ?: '';
}
function fl_seo_metadata_head() {
if (is_admin()) return;
$projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? '';
$projectImageUrl = $_SERVER['PROJECT_IMAGE_URL'] ?? '';
$siteName = get_bloginfo('name');
$siteDescription = get_bloginfo('description');
if (is_front_page()) {
$homepageImage = fl_get_homepage_social_image_url();
if (!empty($homepageImage)) {
$projectImageUrl = $homepageImage;
}
}
$metaDescription = !empty($projectDescription) ? $projectDescription : $siteDescription;
$metaTitle = is_front_page() ? $siteName : get_the_title() . ' | ' . $siteName;
// Determine the current URL more accurately
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$host = $_SERVER['HTTP_HOST'] ?? 'localhost';
$current_url = $protocol . $host . $_SERVER['REQUEST_URI'];
?>
<?php if ($metaDescription): ?>
<!-- Meta description -->
<meta name="description" content="<?php echo esc_attr($metaDescription); ?>" />
<!-- Open Graph meta tags -->
<meta property="og:description" content="<?php echo esc_attr($metaDescription); ?>" />
<!-- Twitter meta tags -->
<meta property="twitter:description" content="<?php echo esc_attr($metaDescription); ?>" />
<?php endif; ?>
<meta property="og:title" content="<?php echo esc_attr($metaTitle); ?>" />
<meta property="og:type" content="website" />
<meta property="og:url" content="<?php echo esc_url($current_url); ?>" />
<meta property="og:site_name" content="<?php echo esc_attr($siteName); ?>" />
<meta property="twitter:card" content="summary_large_image" />
<meta property="twitter:title" content="<?php echo esc_attr($metaTitle); ?>" />
<?php if ($projectImageUrl): ?>
<!-- Open Graph image -->
<meta property="og:image" content="<?php echo esc_url($projectImageUrl); ?>" />
<!-- Twitter image -->
<meta property="twitter:image" content="<?php echo esc_url($projectImageUrl); ?>" />
<?php endif; ?>
<?php
}
add_action('wp_head', 'fl_seo_metadata_head', 1);