Skip to content

Instantly share code, notes, and snippets.

@ziemekpr0
Created April 17, 2016 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ziemekpr0/42a811e524ddaa14b791b07619fda892 to your computer and use it in GitHub Desktop.
Save ziemekpr0/42a811e524ddaa14b791b07619fda892 to your computer and use it in GitHub Desktop.
Prosty plugin do WordPressa. Pobiera jedno losowe zdjęcie z biblioteki mediów. Za pomocą shortcode, można to zdjęcie umieścić we wpisach lub stronach.
<?php
/**
* Plugin Name: Get Random Photo
* Description: Pobiera losowe zdjęcie z biblioteki mediów, które za pomocą shortcode-a można umieścić na stronie lub wpisie. Shortcode: [get_random_photo].
* 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment