• Stars
    star
    105
  • Rank 328,196 (Top 7 %)
  • Language
    PHP
  • License
    GNU General Publi...
  • Created over 12 years ago
  • Updated about 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Get the Image WordPress plugin.

Get the Image

Get the Image is a plugin that grabs images for you. It was designed to make the process of things such as adding thumbnails, feature images, and/or other images to your blog much easier, but it's so much more than that. It is an image-based representation of your WordPress posts.

What the plugin does

This plugin was made to easily get an image related to a post. This is the default method order in which the plugin attempts to grab an image.

  • Meta key (custom field).
  • Post thumbnail (WP featured image).
  • Image attachment.
  • Image embedded in the post content.
  • Default/fallback image.

Usage

The basic function call for the plugin is like so:

<?php get_the_image(); ?>

This is the only function you should use from the plugin. It expects to be called within the WordPress posts loop unless you pass it a post ID directly (post_id argument).

To do more with the image script, you'll need to use what's called function-style parameters. The following is a basic example of using function-style parameters.

<?php get_the_image( array( 'meta_key' => 'thumbnail', 'size' => 'thumbnail' ) ); ?>

Parameters

The get_the_image() function accepts a single parameter of $args, which is an array of parameters for deciding how to load an image. The following is the list of all the default arguments.

$defaults = array(

	// Post the image is associated with.
	'post_id'            => get_the_ID(),

	// Method order (see methods below).
	'order'              => array( 'meta_key', 'featured', 'attachment', 'scan', 'scan_raw', 'callback', 'default' ),

	// Methods of getting an image (in order).
	'meta_key'           => array( 'Thumbnail', 'thumbnail' ), // array|string
	'featured'           => true,
	'attachment'         => true,
	'scan'               => false,
	'scan_raw'           => false, // Note: don't use the array format option with this.
	'callback'           => null,
	'default'            => false,

	// Split image from post content (by default, only used with the 'scan_raw' option).
	'split_content'      => false,

	// Attachment-specific arguments.
	'size'               => has_image_size( 'post-thumbnail' ) ? 'post-thumbnail' : 'thumbnail',

	// Key (image size) / Value ( width or px-density descriptor) pairs (e.g., 'large' => '2x' )
	'srcset_sizes'       => array(),

	// Format/display of image.
	'link'               => 'post', // string|bool - 'post' (true), 'file', 'attachment', false
	'link_class'         => '',
	'image_class'        => false,
	'image_attr'         => array(),
	'width'              => false,
	'height'             => false,
	'before'             => '',
	'after'              => '',

	// Minimum allowed sizes.
	'min_width'          => 0,
	'min_height'         => 0,

	// Captions.
	'caption'            => false, // Default WP [caption] requires a width.

	// Saving the image.
	'meta_key_save'      => false, // Save as metadata (string).
	'thumbnail_id_save'  => false, // Set 'featured image'.
	'cache'              => true,  // Cache the image.

	// Return/echo image.
	'format'             => 'img',
	'echo'               => true,

	// Deprecated arguments.
	'custom_key'         => null, // @deprecated 0.6.0 Use 'meta_key'.
	'default_size'       => null, // @deprecated 0.5.0 Use 'size'.
	'the_post_thumbnail' => null, // @deprecated 1.0.0 Use 'featured'.
	'image_scan'         => null, // @deprecated 1.0.0 Use 'scan' or 'scan_raw'.
	'default_image'      => null, // @deprecated 1.0.0 Use 'default'.
	'order_of_image'     => null, // @deprecated 1.0.0 No replacement.
	'link_to_post'       => null, // @deprecated 1.1.0 Use 'link'.
);
  • post_id - The ID of the post to get the image for. This defaults to the current post in the loop.
  • order - Order of methods used to grab images. Defaults to array( 'meta_key', 'featured', 'attachment', 'scan', 'scan_raw', 'callback', 'default' ).
  • meta_key - This parameter refers to post meta keys (custom fields) that you use. Remember, meta keys are case-sensitive (defaults are Thumbnail and thumbnail). By default, this is an array of meta keys, but it can also be a string for a single meta key.
  • featured - This refers to the the_post_thumbnail() WordPress function. By having this set to true, you may select an image from the featured image meta box while on the edit post screen.
  • attachment - The script will look for images attached to the post (set to true by default).
  • scan - If set to true, the script will search within your post for an image that's been added.
  • scan_raw - If set to true, the script will search within your post for an image and pull the raw HTML for that image.
  • callback - A custom callback function that will be called if set. It's only called if no images are found by any other options of the plugin. However, it will be run before the default is set. The $args array is passed to the callback function as the only parameter.
  • default - Will take the input of an image URL and use it if no other images are found (no default set).
  • split_content - Whether to split the raw HTML of the found image from the post content. Default is false. This method is only used with the scan_raw method.
  • size - This refers to the size of an attached image. You can choose between thumbnail, medium, large, full, or any custom image size you have available (the default is thumbnail or post-thumbnail if theme has set a thumbnail size).
  • link - What to link the image to. 'post' (links to the post), 'file' (links to the image file), 'attachment' (links to the attachment page if image is attachment), or false (link to nothing).
  • link_class - Add a custom HTML class to the link.
  • image_attr - Array of image attributes (key is the attribute name, value is the attribute value).
  • image_class - You can give an additional class to the image for use in your CSS.
  • width - Set the width of the image on output.
  • height - Set the height of the image on output.
  • before - HTML to place before the output of the image.
  • after - HTML to place after the output of the image.
  • min_width - Minimum width of the image to get. This won't work with the scan* methods. Defaults to 0.
  • min_height - Minimum height of the image to get. This won't work with the scan* methods. Defaults to 0.
  • caption - Whether to display the image caption if it exists. Defaults to false.
  • meta_key_save - A meta key to save the image URL as. This is useful if you're not using custom fields but want to cut back on database queries by having the script automatically set the custom field for you. By default, this is set to false.
  • thumbnail_id_save - Whether to save the attachment ID as the post thumbnail (featured image) ID if no featured image is set for the post. By default, this is set to false
  • cache - Whether to use the WordPress Cache API (integrates with caching plugins) to serve the post images. By default, this is set to true.
  • format - What format to return the image in. If set to array the return value of the function will be an array of <img> attributes. All other values will return the <img> element.
  • echo - If set to true, the image is shown on the page. If set to false, the image will be returned to use in your own function. (Set to true by default.)

Some usage examples

Example 1

Let's suppose that you want to add thumbnails to your category archive pages. What you'll need to do is open your category.php file and add this code within the Loop:

<?php get_the_image(); ?>

By default, that will look for an image with the custom field key Thumbnail and thumbnail. If that image doesn't exist, it will check if a post image has been set. If that image doesn't exist, it will search for any images attached to your post.

Example 2

Let's suppose you want a full-sized image and maybe you want to grab it by a custom field key of feature. Depending on your theme, this will need to go within the Loop in whatever file is calling the featured article.

<?php get_the_image( array( 'meta_key' => 'feature', 'size' => 'full' ) ); ?>

If no feature image exists by custom field, it will look for images attached to your post.

Example 3

If you want to have a sort of fallback image, then you can set an image for the script to default to if no other images are found.

<?php get_the_image( array( 'default' => 'http://mysite.com/wp-content/uploads/example.jpg' ) ); ?>

Example 4

You can even make the script scan for images that have been added to your post with this:

<?php get_the_image( array( 'scan' => true ) ); ?>

Example 5

Saving an image to the thumbnail custom field automatically.

<?php get_the_image( array( 'meta_key_save' => 'thumbnail' ) ); ?>

A real-world example

This is an example Loop, which may differ slightly from your theme, but the concept is the same. The call to get the image can go anywhere between the opening and closing lines. In the following example, the image will appear before the post title.

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

	<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

		<?php get_the_image( array( size' => 'medium', 'image_class' => 'feature' ) ); ?>

		<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>

		<div class="entry-summary">
			<?the_excerpt(); ?>
		</div>

	</div>

<?php endwhile; endif; ?>

Protect yourself from errors in the future

Sometimes, we stop using plugins, but we forget to remove the function calls to the plugin in our theme files. When deactivated, this causes errors. To protect yourself from these errors, you can call the image script like this:

<?php if ( function_exists( 'get_the_image' ) ) {
	get_the_image();
} ?>

Basically, this just checks to see if the plugin is activated and has loaded the appropriate function.

Styling your images

The plugin will help you style your images by giving you some CSS classes to work with. It will turn your custom field keys and default size into CSS classes. You can also choose to input your own class.

By default, you can add this to your CSS:

img.thumbnail {}

Let's suppose you've used this code:

<?php get_the_image( array( 'meta_key' => array( 'Donkey Kong', 'mario' ), 'size' => 'full' ) ); ?>

This will give you these CSS classes to work with:

img.full {}
img.donkey-kong {}
img.mario {}

You can also input a custom CSS class like so:

<?php get_the_image( array( 'image_class' => 'custom-image' ) ); ?>

You will still have the size and meta_key classes plus your additional class:

img.custom-image {}
img.thumbnail {}

Support

I run a WordPress community called Theme Hybrid, which is where I fully support all of my WordPress projects, including plugins. You can sign up for an account to get plugin support for a small yearly fee.

I know. I know. You might not want to pay for support, but just consider it a donation to the project. To continue making cool, GPL-licensed plugins and having the time to support them, I must pay the bills.

Copyright and License

Get the Image is licensed under the GNU GPL, version 2 or later.

2008 – 2017 © Justin Tadlock.

More Repositories

1

members

Members WordPress plugin.
PHP
409
star
2

content-type-standards

WordPress community-curated standards for common post types, taxonomies, and metadata.
PHP
303
star
3

butterbean

A neat little post meta framework.
PHP
204
star
4

hybrid-base

A base theme for building with the Hybrid Core framework.
PHP
155
star
5

custom-content-portfolio

A portfolio plugin.
PHP
102
star
6

grid-columns

For the love of all that is Holy, stop putting 22 column shortcodes in a theme for what's possible with one. A plugin.
PHP
98
star
7

theme-mediaelement

Base stylesheet for theme developers to work with the WordPress media player (mediaelement.js).
CSS
86
star
8

customizer-typography

Proof-of-concept for a typography class and multiple settings tied to a customizer control.
PHP
78
star
9

message-board

A simple forum plugin for us simple folks.
PHP
63
star
10

stargazer

A new theme in the making.
CSS
58
star
11

whistles

Something new. Something different. Tabs, accordions, and all that jazz. Done right.
PHP
50
star
12

restaurant

Base plugin for handling restaurant sites. Created for themers building restaurant themes.
47
star
13

trt-customizer-pro

Repository with examples of using the Customize API to make pro sections, controls, etc.
PHP
46
star
14

admin-themes

Admin themes plugin for WordPress.
PHP
37
star
15

block-pattern-builder

Build block patterns from the WordPress admin.
PHP
37
star
16

exhale

A WordPress theme project.
PHP
34
star
17

series

Official home of the Series plugin for WordPress.
PHP
31
star
18

cleaner-gallery

Official repository for the Cleaner Gallery WordPress plugin.
PHP
23
star
19

first-draft

Starting point for WordPress block themes.
PHP
22
star
20

entry-views

A WordPress plugin for tracking the number of views a post gets.
PHP
22
star
21

saga

Saga: A WordPress theme for writers.
CSS
20
star
22

toot

Just a little testimonials plugin.
PHP
18
star
23

custom-header-extended

Per-post custom headers for WordPress.
PHP
17
star
24

clean-my-archives

Official repository for the Clean My Archives WordPress plugin.
PHP
17
star
25

registration-honeypot

WordPress plugin for stopping most spambot registrations via a simple honeypot method.
PHP
17
star
26

hybrid

Repository for the Hybrid WordPress theme.
PHP
15
star
27

members-role-hierarchy

Hierarchical roles add-on plugin for Members.
PHP
14
star
28

widgets-reloaded

Home of the Widgets Reloaded plugin.
PHP
14
star
29

socially-awkward

Theme with a heavy emphasis on post formats and media.
PHP
13
star
30

butterbean-example

Example plugin using ButterBean.
PHP
12
star
31

extant

PHP
11
star
32

jt-lang

Quick dev plugin for language testing.
PHP
11
star
33

members-role-levels

Plugin to expose user/role levels in the UI of the Members plugin.
PHP
11
star
34

custom-background-extended

Per-post custom backgrounds for WordPress.
9
star
35

my-snippets

My Snippets WordPress plugin.
PHP
9
star
36

ravel

Ravel WordPress theme
CSS
9
star
37

picturesque

Picturesque WordPress theme built from the Hybrid Core framework.
PHP
8
star
38

members-admin-access

PHP
7
star
39

avatars-meta-box

Replaces the post author drop-down with author avatars to select.
PHP
7
star
40

custom-classes

Custom Classes WordPress plugin repository.
PHP
7
star
41

query-posts

Query Posts widget
PHP
5
star
42

mullet

Fork of the Grunion contact form because it's now packaged and maintained in Jetpack.
5
star
43

just-me

A WordPress theme idea in progress.
PHP
5
star
44

news

Repository for the News WordPress theme.
PHP
5
star
45

quote-this

Quote This WordPress plugin.
PHP
5
star
46

members-core-create-caps

PHP
5
star
47

disable-post-format-ui

Plugin to disable the post format UI on the edit post screen.
PHP
5
star
48

sliding-panel

Official home of the Sliding Panel plugin for WordPress.
PHP
5
star
49

picturesque-child

Example child theme for the Picturesque parent theme for WordPress.
PHP
4
star
50

noah-edit-control

Custom plugin for Noah, client.
PHP
4
star
51

hybrid-tabs

Hybrid Tabs WordPress plugin
PHP
4
star
52

prevent-password-reset

Official home of the Prevent Password Reset plugin.
PHP
4
star
53

role-map-edd-members

Map user roles to EDD Members options.
PHP
4
star
54

post-format-meta-box

Plugin to replace WordPress 3.6 post format UI with pre-3.6 meta box.
PHP
4
star
55

widget-title-html

Allows limited, inline HTML in widget titles.
PHP
4
star
56

reverse-comment-textarea

Reverts the "comment" field position to below the other form fields.
PHP
3
star
57

wporg-patterns

Block patterns for WordPress.org.
HTML
3
star
58

unique

Unique WordPress theme on GitHub.
PHP
3
star
59

chun

Theme in progress (temporary repo name)
PHP
3
star
60

theme-hybrid-updater

Updater for ThemeHybrid.com themes.
PHP
3
star
61

my-life

Repository for the My Life WordPress theme.
PHP
3
star
62

comments-user-column

Adds a column to the edit comments screen that displays a comment author's site display name if they were logged in while commenting.
PHP
3
star
63

template-tag-shortcodes

Shortcodes that mirror the WordPress template tags.
PHP
3
star
64

cpt-generator

A custom post type generator.
PHP
3
star
65

th5

Version 5 of ThemeHybrid.com.
PHP
3
star
66

gutenberg-tests

Tests and ideas to work with the Gutenberg project.
JavaScript
2
star
67

theme-classic-header

WordPress block theme with classic header.
PHP
2
star
68

exhale-unravel

Dark child theme for the Exhale WordPress theme.
PHP
2
star
69

members-privacy-caps

PHP
2
star
70

saga-child

Example child theme for the Saga WordPress theme
PHP
2
star
71

book-query-loop-var

Book custom post type with a query loop variation.
PHP
2
star
72

literary

Plugin for making a "writing" or "literature" section on a Web site.
2
star
73

theme-handbook

Theme handbook overhaul.
2
star
74

trending

Repository for the Trending WordPress theme
PHP
2
star
75

dancing-in-the-moonlight

A fun Christmas child theme for Stargazer.
CSS
2
star
76

prototype

Repository for the Prototype WordPress theme.
PHP
2
star
77

retro-fitted

Repository for the Retro-fitted WordPress theme.
PHP
2
star
78

theme-review

PHP
1
star
79

structure

Repository for the Structure WordPress theme.
PHP
1
star
80

th

1
star
81

nova

Something different.
PHP
1
star
82

react-tic-tac-toe

Tic-tac-toe built from Intro to React tutorial.
JavaScript
1
star
83

mp6-expand-widget-controls

Allows widget controls to expand outside of the sidebar on the widgets screen when using the MP6 plugin.
PHP
1
star
84

the-last

Secret theme project in the works.
1
star
85

th6

ThemeHybrid.com version 6 theme.
PHP
1
star
86

exhale-mountains

PHP
1
star
87

twiggy

A clean and minimal child theme for the Stargazer WordPress theme.
PHP
1
star
88

tt1-block-parts

Demo child theme of Twenty Twenty-One using block template parts.
PHP
1
star
89

butterbean-lesson-1

Plugin example from Lesson #1 of the ButterBean tutorial series.
PHP
1
star
90

book-review-variations

Example of creating custom Query Loop variations.
JavaScript
1
star
91

exhale-manuscript

PHP
1
star
92

jt-blog

Backup of my blog posts
1
star