Skip to content

Instantly share code, notes, and snippets.

@gbyat
Last active April 22, 2016 21:05
Show Gist options
  • Save gbyat/ff745543a1b95cd36c87f45e2d190b8c to your computer and use it in GitHub Desktop.
Save gbyat/ff745543a1b95cd36c87f45e2d190b8c to your computer and use it in GitHub Desktop.
WordPress Plugin Expamle AMP Random Image
<?php
/*
* Plugin Name: PPP Random AMP Image (needs AMP plugin to work!)
* Description: Add random category image to amp articles without post_thumbnail. Install and activate <a href="https://de.wordpress.org/plugins/amp/">AMP Plugin</a> first
*
*/
function pppf_add_media_taxonomy() {
$labels = array(
'name' => _x( 'Media Category', 'taxonomy general name' ),
'singular_name' => _x( 'Media Category', 'taxonomy singular name' ),
'menu_name' => __( 'Media Categories' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'show_admin_column' => true,
'show_in_nav_menus' => false,
'query_var' => false,
'rewrite' => false,
);
register_taxonomy( 'media-category', array( 'attachment' ), $args );
}
add_action ( 'init' , 'pppf_add_media_taxonomy' );
function pppf_get_random_image() {
$args = array(
'post_type' => 'attachment',
'post_status' => 'any',
'orderby' => 'rand', // select image by random.
'posts_per_page' => 1, // number of random image, default is 1 random image, you can change number to for example 10 to display 10 random images.
'post_mime_type' => array('image/png', 'image/x-png', 'image/jpeg', 'image/jpg'), // image mime type (image format), you can remove some mime type or adding custom mime type.
'tax_query' => array(
array(
'taxonomy' => 'media-category',
'field' => 'slug',
'terms' => 'random'
)
),
);
$images = get_posts( $args );
foreach ( $images as $image ) {
return $image->ID;
}
wp_reset_postdata();
}
function pptf_amp_modify_json_metadata( $metadata, $post ) {
if ( has_post_thumbnail( $post->ID ) ) return $metadata;
$img_id = pppf_get_random_image();
if ( $img_id ) {
$img_array = wp_get_attachment_image_src( $img_id, 'large' );
if ( !array_key_exists( 'image', $metadata ) ) {
$metadata['image'] = array(
'@type' => 'ImageObject',
'url' => $img_array[0],
'width' => $img_array[1],
'height' => $img_array[2]
);
}
}
return $metadata;
}
add_filter( 'amp_post_template_metadata', 'pptf_amp_modify_json_metadata', 10, 2 );
/*
* if post has no thumbail use random image instead
* theme must support post_thumbnails
*/
function pppf_post_thumbnail_html( $html ) {
if ( empty( $html ) ) {
$img_id = pppf_get_random_image();
$img_array = wp_get_attachment_image_src( $img_id, 'large' );
$title = get_the_title($img_id);
$html = sprintf('<img src="%1$s" width="%2$s" height="%3$s" alt="%4$s" />', esc_url($img_array[0]),esc_attr($img_array[1]),esc_attr($img_array[2]),esc_attr($title));
}
return $html;
}
add_filter( 'post_thumbnail_html', 'pppf_post_thumbnail_html' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment