Seo
**Site relevance:** 2IA.org
**Memory type:** theme source memory
**Source path:** 2IA.org/wp-content/themes/twoia-intelligence/inc/seo.php
**Size:** 5.1 KB
Summary
Lightweight SEO metadata.
Source Preview
This source file is short enough to preview directly on its source-memory page.
<?php
/**
* Lightweight SEO metadata.
*
* @package TwoIA
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Detect common SEO plugins to avoid duplicate meta tags.
*
* @return bool
*/
function twoia_seo_plugin_active() {
return class_exists( 'WPSEO_Frontend' ) || defined( 'RANK_MATH_VERSION' ) || defined( 'AIOSEO_VERSION' );
}
/**
* Build a plain-text meta description.
*
* @return string
*/
function twoia_get_meta_description() {
if ( is_front_page() || is_home() ) {
return esc_html__( '2IA publishes independent research on surveillance, identity systems, public records, intelligence power, anonymity, civil liberties, and freedom.', 'two-identities-anonymous' );
}
if ( function_exists( 'twoia_get_requested_virtual_page' ) ) {
$page = twoia_get_requested_virtual_page();
if ( $page && twoia_should_render_virtual_page( $page['slug'] ) ) {
if ( empty( $page['route_type'] ) && ! empty( $page['meta_description'] ) ) {
return wp_strip_all_tags( $page['meta_description'] );
}
if ( function_exists( 'twoia_get_virtual_share_payload' ) ) {
$section = ! empty( $page['brief_section'] ) ? $page['brief_section'] : null;
$dossier = array();
if ( $section && function_exists( 'twoia_get_virtual_dossier_payload' ) ) {
$dossier = twoia_get_virtual_dossier_payload( $page, $section );
}
$payload = twoia_get_virtual_share_payload( $page, $section, $dossier );
return wp_trim_words( wp_strip_all_tags( $payload['line'] . ' ' . $payload['demand'] ), 36, '...' );
}
return wp_trim_words( wp_strip_all_tags( $page['summary'] ), 32, '...' );
}
}
if ( is_singular() ) {
$post = get_post();
if ( $post instanceof WP_Post ) {
$source = has_excerpt( $post ) ? $post->post_excerpt : $post->post_content;
return wp_trim_words( wp_strip_all_tags( strip_shortcodes( $source ) ), 32, '…' );
}
}
if ( is_front_page() || is_home() ) {
return wp_trim_words( wp_strip_all_tags( twoia_theme_mod( 'twoia_hero_body', '' ) ), 32, '…' );
}
if ( is_archive() ) {
$description = get_the_archive_description();
if ( $description ) {
return wp_trim_words( wp_strip_all_tags( $description ), 32, '…' );
}
}
if ( is_search() ) {
return sprintf(
/* translators: %s: Search query. */
esc_html__( 'Search results for %s in the 2IA public-intelligence research archive.', 'two-identities-anonymous' ),
get_search_query()
);
}
return esc_html__( '2IA studies modern communications surveillance, metadata, identity, AI analysis, civil liberties, and lawful public understanding.', 'two-identities-anonymous' );
}
/**
* Return the canonical URL for the current view.
*
* @return string
*/
function twoia_get_canonical_url() {
if ( function_exists( 'twoia_get_requested_virtual_page' ) ) {
$page = twoia_get_requested_virtual_page();
if ( $page && twoia_should_render_virtual_page( $page['slug'] ) ) {
if ( 'research-archive' === $page['slug'] && empty( $page['route_type'] ) ) {
return home_url( '/research/' );
}
return home_url( '/' . $page['slug'] . '/' );
}
}
if ( is_singular() ) {
return get_permalink();
}
if ( is_front_page() || is_home() ) {
return home_url( '/' );
}
return get_pagenum_link( max( 1, get_query_var( 'paged' ) ) );
}
/**
* Return an Open Graph image.
*
* @return string
*/
function twoia_get_og_image() {
if ( is_singular() && has_post_thumbnail() ) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'briefing-hero' );
if ( $image && ! empty( $image[0] ) ) {
return $image[0];
}
}
return twoia_default_briefing_image_url();
}
/**
* Output basic meta, Open Graph, and Twitter card tags.
*/
function twoia_output_meta_tags() {
if ( twoia_seo_plugin_active() ) {
return;
}
$description = twoia_get_meta_description();
$canonical = twoia_get_canonical_url();
$title = wp_get_document_title();
$article_post_types = function_exists( 'twoia_article_post_types' ) ? twoia_article_post_types() : array( 'post' );
$type = is_singular( $article_post_types ) ? 'article' : 'website';
$image = twoia_get_og_image();
?>
<meta name="description" content="<?php echo esc_attr( $description ); ?>">
<link rel="canonical" href="<?php echo esc_url( $canonical ); ?>">
<meta property="og:site_name" content="<?php echo esc_attr( function_exists( 'twoia_public_site_name' ) ? twoia_public_site_name() : get_bloginfo( 'name' ) ); ?>">
<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( $canonical ); ?>">
<meta property="og:image" content="<?php echo esc_url( $image ); ?>">
<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', 'twoia_output_meta_tags', 5 );