Default featured image - Version 1.0

Version Description

  • Plugin will now remove it's setting on plugin removal
  • added a default class to the <img> tag, if it shows a default featured image
  • The default featured image will now also return with get_post_thumbnail_id, making the chance that it fail far far smaller.
  • The image given in the media page is now validated

=

Download this release

Release Info

Developer janw.oostendorp
Plugin Icon 128x128 Default featured image
Version 1.0
Comparing to
See all releases

Code changes from version 0.9 to 1.0

Files changed (2) hide show
  1. readme.txt +45 -32
  2. set-default-featured-image.php +53 -10
readme.txt CHANGED
@@ -2,8 +2,8 @@
2
  Contributors: janwoostendorp
3
  Tags: media, image
4
  Requires at least: 3.5
5
- Tested up to: 3.5
6
- Stable tag: 0.9
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
@@ -13,23 +13,8 @@ Add a default featured image to the media settings page
13
 
14
  Add a default featured image to the media settings page. This featured image will show up if no featured image is set. Simple as that.
15
 
16
- If you want to use a different image on certain occasions use the `dfi_thumbnail_id` filter like:
17
 
18
- <?php //this will work in your themes functions.php
19
- // Don't use a featured image on page 5
20
- add_action('template_redirect', function () {
21
- if ( is_single() ) {
22
- add_filter('dfi_thumbnail_id', function () { return 0; } );
23
- }
24
- });
25
-
26
- // use a different image on the "book" posttype, it's id is 12
27
- add_action('template_redirect', function () {
28
- if ( is_single() ) {
29
- add_filter('dfi_thumbnail_id', function () { return 12; } );
30
- }
31
- });
32
- ?>
33
  = Suggestions are welcome =
34
  * Found a bug?
35
  * Want to help to translate it in your language?
@@ -41,37 +26,65 @@ If you want to use a different image on certain occasions use the `dfi_thumbnail
41
 
42
  1. Unzip the folder to the `/wp-content/plugins/` directory
43
  2. Activate the plugin through the 'Plugins' menu in WordPress
44
- 3. Go to the settigns->media page and select an image.
45
-
46
 
47
  == Frequently Asked Questions ==
48
 
49
- = can I exclude a page or give it a different image? =
 
 
 
 
 
50
 
51
- yes. you can exclude all kinds of things with the [conditional tags](http://codex.wordpress.org/Conditional_Tags).
 
 
 
 
52
 
53
- <?php //this will work in your themes functions.php
54
- // Dont use a featured image on page 5
55
  add_action('template_redirect', function () {
56
- if ( is_single() ) {
57
- add_filter('dfi_thumbnail_id', function () { return 0; } );
58
  }
59
  });
60
 
61
- // use a different image on the "book" posttype, it's id is 12
 
62
  add_action('template_redirect', function () {
63
- if ( is_single() ) {
64
- add_filter('dfi_thumbnail_id', function () { return 12; } );
65
  }
66
  });
67
- ?>
 
 
 
 
 
 
 
 
 
 
68
 
69
  == Screenshots ==
70
 
71
- 1. The button and preview as it appears on the settings page
72
  2. The media manager will start with the current selected image
73
 
74
  == Changelog ==
75
 
76
  = 0.9 =
77
- * Launch
 
 
 
 
 
 
 
 
 
 
 
2
  Contributors: janwoostendorp
3
  Tags: media, image
4
  Requires at least: 3.5
5
+ Tested up to: 3.5.1
6
+ Stable tag: 1.0
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
13
 
14
  Add a default featured image to the media settings page. This featured image will show up if no featured image is set. Simple as that.
15
 
16
+ For exceptions and to see which functions to use see the [FAQ](http://wordpress.org/extend/plugins/default-featured-image/faq/).
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  = Suggestions are welcome =
19
  * Found a bug?
20
  * Want to help to translate it in your language?
26
 
27
  1. Unzip the folder to the `/wp-content/plugins/` directory
28
  2. Activate the plugin through the 'Plugins' menu in WordPress
29
+ 3. Go to the settings->media page and select an image.
 
30
 
31
  == Frequently Asked Questions ==
32
 
33
+ = My chosen featured image doesn't show, why isn't it working? =
34
+
35
+ This plugin can't guarantee that it works. That depends on the themes. Still I want to know if it fails, so [contact me](http://wordpress.org/support/plugin/default-featured-image)
36
+
37
+ = Which functions can I use to display the featured image? =
38
+ The plugin uses the default WordPress functions `the_post_thumbnail` or `get_the_post_thumbnail`. `has_post_thumbnail` will always return true. `get_post_thumbnail_id` will return the ID set on the post or the DFI you set.
39
 
40
+ = Can I exclude a page or give it a different image? =
41
+
42
+ yes. you can exclude all kinds of things with the [conditional tags](http://codex.wordpress.org/Conditional_Tags). A few examples which you can paste in your `functions.php`
43
+
44
+ **Dont use a featured image on page 5**
45
 
 
 
46
  add_action('template_redirect', function () {
47
+ if ( is_single( 5 ) || get_the_ID() == 5 ) {
48
+ add_filter('dfi_thumbnail_id', function () { return 0; } );
49
  }
50
  });
51
 
52
+ **use a different image on the "book" posttype, it's id is 12**
53
+
54
  add_action('template_redirect', function () {
55
+ if ( is_singular( 'book' ) || get_post_type() == 'book') {
56
+ add_filter('dfi_thumbnail_id', function () { return 12; } );
57
  }
58
  });
59
+
60
+ = Can I change the HTML of the image returned? =
61
+ yes you can with the filter `dfi_thumbnail_html`.
62
+
63
+ function dfi_add_class($html, $post_id, $default_thumbnail_id, $size, $attr) {
64
+ // add a class to the existing class list
65
+ $attr['class'] = 'my-class '.$attr['class'];
66
+
67
+ return wp_get_attachment_image( $default_thumbnail_id, $size, false, $attr );
68
+ }
69
+ add_filter( 'dfi_thumbnail_html', 'dfi_add_class', 10, 5 );
70
 
71
  == Screenshots ==
72
 
73
+ 1. The setting on the media page
74
  2. The media manager will start with the current selected image
75
 
76
  == Changelog ==
77
 
78
  = 0.9 =
79
+ * Launch
80
+
81
+ = 1.0 =
82
+ * Plugin will now remove it's setting on plugin removal
83
+ * added a default class to the `<img>` tag, if it shows a default featured image
84
+ * The default featured image will now also return with `get_post_thumbnail_id`, making the chance that it fail far far smaller.
85
+ * The image given in the media page is now validated
86
+
87
+ == Upgrade Notice ==
88
+
89
+ = 1.0 =
90
+ Update makes sure that the set image will show. Everywhere.
set-default-featured-image.php CHANGED
@@ -3,7 +3,7 @@
3
  plugin name: Default featured image
4
  Plugin URI: http://wordpress.org/extend/plugins/default-featured-image/
5
  Description: Allows users to select a default feartured image in the media settings
6
- Version: 0.9
7
  Author: Jan Willem Oostendorp
8
  License: GPLv2 or later
9
  */
@@ -22,19 +22,60 @@ class default_featured_image
22
  add_action( 'admin_print_scripts-options-media.php', array( &$this, 'admin_scripts' ) );
23
  // get the preview image ajaxs call
24
  add_action( 'wp_ajax_dfi_change_preview', array( &$this, 'ajax_wrapper' ) );
 
 
25
  // display a default featured image
26
- add_filter( 'post_thumbnail_html', array( &$this, 'show_dfi' ), 10, 5 );
27
  // add a link on the plugin page to the setting
28
  add_filter('plugin_action_links', array(&$this, 'add_settings_link'), 10, 2 );
29
  // add L10n
30
  add_action( 'init', array(&$this, 'L10n') );
 
 
 
31
  }
32
 
 
 
 
33
 
34
  function L10n() {
35
  load_plugin_textdomain(self::L10n, false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
36
  }
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  /**
39
  * register the setting on the media settings page
40
  */
@@ -81,8 +122,10 @@ class default_featured_image
81
 
82
  // Validate user input
83
  function input_validation( $input ) {
84
- //@todo Validation
85
- return $input;
 
 
86
  }
87
 
88
  /**
@@ -144,16 +187,16 @@ class default_featured_image
144
  * @return string
145
  */
146
  function show_dfi( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
147
- // if an image is set return the image
148
- if ( !empty( $html ) ) {
 
 
149
  return $html;
150
- }
151
 
152
- $default_thumbnail_id = get_option( 'default_featured_image' ); //select the default thumb
153
  $default_thumbnail_id = apply_filters( 'dfi_thumbnail_id', $default_thumbnail_id );
154
 
155
- //set title to post
156
- $attr['title'] = get_the_title(); //@todo still usefull?
157
 
158
  $html = wp_get_attachment_image( $default_thumbnail_id, $size, false, $attr );
159
  $html = apply_filters( 'dfi_thumbnail_html', $html, $post_id, $default_thumbnail_id, $size, $attr );
3
  plugin name: Default featured image
4
  Plugin URI: http://wordpress.org/extend/plugins/default-featured-image/
5
  Description: Allows users to select a default feartured image in the media settings
6
+ Version: 1.0
7
  Author: Jan Willem Oostendorp
8
  License: GPLv2 or later
9
  */
22
  add_action( 'admin_print_scripts-options-media.php', array( &$this, 'admin_scripts' ) );
23
  // get the preview image ajaxs call
24
  add_action( 'wp_ajax_dfi_change_preview', array( &$this, 'ajax_wrapper' ) );
25
+ // set dfi meta key on every ocasion
26
+ add_filter( 'get_post_metadata', array(&$this, 'set_dfi_meta_key'), 10, 4 );
27
  // display a default featured image
28
+ add_filter( 'post_thumbnail_html', array( &$this, 'show_dfi' ), 20, 5 );
29
  // add a link on the plugin page to the setting
30
  add_filter('plugin_action_links', array(&$this, 'add_settings_link'), 10, 2 );
31
  // add L10n
32
  add_action( 'init', array(&$this, 'L10n') );
33
+ // remove setting on removal
34
+ register_uninstall_hook(__FILE__, array('default_featured_image', 'uninstall'));
35
+
36
  }
37
 
38
+ static function uninstall() {
39
+ delete_option( 'dfi_image_id' );
40
+ }
41
 
42
  function L10n() {
43
  load_plugin_textdomain(self::L10n, false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
44
  }
45
 
46
+ function set_dfi_meta_key( $null, $object_id, $meta_key, $single ) {
47
+ if ( is_admin() )
48
+ return;
49
+
50
+ // only affect thumbnails
51
+ if ( '_thumbnail_id' != $meta_key )
52
+ return;
53
+
54
+ //@see /wp-includes/meta.php get_metadata()
55
+ $meta_type = 'post';
56
+ $meta_cache = wp_cache_get($object_id, $meta_type . '_meta');
57
+
58
+ if ( !$meta_cache ) {
59
+ $meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
60
+ $meta_cache = $meta_cache[$object_id];
61
+ }
62
+
63
+ if ( !$meta_key )
64
+ return $meta_cache;
65
+
66
+ if ( isset($meta_cache[$meta_key]) ) {
67
+ if ( $single )
68
+ return maybe_unserialize( $meta_cache[$meta_key][0] );
69
+ else
70
+ return array_map('maybe_unserialize', $meta_cache[$meta_key]);
71
+ }
72
+
73
+ if ($single)
74
+ return get_option( 'dfi_image_id' ); // set the default featured img ID
75
+ else
76
+ return array();
77
+ }
78
+
79
  /**
80
  * register the setting on the media settings page
81
  */
122
 
123
  // Validate user input
124
  function input_validation( $input ) {
125
+ if ( wp_attachment_is_image( $input) ) {
126
+ return $input;
127
+ }
128
+ return false;
129
  }
130
 
131
  /**
187
  * @return string
188
  */
189
  function show_dfi( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
190
+ $default_thumbnail_id = get_option( 'dfi_image_id' ); //select the default thumb
191
+
192
+ // if an image is set return that image
193
+ if ( $default_thumbnail_id != $post_thumbnail_id )
194
  return $html;
 
195
 
196
+ // allow to set an other ID see the readme.txt for details
197
  $default_thumbnail_id = apply_filters( 'dfi_thumbnail_id', $default_thumbnail_id );
198
 
199
+ $attr['class'] = "attachment-{$size} default-featured-img";
 
200
 
201
  $html = wp_get_attachment_image( $default_thumbnail_id, $size, false, $attr );
202
  $html = apply_filters( 'dfi_thumbnail_html', $html, $post_id, $default_thumbnail_id, $size, $attr );