Skip to content

Instantly share code, notes, and snippets.

@macbookandrew
Last active May 27, 2016 18:14
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 macbookandrew/7172aef947803f1ac439a352548f382f to your computer and use it in GitHub Desktop.
Save macbookandrew/7172aef947803f1ac439a352548f382f to your computer and use it in GitHub Desktop.
WP plugin to show a single random quote image
<?php
/*
* Plugin Name: Quote Images
* Plugin URI: https://gist.github.com/macbookandrew/7172aef947803f1ac439a352548f382f
* Description: Quote images with shortcode for random display
* Version: 1.0.0
* Author: AndrewRMinion Design
* Author URI: https://andrewrminion.com
*/
if (!defined('ABSPATH')) {
exit;
}
// Register Custom Post Type
function quote_image() {
$labels = array(
'name' => 'Quote Images',
'singular_name' => 'Quote Image',
'menu_name' => 'Quotes',
'name_admin_bar' => 'Quotes',
'archives' => 'Quote Image Archives',
'parent_item_colon' => 'Parent Quote Image:',
'all_items' => 'All Quote Images',
'add_new_item' => 'Add New Quote Image',
'add_new' => 'Add New',
'new_item' => 'New Quote Image',
'edit_item' => 'Edit Quote Image',
'update_item' => 'Update Quote Image',
'view_item' => 'View Quote Image',
'search_items' => 'Search Quote Image',
'not_found' => 'Not found',
'not_found_in_trash' => 'Not found in Trash',
'featured_image' => 'Featured Image',
'set_featured_image' => 'Set featured image',
'remove_featured_image' => 'Remove featured image',
'use_featured_image' => 'Use as featured image',
'insert_into_item' => 'Insert into quote image',
'uploaded_to_this_item' => 'Uploaded to this quote image',
'items_list' => 'Quote Images list',
'items_list_navigation' => 'Quote Images list navigation',
'filter_items_list' => 'Filter quote images list',
);
$args = array(
'label' => 'Quote Image',
'description' => 'Quote Image',
'labels' => $labels,
'supports' => array( 'title', 'thumbnail', ),
'taxonomies' => array( 'category', 'post_tag' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 25,
'menu_icon' => 'dashicons-format-quote',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
register_post_type( 'quote_image', $args );
}
add_action( 'init', 'quote_image', 0 );
// Add Custom Image Size
function quote_image_size() {
add_image_size( 'quote', 350, 350 );
}
add_action( 'after_setup_theme', 'quote_image_size' );
// Add Shortcode
function quote_image_shortcode( $atts ) {
$atts = shortcode_atts( array(
'id' => NULL,
), $atts, 'quote_iamge');
// WP_Query arguments
$args = array(
'post_type' => array( 'quote_image' ),
'post_status' => array( 'publish' ),
'nopaging' => true,
'posts_per_page' => '1',
);
// Check for specified ID
if ( $atts['id'] ) {
$args['p'] = $atts['id'];
} else {
$args['orderby'] = 'rand';
}
// The Query
$quote_image_query = new WP_Query( $args );
// The Loop
if ( $quote_image_query->have_posts() ) {
while ( $quote_image_query->have_posts() ) {
$quote_image_query->the_post();
return '<div class="quote-image">' . get_the_post_thumbnail( get_the_ID(), 'quote' ) . '</div>';
}
}
// Restore original Post Data
wp_reset_postdata();
}
add_shortcode( 'quote_image', 'quote_image_shortcode' );
// Move Featured Image Metabox
function quote_image_move_metabox() {
remove_meta_box( 'postimagediv', 'quote_image', 'side' );
remove_meta_box( 'wpseo_meta', 'quote_image', 'high' );
add_meta_box( 'postimagediv', __('Quote Image'), 'post_thumbnail_meta_box', 'quote_image', 'normal', 'high' );
}
add_action('do_meta_boxes', 'quote_image_move_metabox');
// Add Note About Quote Size
function quote_image_note_below_the_title() {
echo '<p>Quote Images should be at least 350&times;350px.</p>';
}
add_action( 'edit_form_after_title', 'quote_image_note_below_the_title' );
@macbookandrew
Copy link
Author

macbookandrew commented May 27, 2016

v1.0.0

  • Initial plugin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment