init(); } /** * Initialize the module */ private function init() { // Frontend hooks add_action('wp_head', array($this, 'inject_opengraph_tags'), 5); // Admin hooks if (is_admin()) { add_action('admin_post_update_opengraph_settings', array($this, 'handle_form_submission')); } } /** * Inject OpenGraph meta tags into head section */ public function inject_opengraph_tags() { $settings = $this->get_opengraph_settings(); if (!$settings['enabled']) { return; } $tags = $this->generate_opengraph_tags(); if (!empty($tags)) { echo "\n\n"; foreach ($tags as $tag) { echo $tag . "\n"; } echo "\n\n"; } } /** * Generate OpenGraph meta tags based on current page */ private function generate_opengraph_tags() { $tags = array(); $settings = $this->get_opengraph_settings(); // Required tags $tags[] = ''; $tags[] = ''; $tags[] = ''; $tags[] = ''; // Optional but recommended tags $tags[] = ''; $tags[] = ''; // Site name if (!empty($settings['site_name'])) { $tags[] = ''; } // Facebook App ID if (!empty($settings['app_id'])) { $tags[] = ''; } // Image dimensions if available $image_data = $this->get_image_data($this->get_page_image()); if ($image_data) { $tags[] = ''; $tags[] = ''; if (!empty($image_data['type'])) { $tags[] = ''; } } // Article-specific tags for posts if (is_single() && get_post_type() === 'post') { $post = get_post(); $tags[] = ''; $tags[] = ''; // Author $author = get_the_author_meta('display_name', $post->post_author); if ($author) { $tags[] = ''; } // Categories as sections $categories = get_the_category(); foreach ($categories as $category) { $tags[] = ''; } // Tags $post_tags = get_the_tags(); if ($post_tags) { foreach ($post_tags as $tag) { $tags[] = ''; } } } return apply_filters('tigerstyle_heat_opengraph_tags', $tags); } /** * Get canonical URL for current page */ private function get_canonical_url() { if (is_home() || is_front_page()) { return home_url('/'); } if (is_singular()) { return get_permalink(); } if (is_category()) { return get_category_link(get_queried_object_id()); } if (is_tag()) { return get_tag_link(get_queried_object_id()); } if (is_author()) { return get_author_posts_url(get_queried_object_id()); } // Fallback to current URL global $wp; return home_url(add_query_arg(array(), $wp->request)); } /** * Get page title optimized for social sharing */ private function get_page_title() { if (is_singular()) { return get_the_title(); } if (is_home() || is_front_page()) { return get_bloginfo('name'); } if (is_category()) { return single_cat_title('', false); } if (is_tag()) { return single_tag_title('', false); } if (is_author()) { return get_the_author_meta('display_name', get_queried_object_id()); } if (is_archive()) { return get_the_archive_title(); } return get_bloginfo('name'); } /** * Get page description for social sharing */ private function get_page_description() { $settings = $this->get_opengraph_settings(); if (is_singular()) { $post = get_post(); // Try excerpt first if (!empty($post->post_excerpt)) { return wp_strip_all_tags($post->post_excerpt); } // Try content excerpt $content = wp_strip_all_tags($post->post_content); if ($content) { return wp_trim_words($content, 30); } } if (is_category()) { $description = category_description(); if ($description) { return wp_strip_all_tags($description); } } if (is_tag()) { $description = tag_description(); if ($description) { return wp_strip_all_tags($description); } } // Fallback to site description return get_bloginfo('description') ?: $settings['default_description']; } /** * Get appropriate image for the page */ private function get_page_image() { $settings = $this->get_opengraph_settings(); // Featured image for posts/pages if (is_singular() && has_post_thumbnail()) { $image_id = get_post_thumbnail_id(); $image_url = wp_get_attachment_image_url($image_id, 'large'); if ($image_url) { return $image_url; } } // Find first image in content if (is_singular()) { $content = get_post_field('post_content'); preg_match('/]+src="([^">]+)"/', $content, $matches); if (!empty($matches[1])) { return $matches[1]; } } // Default image from settings if (!empty($settings['default_image'])) { return $settings['default_image']; } // Site logo as fallback $custom_logo_id = get_theme_mod('custom_logo'); if ($custom_logo_id) { $logo_url = wp_get_attachment_image_url($custom_logo_id, 'large'); if ($logo_url) { return $logo_url; } } return ''; } /** * Get OpenGraph type for current page */ private function get_page_type() { if (is_single() && get_post_type() === 'post') { return 'article'; } if (is_page()) { return 'website'; } if (is_author()) { return 'profile'; } return 'website'; } /** * Get locale for OpenGraph */ private function get_locale() { $locale = get_locale(); // Convert WordPress locale to OpenGraph format $locale_map = array( 'en_US' => 'en_US', 'en_GB' => 'en_GB', 'es_ES' => 'es_ES', 'fr_FR' => 'fr_FR', 'de_DE' => 'de_DE', 'it_IT' => 'it_IT', 'pt_BR' => 'pt_BR', 'nl_NL' => 'nl_NL', 'ru_RU' => 'ru_RU', 'ja' => 'ja_JP', 'zh_CN' => 'zh_CN', ); return isset($locale_map[$locale]) ? $locale_map[$locale] : 'en_US'; } /** * Get image dimensions and type */ private function get_image_data($image_url) { if (empty($image_url)) { return false; } // Try to get attachment ID from URL $attachment_id = attachment_url_to_postid($image_url); if ($attachment_id) { $metadata = wp_get_attachment_metadata($attachment_id); if ($metadata && isset($metadata['width'], $metadata['height'])) { return array( 'width' => $metadata['width'], 'height' => $metadata['height'], 'type' => get_post_mime_type($attachment_id) ); } } // Try to get image size from URL (if local) if (strpos($image_url, home_url()) === 0) { $upload_dir = wp_upload_dir(); $image_path = str_replace($upload_dir['baseurl'], $upload_dir['basedir'], $image_url); if (file_exists($image_path)) { $image_info = getimagesize($image_path); if ($image_info) { return array( 'width' => $image_info[0], 'height' => $image_info[1], 'type' => $image_info['mime'] ); } } } return false; } /** * Get OpenGraph settings */ private function get_opengraph_settings() { $defaults = array( 'enabled' => true, 'site_name' => get_bloginfo('name'), 'app_id' => '', 'default_description' => get_bloginfo('description'), 'default_image' => '', 'include_article_tags' => true, 'include_author_info' => true ); $settings = get_option('tigerstyle_opengraph_settings', array()); return wp_parse_args($settings, $defaults); } /** * Handle form submission */ public function handle_form_submission() { if (!current_user_can('manage_options')) { wp_die(__('You do not have sufficient permissions to access this page.')); } if (!wp_verify_nonce($_POST['_wpnonce'], 'tigerstyle_opengraph_settings')) { wp_die(__('Security check failed. Please try again.')); } $settings = array( 'enabled' => isset($_POST['og_enabled']) ? 1 : 0, 'site_name' => sanitize_text_field($_POST['og_site_name'] ?? ''), 'app_id' => sanitize_text_field($_POST['og_app_id'] ?? ''), 'default_description' => sanitize_textarea_field($_POST['og_default_description'] ?? ''), 'default_image' => esc_url_raw($_POST['og_default_image'] ?? ''), 'include_article_tags' => isset($_POST['og_include_article_tags']) ? 1 : 0, 'include_author_info' => isset($_POST['og_include_author_info']) ? 1 : 0 ); update_option('tigerstyle_opengraph_settings', $settings); wp_redirect(add_query_arg(array('page' => 'tigerstyle-heat', 'tab' => 'opengraph', 'updated' => 'true'), admin_url('admin.php'))); exit; } /** * Get debug information for current page */ public function get_debug_info() { if (!current_user_can('manage_options')) { return array(); } return array( 'url' => $this->get_canonical_url(), 'title' => $this->get_page_title(), 'description' => $this->get_page_description(), 'image' => $this->get_page_image(), 'type' => $this->get_page_type(), 'locale' => $this->get_locale(), 'tags' => $this->generate_opengraph_tags() ); } /** * Render admin page */ public function render_admin_page() { $settings = $this->get_opengraph_settings(); ?>

  • og:title -
  • og:description -
  • og:image -
  • og:url -
  • og:type -


get_debug_info(); if (!empty($debug_info)): ?>


OpenGraph Preview