-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom-image-api.php
More file actions
83 lines (59 loc) · 1.85 KB
/
random-image-api.php
File metadata and controls
83 lines (59 loc) · 1.85 KB
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
<?php
/*
Plugin Name: Random Image API
Description: WordPress Plugin with REST API Route to Serve Up Images
Version: 0.2.0
Author: Scot Rumery
Author URI: https://rumspeed.com/
License: GPLv2
*/
define( 'RANDOM_IMAGE_API_VERSION', '0.2.0' );
define( 'RANDOM_IMAGE_API_DIR', dirname( __FILE__ ) );
define( 'RANDOM_IMAGE_API_URI', plugins_url( '' , __FILE__ ) );
/**
* Register the REST route.
*
* @see http://example.com/wp-json/fullscreenimage/v1/random-image/
*/
add_action( 'rest_api_init', function () {
register_rest_route( 'fullscreenimage/v1', '/random-image/', array(
'methods' => 'GET, POST',
'callback' => 'fullscreenimage__show_random_image',
'show_in_index' => false,
) );
} );
/**
* Show random image
*/
function fullscreenimage__show_random_image(){
$image_ids = get_posts(
array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_status' => 'inherit',
'numberposts' => 1,
'orderby' => 'rand',
'fields' => 'ids',
) );
// convert ids to urls
// $images = array_map( "wp_get_attachment_url", $image_ids );
// convert ids to paths
$images = array_map( "get_attached_file", $image_ids );
$image_id = $image_ids[ 0 ];
$image_path = $images[ 0 ];
// make sure url is readable -- if not, stop!
if ( ! is_readable( $image_path ) ) {
wp_die( "File is not readable: $image_path" );
}
$image = file_get_contents( $image_path );
$type = get_post_mime_type( $image_id );
if ( empty ( $type ) ) {
$type = "image/jpg";
}
// output headers and image data
nocache_headers();
header( "Content-type: $type;" );
header( "Content-Length: " . strlen( $image ) );
echo $image;
die();
}