| |
| <?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| function gruppa_seo() {
|
| static $seo = null;
|
| if ( null === $seo ) {
|
| global $wpdb;
|
| $row = is_singular() ? $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}jet_cct_seo WHERE seo_parent = %d LIMIT 1", get_queried_object_id() ), ARRAY_A ) : null;
|
| $seo = $row ? array_map( fn( $v ) => trim( (string) $v ), $row ) : false;
|
| }
|
| return $seo;
|
| }
|
|
|
| add_filter( 'pre_get_document_title', function ( $title ) {
|
| $seo = gruppa_seo();
|
| return ! empty( $seo['seo_title'] ) ? wp_strip_all_tags( $seo['seo_title'] ) : $title;
|
| }, 999 );
|
|
|
| add_filter( 'wp_robots', function ( $robots ) {
|
| $seo = gruppa_seo();
|
| if ( ! $seo ) return $robots;
|
| $robots = [];
|
| foreach ( explode( ',', $seo['seo_robots'] ?: 'index,follow' ) as $v ) $robots[ trim( $v ) ] = true;
|
| return $robots;
|
| }, 999 );
|
|
|
| add_filter( 'get_canonical_url', function ( $url ) {
|
| $seo = gruppa_seo();
|
| return ! empty( $seo['seo_canonical'] ) ? $seo['seo_canonical'] : $url;
|
| }, 999 );
|
|
|
| add_action( 'wp_head', function () {
|
| $seo = gruppa_seo();
|
| if ( ! $seo ) return;
|
| $tags = [
|
| 'name="description"' => $seo['seo_description'],
|
| 'name="keywords"' => $seo['seo_keywords'],
|
| 'property="og:type"' => 'website',
|
| 'property="og:url"' => wp_get_canonical_url(),
|
| 'property="og:title"' => $seo['seo_title'],
|
| 'property="og:description"' => $seo['seo_description'],
|
| 'property="og:image"' => $seo['seo_og_image'],
|
| 'name="twitter:card"' => $seo['seo_og_image'] ? 'summary_large_image' : 'summary',
|
| 'name="twitter:title"' => $seo['seo_title'],
|
| 'name="twitter:description"' => $seo['seo_description'],
|
| 'name="twitter:image"' => $seo['seo_og_image'],
|
| ];
|
| foreach ( array_filter( $tags ) as $attr => $content ) {
|
| echo '<meta ' . $attr . ' content="' . esc_attr( $content ) . '">' . "\n";
|
| }
|
| }, 20 );
|
| |
| |
Comments