Home / Disable / CCT MetaSEO Relations
Duplicate Snippet

Embed Snippet on Your Site

CCT MetaSEO Relations

Code Preview
php
<?php
/**
 * =========================================
 * Gruppa CCT SEO
 * CCT: SEO
 * Relation: post_id -> WP Post ID
 * =========================================
 */
/**
 * =========================================
 * SAFE GET
 * =========================================
 */
function gt_safe($obj, $key, $default = '') {
    if (is_object($obj) && isset($obj->$key)) {
        return trim((string) $obj->$key);
    }
    return $default;
}
/**
 * =========================================
 * GET SEO CCT DATA
 * =========================================
 */
function gt_get_seo_data() {
    static $cache = null;
    if ($cache !== null) {
        return $cache;
    }
    global $wpdb;
    $post_id = get_queried_object_id();
    if (!$post_id) {
        return false;
    }
    $table = $wpdb->prefix . 'jet_cct_seo';
    $cache = $wpdb->get_row(
        $wpdb->prepare(
            "SELECT *
             FROM {$table}
             WHERE post_id = %d
             LIMIT 1",
            $post_id
        )
    );
    return $cache;
}
/**
 * =========================================
 * SEO TITLE
 * =========================================
 */
add_filter('pre_get_document_title', function ($title) {
    if (!is_singular()) {
        return $title;
    }
    $seo = gt_get_seo_data();
    if (!$seo) {
        return $title;
    }
    $seo_title = gt_safe($seo, 'seo_title');
    return $seo_title
        ? wp_strip_all_tags($seo_title)
        : $title;
}, 999);
/**
 * =========================================
 * ROBOTS
 * =========================================
 */
add_filter('wp_robots', function ($robots) {
    if (!is_singular()) {
        return $robots;
    }
    $seo = gt_get_seo_data();
    if (!$seo) {
        return $robots;
    }
    $value = gt_safe(
        $seo,
        'seo_robots',
        'index,follow'
    );
    $robots = [];
    switch ($value) {
        case 'index,nofollow':
            $robots['index'] = true;
            $robots['nofollow'] = true;
            break;
        case 'noindex,follow':
            $robots['noindex'] = true;
            $robots['follow'] = true;
            break;
        case 'noindex,nofollow':
            $robots['noindex'] = true;
            $robots['nofollow'] = true;
            break;
        default:
            $robots['index'] = true;
            $robots['follow'] = true;
            break;
    }
    return $robots;
}, 999);
/**
 * =========================================
 * META TAGS
 * =========================================
 */
add_action('wp_head', function () {
    if (!is_singular()) {
        return;
    }
    $seo = gt_get_seo_data();
    if (!$seo) {
        return;
    }
    $seo_title       = gt_safe($seo, 'seo_title');
    $seo_description = gt_safe($seo, 'seo_description');
    $seo_keywords    = gt_safe($seo, 'seo_keywords');
    $seo_canonical   = gt_safe($seo, 'seo_canonical');
    $seo_og_image    = gt_safe($seo, 'seo_og_image');
    if (!$seo_canonical) {
        $seo_canonical = get_permalink();
    }
    /**
     * Description
     */
    if ($seo_description) {
        echo '<meta name="description" content="' . esc_attr($seo_description) . '">' . PHP_EOL;
        echo '<meta property="og:description" content="' . esc_attr($seo_description) . '">' . PHP_EOL;
    }
    /**
     * Keywords
     */
    if ($seo_keywords) {
        echo '<meta name="keywords" content="' . esc_attr($seo_keywords) . '">' . PHP_EOL;
    }
    /**
     * Canonical
     */
    echo '<link rel="canonical" href="' . esc_url($seo_canonical) . '">' . PHP_EOL;
    /**
     * OG Title
     */
    if ($seo_title) {
        echo '<meta property="og:title" content="' . esc_attr($seo_title) . '">' . PHP_EOL;
    }
    /**
     * OG Image
     */
    if ($seo_og_image) {
        echo '<meta property="og:image" content="' . esc_url($seo_og_image) . '">' . PHP_EOL;
    }
    /**
     * OG Defaults
     */
    echo '<meta property="og:type" content="website">' . PHP_EOL;
    echo '<meta property="og:url" content="' . esc_url(get_permalink()) . '">' . PHP_EOL;
}, 20);

Comments

Add a Comment