'on', 'default_image' => '', 'twitter_site' => '', 'facebook_app_id' => ''); $s = get_option(CITAMI_SOCIAL_SETTINGS_OPT, array()); if (is_array($s)) { foreach ($d as $k => $v) { if (isset($s[$k]) && is_string($s[$k])) { $d[$k] = $s[$k]; } } } return $d; } function citami_sanitize_social_settings($value) { $in = is_array($value) ? $value : array(); $img = isset($in['default_image']) ? trim((string) $in['default_image']) : ''; $tw = isset($in['twitter_site']) ? trim((string) $in['twitter_site']) : ''; $tw = $tw === '' ? '' : '@' . preg_replace('/[^A-Za-z0-9_]/', '', ltrim($tw, '@')); return array( 'enabled' => (isset($in['enabled']) && $in['enabled'] === 'off') ? 'off' : 'on', 'default_image' => preg_match('#^https?://#i', $img) ? esc_url_raw($img) : '', 'twitter_site' => $tw === '@' ? '' : $tw, 'facebook_app_id' => isset($in['facebook_app_id']) ? preg_replace('/\D/', '', (string) $in['facebook_app_id']) : '', ); } add_action('admin_init', function () { register_setting('citami_seo', CITAMI_SOCIAL_SETTINGS_OPT, array( 'type' => 'array', 'sanitize_callback' => 'citami_sanitize_social_settings', 'default' => array(), )); }); function citami_social_get_post_fields($post_id) { return array( 'og_title' => (string) get_post_meta($post_id, CITAMI_META_OG_TITLE, true), 'og_description' => (string) get_post_meta($post_id, CITAMI_META_OG_DESC, true), 'og_image' => (string) get_post_meta($post_id, CITAMI_META_OG_IMAGE, true), ); } function citami_social_save_post_fields($post_id, array $fields) { if (array_key_exists('og_title', $fields)) { update_post_meta($post_id, CITAMI_META_OG_TITLE, mb_substr(citami_seo_sanitize_template($fields['og_title']), 0, 200)); } if (array_key_exists('og_description', $fields)) { update_post_meta($post_id, CITAMI_META_OG_DESC, mb_substr(citami_seo_sanitize_template($fields['og_description']), 0, 320)); } if (array_key_exists('og_image', $fields)) { $img = trim((string) $fields['og_image']); update_post_meta($post_id, CITAMI_META_OG_IMAGE, preg_match('#^https?://#i', $img) ? esc_url_raw($img) : ''); } return citami_social_get_post_fields($post_id); } /** Image for the current context: override → featured → default → entity logo. */ function citami_social_image($ctx) { if (!empty($ctx['post'])) { $custom = (string) get_post_meta($ctx['post']->ID, CITAMI_META_OG_IMAGE, true); if ($custom !== '') { return $custom; } $featured = citami_schema_featured_image($ctx['post']); if ($featured !== '') { return $featured; } } $s = citami_social_settings(); if ($s['default_image'] !== '') { return $s['default_image']; } $entity = citami_schema_settings()['entity']; return $entity['logo'] !== '' ? $entity['logo'] : ''; } /** [name/property, content] pairs for the current request. */ function citami_social_tags($ctx = null) { $ctx = $ctx === null ? citami_seo_context() : $ctx; if ($ctx['kind'] === '404' || $ctx['kind'] === 'search') { return array(); } $s = citami_social_settings(); $title = citami_seo_title($ctx); if ($title === null || $title === '') { $title = get_bloginfo('name'); } $desc = citami_seo_description($ctx); $url = citami_seo_canonical($ctx); $type = 'website'; if (!empty($ctx['post'])) { $f = citami_social_get_post_fields($ctx['post']->ID); if ($f['og_title'] !== '') { $title = citami_seo_render_template($f['og_title'], citami_seo_vars($ctx)); } if ($f['og_description'] !== '') { $desc = citami_seo_render_template($f['og_description'], citami_seo_vars($ctx)); } if ($ctx['kind'] === 'singular' && $ctx['post']->post_type === 'post') { $type = 'article'; } } $image = citami_social_image($ctx); $tags = array( array('property', 'og:type', $type), array('property', 'og:title', $title), array('property', 'og:site_name', get_bloginfo('name')), array('property', 'og:locale', str_replace('-', '_', (string) get_locale())), ); if ($desc !== '') { $tags[] = array('property', 'og:description', $desc); } if ($url !== '') { $tags[] = array('property', 'og:url', $url); } if ($image !== '') { $tags[] = array('property', 'og:image', $image); } if ($type === 'article' && !empty($ctx['post'])) { if (!empty($ctx['post']->post_date)) { $tags[] = array('property', 'article:published_time', citami_schema_iso_date($ctx['post']->post_date)); } if (!empty($ctx['post']->post_modified)) { $tags[] = array('property', 'article:modified_time', citami_schema_iso_date($ctx['post']->post_modified)); } } if ($s['facebook_app_id'] !== '') { $tags[] = array('property', 'fb:app_id', $s['facebook_app_id']); } $tags[] = array('name', 'twitter:card', $image !== '' ? 'summary_large_image' : 'summary'); $tags[] = array('name', 'twitter:title', $title); if ($desc !== '') { $tags[] = array('name', 'twitter:description', $desc); } if ($image !== '') { $tags[] = array('name', 'twitter:image', $image); } if ($s['twitter_site'] !== '') { $tags[] = array('name', 'twitter:site', $s['twitter_site']); } return $tags; } function citami_social_print_head() { if (!citami_seo_enabled() || citami_social_settings()['enabled'] !== 'on') { return; } foreach (citami_social_tags() as $t) { $attr = $t[0] === 'property' ? 'property' : 'name'; echo '' . "\n"; } } add_action('wp_head', 'citami_social_print_head', 2); add_action('citami_seo_meta_box_fields', function ($post) { $f = citami_social_get_post_fields($post->ID); ?>
Social (Open Graph / X)
| Open Graph / X | |
|---|---|