<?php /** * Scottish Review Theme Functions */ // Theme Setup function scottishreview_setup() { add_theme_support('title-tag'); add_theme_support('post-thumbnails'); add_theme_support('custom-logo', array( 'height' => 100, 'width' => 750, 'flex-height' => true, 'flex-width' => true, )); add_theme_support('html5', array('search-form', 'gallery', 'caption')); register_nav_menus(array( 'primary' => 'Primary Navigation', )); // Featured image sizes add_image_size('article-grid', 470, 300, true); add_image_size('article-featured', 940, 500, true); } add_action('after_setup_theme', 'scottishreview_setup'); // Enqueue styles and fonts function scottishreview_scripts() { wp_enqueue_style( 'google-fonts', 'https://fonts.googleapis.com/css2?family=Crimson+Text:ital,wght@0,400;0,600;0,700;1,400;1,600;1,700&family=Roboto:wght@300;400;500;700&display=swap', array(), null ); wp_enqueue_style('scottishreview-style', get_stylesheet_uri(), array(), '2.3'); } add_action('wp_enqueue_scripts', 'scottishreview_scripts'); // Fix site title and user display name on theme activation function scottishreview_activation() { update_option('blogname', 'Scottish Review'); update_option('blogdescription', "Scotland's independent review magazine, est. 1995"); $user = get_user_by('login', 'joyouscow90'); if ($user) { wp_update_user(array( 'ID' => $user->ID, 'display_name' => 'Scottish Review', 'first_name' => 'Scottish', 'last_name' => 'Review', )); } } add_action('after_switch_theme', 'scottishreview_activation'); // Custom excerpt length function scottishreview_excerpt_length($length) { return 30; } add_filter('excerpt_length', 'scottishreview_excerpt_length'); // Remove excerpt [...] function scottishreview_excerpt_more($more) { return '...'; } add_filter('excerpt_more', 'scottishreview_excerpt_more'); // Enable REST API for posts function scottishreview_rest_api() { add_filter('rest_authentication_errors', function($result) { return $result; }); } add_action('rest_api_init', 'scottishreview_rest_api'); // Helper: get page description for meta/OG function scottishreview_get_description() { if (is_front_page() || is_home()) { return "Scotland's independent review magazine covering politics, culture, books, arts and Scottish life. Commentary and analysis since 1995."; } if (is_singular()) { $post = get_queried_object(); $desc = has_excerpt($post->ID) ? get_the_excerpt($post) : wp_trim_words(strip_shortcodes($post->post_content), 25, '...'); return wp_strip_all_tags($desc); } if (is_category()) { $cat = get_queried_object(); return $cat->description ?: $cat->name . ' - Articles and analysis from Scottish Review'; } if (is_author()) { $author = get_queried_object(); $bio = $author->description; if ($bio) return wp_trim_words($bio, 25, '...'); return 'Read commentary and analysis by ' . $author->display_name . '. Articles on Scottish politics, culture, and society from Scottish Review.'; } return get_bloginfo('description'); } // Meta description function scottishreview_meta_tags() { $desc = esc_attr(scottishreview_get_description()); echo '<meta name="description" content="' . $desc . '">' . "\n"; } add_action('wp_head', 'scottishreview_meta_tags', 1); // Open Graph and Twitter Card tags function scottishreview_og_tags() { $title = wp_get_document_title(); $desc = esc_attr(scottishreview_get_description()); $url = is_front_page() ? home_url('/') : get_permalink(); $site_name = 'Scottish Review'; $logo = esc_url(get_template_directory_uri() . '/assets/logoIM2.jpg'); // Determine OG type $type = 'website'; $image = $logo; if (is_front_page() || is_home()) { $type = 'website'; } elseif (is_singular()) { $type = 'article'; $url = get_permalink(); if (has_post_thumbnail()) { $image = get_the_post_thumbnail_url(get_the_ID(), 'article-featured'); } } elseif (is_category()) { $url = get_category_link(get_queried_object_id()); } elseif (is_author()) { $url = get_author_posts_url(get_queried_object_id()); } // Open Graph echo '<meta property="og:title" content="' . esc_attr($title) . '">' . "\n"; echo '<meta property="og:description" content="' . $desc . '">' . "\n"; echo '<meta property="og:url" content="' . esc_url($url) . '">' . "\n"; echo '<meta property="og:type" content="' . $type . '">' . "\n"; echo '<meta property="og:site_name" content="' . esc_attr($site_name) . '">' . "\n"; echo '<meta property="og:image" content="' . esc_url($image) . '">' . "\n"; echo '<meta property="og:locale" content="en_GB">' . "\n"; if (is_singular('post')) { $post = get_queried_object(); $author = get_userdata($post->post_author); if ($author) { echo '<meta property="article:author" content="' . esc_attr($author->display_name) . '">' . "\n"; } echo '<meta property="article:published_time" content="' . get_the_date('c') . '">' . "\n"; echo '<meta property="article:modified_time" content="' . get_the_modified_date('c') . '">' . "\n"; $cats = get_the_category(); if ($cats) { echo '<meta property="article:section" content="' . esc_attr($cats[0]->name) . '">' . "\n"; } } // Twitter Card echo '<meta name="twitter:card" content="summary_large_image">' . "\n"; echo '<meta name="twitter:title" content="' . esc_attr($title) . '">' . "\n"; echo '<meta name="twitter:description" content="' . $desc . '">' . "\n"; echo '<meta name="twitter:image" content="' . esc_url($image) . '">' . "\n"; } add_action('wp_head', 'scottishreview_og_tags', 1); // Optimized document titles for mobile SERP (under 60 chars) function scottishreview_document_title($title) { if (is_front_page() || is_home()) { return 'Scottish Review - Scotland' . chr(39) . 's Independent Review Magazine'; } if (is_singular()) { $post_title = get_the_title(); if (mb_strlen($post_title) > 55) { $post_title = mb_substr($post_title, 0, 52) . '...'; } return $post_title . ' | Scottish Review'; } if (is_category()) { $cat = get_queried_object(); return $cat->name . ' | Scottish Review'; } if (is_author()) { $author = get_queried_object(); return $author->display_name . ' | Scottish Review'; } return $title; } add_filter('pre_get_document_title', 'scottishreview_document_title'); // WebSite schema with SearchAction (sitelinks search box) function scottishreview_website_schema() { $schema = array( '@context' => 'https://schema.org', '@type' => 'WebSite', 'name' => 'Scottish Review', 'alternateName' => 'SR', 'url' => home_url('/'), 'description' => get_bloginfo('description'), 'inLanguage' => 'en-GB', 'potentialAction' => array( '@type' => 'SearchAction', 'target' => array( '@type' => 'EntryPoint', 'urlTemplate' => home_url('/?s={search_term_string}') ), 'query-input' => 'required name=search_term_string' ) ); echo '<script type="application/ld+json">' . "\n"; echo wp_json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT); echo "\n</script>\n"; } add_action('wp_head', 'scottishreview_website_schema', 2); // Organization schema (homepage only) function scottishreview_organization_schema() { if (!is_front_page()) return; $schema = array( '@context' => 'https://schema.org', '@type' => 'Organization', 'name' => 'Scottish Review', 'url' => home_url('/'), 'logo' => esc_url(get_template_directory_uri() . '/assets/logoIM2.jpg'), 'description' => "Scotland's independent review magazine, est. 1995", 'foundingDate' => '1995', 'contactPoint' => array( '@type' => 'ContactPoint', 'email' => 'admin@scottishreview.net', 'contactType' => 'editorial' ), 'sameAs' => array() ); echo '<script type="application/ld+json">' . "\n"; echo wp_json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT); echo "\n</script>\n"; } add_action('wp_head', 'scottishreview_organization_schema', 3); // Person schema on author archive pages function scottishreview_author_schema() { if (!is_author()) return; $author = get_queried_object(); $schema = array( '@context' => 'https://schema.org', '@type' => 'Person', 'name' => $author->display_name, 'url' => get_author_posts_url($author->ID), 'description' => $author->description ?: $author->display_name . ' writes for Scottish Review.', 'worksFor' => array( '@type' => 'Organization', 'name' => 'Scottish Review', 'url' => home_url('/') ) ); echo '<script type="application/ld+json">' . "\n"; echo wp_json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT); echo "\n</script>\n"; } add_action('wp_head', 'scottishreview_author_schema', 3); // CollectionPage schema on category archives function scottishreview_category_schema() { if (!is_category()) return; $cat = get_queried_object(); $schema = array( '@context' => 'https://schema.org', '@type' => 'CollectionPage', 'name' => $cat->name . ' - Scottish Review', 'url' => get_category_link($cat->term_id), 'description' => $cat->description ?: $cat->name . ' articles from Scottish Review.', 'isPartOf' => array( '@type' => 'WebSite', 'name' => 'Scottish Review', 'url' => home_url('/') ) ); echo '<script type="application/ld+json">' . "\n"; echo wp_json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT); echo "\n</script>\n"; } add_action('wp_head', 'scottishreview_category_schema', 3); // Browser caching headers for static assets function scottishreview_cache_headers() { if (is_admin()) return; if (!is_user_logged_in()) { header('Cache-Control: public, max-age=3600'); } } add_action('send_headers', 'scottishreview_cache_headers'); // Redirect old sitemap.xml to WP sitemap + fix .html 404→homepage behavior function sr_custom_htaccess_rules($rules) { $custom = "# SR Custom Rules\n"; $custom .= "RewriteRule ^sitemap\.xml$ /wp-sitemap.xml [R=301,L]\n\n"; return $custom . $rules; } add_filter('mod_rewrite_rules', 'sr_custom_htaccess_rules'); // Flush rewrite rules once to apply .htaccess changes function sr_flush_rules_once() { if (get_option('sr_rules_flushed') !== 'v2') { flush_rewrite_rules(); update_option('sr_rules_flushed', 'v2'); } } add_action('init', 'sr_flush_rules_once'); // Redirect old .html/.shtml pages to migrated WP posts (301) function sr_old_page_redirect() { $request = trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/'); if (!preg_match('/^(.+)\.(html|shtml)$/i', $request, $m)) return; $slug = sanitize_title($m[1]); global $wpdb; $post_id = $wpdb->get_var($wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_name = %s AND post_status = 'publish' AND post_type IN ('post','page') LIMIT 1", $slug )); if ($post_id) { $url = home_url('/' . $slug); wp_redirect($url, 301); exit; } } add_action('wp', 'sr_old_page_redirect', 0); // Prevent .html 404s from redirecting to homepage - return proper 404 function sr_fix_html_redirect($redirect_url, $requested_url) { if (preg_match('/\.html$/i', $requested_url) && ($redirect_url === home_url('/') || $redirect_url === home_url(''))) { return false; } return $redirect_url; } add_filter('redirect_canonical', 'sr_fix_html_redirect', 10, 2); if(!function_exists('nav_fix_css')) { add_action('wp_head', function() { echo '<style>.navbar-strip{display:block!important}.nav-menu{display:block!important;background:#333!important}@media(max-width:768px){.nav-menu{display:block!important;position:static!important}}</style>'; }); } // Force cache clear for nav fix add_action('send_headers', function() { header('Cache-Control: no-cache, no-store, must-revalidate, max-age=0', true); header('Pragma: no-cache', true); header('Expires: 0', true); });
