-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
103 lines (87 loc) · 2.59 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
/**
* Plugin Name: OG:Image fixer for Yoast
* Description: Uses the featured image as the Open Graph image if one exists.
* Author: John Hawkins
* Version: 1.0.1
* Author URI: https://vegasgeek.com
* Text Domain: fix-og-image
*
* @package Fix_OG_Images
*/
// exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Filter the Open Graph image to use the featured image if one exists.
*
* @param string $image The current Open Graph image.
* @return string The new Open Graph image.
*/
function vg_alter_og_image( $image ) {
$post_id = get_the_ID();
$featured_image = get_the_post_thumbnail_url( $post_id );
if ( $featured_image ) {
$image = $featured_image;
}
return $image;
}
add_filter( 'wpseo_opengraph_image', 'vg_alter_og_image' );
/**
* Filter the Open Graph image width to use the featured image width if one exists.
*
* @param int $width The current Open Graph image width.
* @return int The new Open Graph image width.
*/
function vgalter_og_image_width( $width ) {
$post_id = get_the_ID();
$featured_image_id = get_post_thumbnail_id( $post_id );
if ( $featured_image_id ) {
$image_meta = wp_get_attachment_metadata( $featured_image_id );
if ( isset( $image_meta['width'] ) ) {
$width = $image_meta['width'];
}
}
return $width;
}
add_filter( 'wpseo_opengraph_image_width', 'vgalter_og_image_width' );
/**
* Filter the Open Graph image height to use the featured image height if one exists.
*
* @param int $height The current Open Graph image height.
* @return int The new Open Graph image height.
*/
function vg_alter_og_image_height( $height ) {
$post_id = get_the_ID();
$featured_image_id = get_post_thumbnail_id( $post_id );
if ( $featured_image_id ) {
$image_meta = wp_get_attachment_metadata( $featured_image_id );
if ( isset( $image_meta['height'] ) ) {
$height = $image_meta['height'];
}
}
return $height;
}
add_filter( 'wpseo_opengraph_image_height', 'vg_alter_og_image_height' );
/**
* Filter the Open Graph image type to use the featured image type if one exists.
*
* @param string $type The current Open Graph image type.
* @return string The new Open Graph image type.
*/
function vg_alter_og_image_type( $type ) {
$post_id = get_the_ID();
$featured_image_id = get_post_thumbnail_id( $post_id );
if ( $featured_image_id ) {
$image_path = get_attached_file( $featured_image_id );
if ( $image_path ) {
$image_info = wp_check_filetype( $image_path );
if ( isset( $image_info['type'] ) ) {
$type = $image_info['type'];
}
}
}
return $type;
}
add_filter( 'wpseo_opengraph_image_type', 'vg_alter_og_image_type' );