Last active
January 1, 2016 20:45
-
-
Save gmazzap/11094807 to your computer and use it in GitHub Desktop.
Simple WordPress plugin that extends WP custom background feature to show a random image that change hourly. Images are selected via a checkbox on attachment edit screen. Wordpre for a WPSE anser, see http://wordpress.stackexchange.com/questions/141778/35541
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: GM Random Background | |
* Description: Extends WP custom background feature to show a random image that change hourly | |
* Plugin URI: http://wordpress.stackexchange.com/questions/141778/35541 | |
* Author: G. M. | |
* Author URI: http://wordpress.stackexchange.com/users/35541/g-m | |
* | |
*/ | |
/* | |
Copyright (C) 2014 Giuseppe Mazzapica | |
This program is free software; you can redistribute it and/or | |
modify it under the terms of the GNU General Public License | |
as published by the Free Software Foundation; either version 2 | |
of the License, or (at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program; if not, write to the Free Software | |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
*/ | |
add_action( 'after_setup_theme', 'gmrb_background_image_size' ); | |
add_action( 'after_setup_theme', 'gmrb_background_support' ); | |
add_action( 'attachment_submitbox_misc_actions', 'gmrb_background_checkbox' ); | |
add_action( 'update_post_meta', 'gmrb_background_checkbox_save', 20, 4 ); | |
add_filter( 'theme_mod_background_image', 'gmrb_background_custom_filter' ); | |
function gmrb_background_image_size() { | |
// using WP 3.9 crop position | |
// see https://codex.wordpress.org/Function_Reference/add_image_size#Crop_Mode | |
$width = apply_filters( 'gmrb_background_width', 1920 ); | |
$height = apply_filters( 'gmrb_background_height', 1200 ); | |
$crop = apply_filters( 'gmrb_background_crop', array( 'center', 'center') ); | |
add_image_size( 'gmrb_background', $width, $height, $crop ); | |
} | |
function gmrb_background_support() { | |
if ( ! current_theme_supports( 'custom-background' ) ) { | |
add_theme_support( 'custom-background' ); | |
} | |
} | |
function gmrb_background_checkbox() { | |
wp_nonce_field('is_background', '_is_bkgrnd_n'); | |
$html = '<div class="misc-pub-section misc-pub-filename"><label for="is_bkgrnd">'; | |
$html .= '<input id="is_bkgrnd" name="_is_background" type="checkbox" value="1"%s />'; | |
$html .= ' %s</label></div>'; | |
global $post; | |
$is = get_post_meta( $post->ID, '_is_background', TRUE ); | |
printf( $html , checked(1, $is, FALSE), esc_html__('Background') ); | |
} | |
function gmrb_background_checkbox_save( $mid, $pid, $key, $value ) { | |
$post = filter_input(INPUT_SERVER, 'REQUEST_METHOD', FILTER_SANITIZE_STRING); | |
if ( strtoupper($post) !== 'POST' ) return; | |
if ( ! $key === '_wp_attachment_metadata') return; | |
if ( ! check_admin_referer('is_background', '_is_bkgrnd_n') ) return; | |
$is = (int) filter_input(INPUT_POST, '_is_background', FILTER_SANITIZE_NUMBER_INT); | |
if ( $is === 1 ) { | |
update_post_meta($pid, '_is_background', '1' ); | |
} elseif( get_post_meta($pid, '_is_background', TRUE ) ) { | |
delete_post_meta($pid, '_is_background'); | |
} | |
} | |
function gmrb_background_get_random() { | |
// first check transient | |
$background = get_transient( 'my_background_random' ); | |
if ( empty( $background ) ) { | |
// no transient, perform a query | |
$args = array( | |
'post_type' => 'attachment', | |
'post_status' => 'inherit', | |
'orderby' => 'rand', | |
'posts_per_page' => 1, | |
'meta_query' => array( | |
array( 'key' => '_is_background', 'value' => 1, 'type' => 'NUMERIC' ) | |
) | |
); | |
$result = (array) get_posts( $args ); | |
if ( ! empty( $result ) ) { | |
$attachment = array_shift( $result ); | |
$image = wp_get_attachment_image_src( $attachment->ID, 'gmrb_background' ); | |
$background = ! empty( $image ) ? $image[0] : FALSE; | |
if ( $background ) { | |
// save transient for one hour | |
set_transient( 'my_background_random', $background, HOUR_IN_SECONDS ); | |
} | |
} | |
} | |
return $background; | |
} | |
function gmrb_background_custom_filter( $image ) { | |
// do nothing on admin or core custom background interface will not work properly | |
if ( is_admin() ) return $image; | |
$random = gmrb_background_get_random(); | |
if ( $random && filter_var( $random, FILTER_VALIDATE_URL ) ) { | |
$image = $random; | |
} | |
return $image; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
by using $post_type and limit the function gmrb_background_checkbox_save to post type "attachement", the above described issue can be fixed