Created
October 11, 2019 20:40
-
-
Save sybrew/57dd909654edefd3871095fb0e4bf137 to your computer and use it in GitHub Desktop.
Example snippet to prepend and append image generators for The SEO Framework.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Do not include this PHP opening tag if PHP is already opened... | |
// Ref: https://theseoframework.com/docs/using-filters/ | |
add_filter( 'the_seo_framework_image_generation_params', 'my_tsf_custom_image_generation_args', 10, 3 ); | |
/** | |
* Adjusts image generation parameters. | |
* | |
* @link https://theseoframework.com/docs/api/filters/#append-image-generators-for-social-images | |
* @param array $params : [ | |
* string size: The image size to use. | |
* boolean multi: Whether to allow multiple images to be returned. | |
* array cbs: The callbacks to parse. Ideally be generators, so we can halt remotely. | |
* array fallback: The callbacks to parse. Ideally be generators, so we can halt remotely. | |
* ]; | |
* @param array|null $args The query arguments. Contains 'id' and 'taxonomy'. | |
* Is null when query is autodetermined. | |
* @return array $params | |
*/ | |
function my_tsf_custom_image_generation_args( $params = [], $args = null, $context = 'social' ) { | |
// Let's not mess with non-social sharing images. | |
if ( 'social' !== $context ) | |
return $params; | |
if ( null === $args ) { | |
// In the loop. | |
if ( is_singular() ) { | |
$params['cbs'] = array_merge( | |
[ 'custom' => 'my_custom_tsf_singular_image_generator' ], // prepend to regular callbacks. | |
$params['cbs'], | |
[ 'other' => 'my_other_tsf_custom_singular_image_generator' ] // append to regular callbacks. | |
); | |
} | |
} else { | |
// Out the loop. Use $args to evaluate the query... | |
if ( ! $args['taxonomy'] ) { | |
// Singular. | |
$params['cbs'] = array_merge( | |
[ 'custom' => 'my_custom_tsf_singular_image_generator' ], // prepend to regular callbacks. | |
$params['cbs'], | |
[ 'other' => 'my_other_tsf_custom_singular_image_generator' ] // append to regular callbacks. | |
); | |
} | |
} | |
return $params; | |
} | |
/** | |
* Generates image URL and ID via my_get_image_value. | |
* | |
* @generator | |
* | |
* @param array|null $args The query arguments. Accepts 'id' and 'taxonomy'. | |
* Leave null to autodetermine query. | |
* @param string $size The size of the image to get. | |
* @yield array : { | |
* string url: The image URL location, | |
* int id: The image ID, | |
* } | |
*/ | |
function my_custom_tsf_singular_image_generator( $args = null, $size = 'full' ) { | |
// Obtain the post ID... (unused in this example) | |
$post_id = isset( $args['id'] ) ? $args['id'] : the_seo_framework()->get_the_real_ID(); | |
// Get the image... | |
$image_params = [ | |
'url' => 'https://example.com/path/to/image.jpg', | |
'id' => 42, // Optional.. | |
]; | |
// Feel free to loop this. Ref: `The_SEO_Framework\Builders\Images::get_content_image_details()` | |
if ( ! empty( $image_params['url'] ) ) { | |
yield [ | |
'url' => $image_params['url'], | |
'id' => isset( $image_params['id'] ) ? $image_params['id'] : 0, // Optional. Used for alt-tag and dimension fetching. | |
]; | |
} else { | |
// Since we don't support PHP 7.0+ only yet in TSF, we must trigger the generator here on failure ("yield from" is unavailable). | |
yield [ | |
'url' => '', | |
'id' => 0, | |
]; | |
} | |
} | |
// Refer to my_tsf_custom_singular_image_generator() | |
function my_other_tsf_custom_singular_image_generator( $args = null, $size = 'full' ) { } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment