Created
April 17, 2016 11:19
-
-
Save ziemekpr0/d559abfda8416694d18fc8d42aa8aebd to your computer and use it in GitHub Desktop.
Pobiera losowe zdjęcie z biblioteki mediów albo wskazanego katalogu, które za pomocą shortcode-a można umieścić na stronie lub wpisie. Shortcode: [get_random_photo] dla losowego obrazka z biblioteki mediów - wyświetla miniaturkę jako link do pełnej wersji obrazka, albo [get_random_dir_photo], dla losowego obrazka ze zdefiniowanego w funkcji kata…
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 | |
/** | |
* Plugin Name: Get Random Photo | |
* Description: Pobiera losowe zdjęcie z biblioteki mediów albo wskazanego katalogu, które za pomocą shortcode-a można umieścić na stronie lub wpisie. Shortcode: [get_random_photo] dla losowego obrazka z biblioteki mediów - wyświetla miniaturkę jako link do pełnej wersji obrazka, albo [get_random_dir_photo], dla losowego obrazka ze zdefiniowanego w funkcji katalogu - domyslnie z wp-content/uploads/my_photos. | |
* Version: 1.0 | |
* Author: ziemekpr0 | |
* Author Email: ziemekpr0@gmail.com | |
* Author URI: http://wpadmin.pl | |
* License: GPLv2 or later | |
*/ | |
/* Security check - block direct access */ | |
if (!defined('ABSPATH')) exit('No direct script access allowed'); | |
add_shortcode('get_random_photo', 'get_random_photo'); | |
function get_random_photo() | |
{ | |
$args = array( | |
'post_type' => 'attachment', | |
'post_mime_type' =>'image', | |
'post_status' => 'inherit', | |
'orderby' => 'rand', | |
'posts_per_page' => 1 | |
); | |
$query = new WP_Query($args); | |
if(empty($query->posts)) { | |
$output = '<p>Nic nie znaleziono.</p>'; | |
} | |
else { | |
$output = '<a href="'. wp_get_attachment_url($query->posts[0]->ID) .'">'. wp_get_attachment_image($query->posts[0]->ID, 'thumbnail') .'</a>'; | |
} | |
wp_reset_query(); | |
echo $output; | |
return; | |
} | |
add_shortcode('get_random_dir_photo', 'get_random_dir_photo'); | |
function get_random_dir_photo() | |
{ | |
/* Zdefiniuj prawidłową ścieżkę do katalogu: */ | |
/* Najlepiej, aby znajdował się w katalogu wp-content/uploads */ | |
/* Przykład dla katalogu my_photos, znajdującego się w wp-content/uploads/my_photos */ | |
$dir_name = 'my_photos'; | |
$wp_upload_dir = wp_upload_dir(); | |
$wp_upload_path = realpath($wp_upload_dir['basedir'] . DIRECTORY_SEPARATOR . $dir_name); | |
$wp_upload_url = $wp_upload_dir['baseurl'] .'/'. $dir_name . '/'; | |
if(!is_dir($wp_upload_path)) | |
{ | |
echo "Podana ścieżka do katalogu jest nieprawidłowa."; | |
return; | |
} | |
$images = glob($wp_upload_path . DIRECTORY_SEPARATOR .'*.{jpg,jpeg,png,gif}', GLOB_BRACE); | |
$image = $images[array_rand($images)]; | |
$image_url = $wp_upload_url . basename($image); | |
echo '<img src="'. $image_url .'" alt="Losowe zdjęcie">'; | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment