Skip to content

Commit d1140c3

Browse files
committed
Add 3 main hooks in this plugin
1 parent b987a80 commit d1140c3

File tree

5 files changed

+138
-51
lines changed

5 files changed

+138
-51
lines changed

README.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Donate link: http://1fix.io/
55
Tags: featured image, metabox
66
Requires at least: 3.5
77
Tested up to: 3.9.1
8-
Stable tag: 0.7.0
8+
Stable tag: 0.8.0
99
License: GPLv2 or later
1010
License URI: http://www.gnu.org/licenses/gpl-2.0.html
1111

@@ -46,5 +46,5 @@ Custom the title, content and link / button text in the Featured Image metabox.
4646

4747
== Changelog ==
4848

49-
= 0.7.0 =
49+
= 0.8.0 =
5050
* The first version

admin/class-custom-featured-image-metabox-admin.php

Lines changed: 91 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@
2121
*/
2222
class Custom_Featured_Image_Metabox_Admin {
2323

24+
/**
25+
* Unique identifier for your plugin.
26+
*
27+
*
28+
* Call $plugin_slug from public plugin class later.
29+
*
30+
* @since 0.8.0
31+
*
32+
* @var string
33+
*/
34+
protected $plugin_slug = null;
35+
36+
protected $plugin_options = null;
37+
2438
/**
2539
* Instance of this class.
2640
*
@@ -53,6 +67,7 @@ private function __construct() {
5367
*/
5468
$plugin = Custom_Featured_Image_Metabox::get_instance();
5569
$this->plugin_slug = $plugin->get_plugin_slug();
70+
$this->plugin_options = $plugin->get_plugin_options();
5671

5772
// Add the options page and menu item.
5873
require_once( plugin_dir_path( __FILE__ ) . 'includes/settings.php' );
@@ -64,14 +79,9 @@ private function __construct() {
6479
$plugin_basename = plugin_basename( plugin_dir_path( realpath( dirname( __FILE__ ) ) ) . 'custom-featured-image-metabox.php' );
6580
add_filter( 'plugin_action_links_' . $plugin_basename, array( $this, 'add_action_links' ) );
6681

67-
/*
68-
* Define custom functionality.
69-
*
70-
* Read more about actions and filters:
71-
* http://codex.wordpress.org/Plugin_API#Hooks.2C_Actions_and_Filters
72-
*/
73-
// add_action( '@TODO', array( $this, 'action_method_name' ) );
74-
// add_filter( '@TODO', array( $this, 'filter_method_name' ) );
82+
add_action( 'add_meta_boxes', array( $this, 'change_metabox_title' ) );
83+
add_filter( 'admin_post_thumbnail_html', array( $this, 'change_metabox_content' ) );
84+
add_filter( 'media_view_strings', array( $this, 'change_media_strings' ), 10, 2 );
7585

7686
}
7787

@@ -143,29 +153,86 @@ public function add_action_links( $links ) {
143153
}
144154

145155
/**
146-
* NOTE: Actions are points in the execution of a page or process
147-
* lifecycle that WordPress fires.
156+
* Change the title of Featured Image Metabox
148157
*
149-
* Actions: http://codex.wordpress.org/Plugin_API#Actions
150-
* Reference: http://codex.wordpress.org/Plugin_API/Action_Reference
158+
* @return null
151159
*
152-
* @since 0.1.0
160+
* @since 0.8.0
153161
*/
154-
public function action_method_name() {
155-
// @TODO: Define your action hook callback here
156-
}
162+
public function change_metabox_title() {
163+
164+
$options = $this->plugin_options;
165+
166+
$screen = get_current_screen();
167+
$post_type = $screen->id;
168+
169+
if ( isset( $options[$post_type] ) && isset( $options[$post_type]['title'] ) && ! empty( $options[$post_type]['title'] ) ) {
170+
//remove original featured image metabox
171+
remove_meta_box( 'postimagediv', $post_type, 'side' );
172+
173+
//add our customized metabox
174+
add_meta_box( 'postimagediv', $options[$post_type]['title'], 'post_thumbnail_meta_box', $post_type, 'side', 'low' );
175+
}
176+
177+
} // end change_metabox_title
157178

158179
/**
159-
* NOTE: Filters are points of execution in which WordPress modifies data
160-
* before saving it or sending it to the browser.
180+
* Change metabox content
161181
*
162-
* Filters: http://codex.wordpress.org/Plugin_API#Filters
163-
* Reference: http://codex.wordpress.org/Plugin_API/Filter_Reference
182+
* @param string $content HTML string
183+
* @return string Modified content
164184
*
165-
* @since 0.1.0
185+
* @since 0.8.0
166186
*/
167-
public function filter_method_name() {
168-
// @TODO: Define your filter hook callback here
169-
}
187+
public function change_metabox_content( $content ) {
188+
189+
$options = $this->plugin_options;
190+
191+
$screen = get_current_screen();
192+
$post_type = $screen->id;
193+
194+
if ( isset( $options[$post_type] ) && isset( $options[$post_type]['instruction'] ) && ! empty( $options[$post_type]['instruction'] ) ) {
195+
$instruction = '<p class="cfim-instruction">' . $options[$post_type]['instruction'] . '</p>';
196+
197+
$content = $instruction . $content;
198+
}
199+
200+
if ( isset( $options[$post_type] ) && isset( $options[$post_type]['set_text'] ) && ! empty( $options[$post_type]['set_text'] ) ) {
201+
$content = str_replace( __( 'Set featured image' ), $options[$post_type]['set_text'], $content );
202+
}
203+
204+
if ( isset( $options[$post_type] ) && isset( $options[$post_type]['remove_text'] ) && ! empty( $options[$post_type]['remove_text'] ) ) {
205+
$content = str_replace( __( 'Remove featured image' ), $options[$post_type]['remove_text'], $content );
206+
}
207+
208+
return $content;
209+
210+
} // end change_metabox_content
211+
212+
/**
213+
* Change the strings in media manager
214+
*
215+
* @param array $strings Strings array
216+
* @param object $post Post object
217+
* @return array Modified strings array
218+
*
219+
* @since 0.8.0
220+
*/
221+
public function change_media_strings( $strings, $post ) {
222+
223+
$options = $this->plugin_options;
224+
$post_type = $post->post_type;
225+
226+
if ( ! empty( $post ) ) {
227+
if ( isset( $options[$post_type] ) && isset( $options[$post_type]['set_text'] ) && ! empty( $options[$post_type]['set_text'] ) ) {
228+
$strings['setFeaturedImage'] = $options[$post_type]['set_text'];
229+
$strings['setFeaturedImageTitle'] = $options[$post_type]['set_text'];
230+
}
231+
232+
}
233+
234+
return $strings;
235+
236+
} // end change_media_strings
170237

171238
}

admin/includes/settings.php

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class Custom_Featured_Image_Metabox_Settings {
1414
*/
1515
protected $plugin_slug = null;
1616

17+
protected $plugin_options = null;
18+
1719
/**
1820
* Instance of this class.
1921
*
@@ -34,6 +36,12 @@ private function __construct() {
3436
$plugin = Custom_Featured_Image_Metabox::get_instance();
3537
$this->plugin_slug = $plugin->get_plugin_slug();
3638

39+
if ( false == get_option( $this->plugin_slug ) ) {
40+
add_option( $this->plugin_slug, $this->default_settings() );
41+
}
42+
$this->plugin_options = $plugin->get_plugin_options();
43+
44+
// Add settings page
3745
add_action( 'admin_init', array( $this, 'admin_init' ) );
3846

3947
}
@@ -62,12 +70,8 @@ public static function get_instance() {
6270
*/
6371
public function admin_init() {
6472

65-
if ( false == get_option( $this->plugin_slug ) ) {
66-
add_option( $this->plugin_slug, $this->default_settings() );
67-
} // end if
68-
6973
$post_types = $this->supported_post_types();
70-
$options = get_option( $this->plugin_slug );
74+
$options = $this->plugin_options;
7175

7276
foreach ( $post_types as $pt ) {
7377
$post_object = get_post_type_object( $pt );
@@ -99,18 +103,18 @@ public function admin_init() {
99103
);
100104

101105
add_settings_field(
102-
'link_text',
103-
__( 'Link Text', $this->plugin_slug ),
104-
array( $this, 'link_text_callback' ),
106+
'set_text',
107+
__( 'Set Text', $this->plugin_slug ),
108+
array( $this, 'set_text_callback' ),
105109
$this->plugin_slug,
106110
$pt,
107111
$args
108112
);
109113

110114
add_settings_field(
111-
'button_text',
112-
__( 'Button Text', $this->plugin_slug ),
113-
array( $this, 'button_text_callback' ),
115+
'remove_text',
116+
__( 'Remove Text', $this->plugin_slug ),
117+
array( $this, 'remove_text_callback' ),
114118
$this->plugin_slug,
115119
$pt,
116120
$args
@@ -136,8 +140,8 @@ public function default_settings() {
136140
$keys = array(
137141
'title' => '',
138142
'instruction' => '',
139-
'link_text' => '',
140-
'button_text' => '',
143+
'set_text' => '',
144+
'remove_text' => '',
141145
);
142146
$defaults = array();
143147

@@ -193,27 +197,27 @@ public function instruction_callback( $args ) {
193197

194198
} // end instruction_callback
195199

196-
public function link_text_callback( $args ) {
200+
public function set_text_callback( $args ) {
197201

198-
$value = isset( $args[1]['link_text'] ) ? $args[1]['link_text'] : '';
202+
$value = isset( $args[1]['set_text'] ) ? $args[1]['set_text'] : '';
199203

200-
$html = '<input type"text" id="link_text" name="' . $this->plugin_slug . '[' . $args[0] . '][link_text]" value="' . $value . '" class="regular-text" />';
201-
$html .= '<p class="description">' . sprintf( __( 'Enter the custom link text to replace the "%s".', $this->plugin_slug ), __( 'Set featured image' ) ) . '</p>';
204+
$html = '<input type"text" id="set_text" name="' . $this->plugin_slug . '[' . $args[0] . '][set_text]" value="' . $value . '" class="regular-text" />';
205+
$html .= '<p class="description">' . sprintf( __( 'Enter the custom text to replace the default "%s".', $this->plugin_slug ), __( 'Set featured image' ) ) . '</p>';
202206

203207
echo $html;
204208

205-
} // end link_text_callback
209+
} // end set_text_callback
206210

207-
public function button_text_callback( $args ) {
211+
public function remove_text_callback( $args ) {
208212

209-
$value = isset( $args[1]['button_text'] ) ? $args[1]['button_text'] : '';
213+
$value = isset( $args[1]['remove_text'] ) ? $args[1]['remove_text'] : '';
210214

211-
$html = '<input type"text" id="button_text" name="' . $this->plugin_slug . '[' . $args[0] . '][button_text]" value="' . $value . '" class="regular-text" />';
212-
$html .= '<p class="description">' . sprintf( __( 'Enter the custom button text to replace the "%s".', $this->plugin_slug ), __( 'Set featured image' ) ) . '</p>';
215+
$html = '<input type"text" id="remove_text" name="' . $this->plugin_slug . '[' . $args[0] . '][remove_text]" value="' . $value . '" class="regular-text" />';
216+
$html .= '<p class="description">' . sprintf( __( 'Enter the custom text to replace the default "%s".', $this->plugin_slug ), __( 'Remove featured image' ) ) . '</p>';
213217

214218
echo $html;
215219

216-
} // end button_text_callback
220+
} // end remove_text_callback
217221

218222
/**
219223
* Validate inputs

custom-featured-image-metabox.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* Plugin Name: Custom Featured Image Metabox
1515
* Plugin URI: http://1fix.io
1616
* Description: Custom the title, content and link / button text in the Featured Image metabox.
17-
* Version: 0.7.0
17+
* Version: 0.8.0
1818
* Author: 1fixdotio
1919
* Author URI: http://1fix.io
2020
* Text Domain: cfim

public/class-custom-featured-image-metabox.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Custom_Featured_Image_Metabox {
2828
*
2929
* @var string
3030
*/
31-
const VERSION = '0.7.0';
31+
const VERSION = '0.8.0';
3232

3333
/**
3434
* Unique identifier for your plugin.
@@ -77,9 +77,25 @@ private function __construct() {
7777
* @return Plugin slug variable.
7878
*/
7979
public function get_plugin_slug() {
80+
8081
return $this->plugin_slug;
8182
}
8283

84+
/**
85+
* Get plugin options
86+
*
87+
* @return array Plugin options
88+
*
89+
* @since 0.8.0
90+
*/
91+
public function get_plugin_options() {
92+
93+
$options = get_option( $this->plugin_slug );
94+
95+
return $options;
96+
97+
} // end get_plugin_options
98+
8399
/**
84100
* Return an instance of this class.
85101
*

0 commit comments

Comments
 (0)