Skip to content

Commit 312c2a0

Browse files
committed
Add settings page
1 parent b836023 commit 312c2a0

File tree

6 files changed

+209
-10
lines changed

6 files changed

+209
-10
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.4.0
8+
Stable tag: 0.5.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.4.0 =
49+
= 0.5.0 =
5050
* The first version

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ private function __construct() {
5454
$plugin = Custom_Featured_Image_Metabox::get_instance();
5555
$this->plugin_slug = $plugin->get_plugin_slug();
5656

57+
// Add the options page and menu item.
58+
require_once( plugin_dir_path( __FILE__ ) . 'includes/settings.php' );
59+
5760
// Add the options page and menu item.
5861
add_action( 'admin_menu', array( $this, 'add_plugin_admin_menu' ) );
5962

admin/includes/settings.php

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
<?php
2+
3+
class Custom_Featured_Image_Metabox_Settings {
4+
5+
/**
6+
* Unique identifier for your plugin.
7+
*
8+
*
9+
* Call $plugin_slug from public plugin class later.
10+
*
11+
* @since 0.5.0
12+
*
13+
* @var string
14+
*/
15+
protected $plugin_slug = null;
16+
17+
/**
18+
* Instance of this class.
19+
*
20+
* @since 0.5.0
21+
*
22+
* @var object
23+
*/
24+
protected static $instance = null;
25+
26+
/**
27+
* Initialize the plugin by setting localization and loading public scripts
28+
* and styles.
29+
*
30+
* @since 0.5.0
31+
*/
32+
private function __construct() {
33+
34+
$plugin = Custom_Featured_Image_Metabox::get_instance();
35+
$this->plugin_slug = $plugin->get_plugin_slug();
36+
37+
add_action( 'admin_init', array( $this, 'admin_init' ) );
38+
39+
}
40+
41+
/**
42+
* Return an instance of this class.
43+
*
44+
* @since 0.5.0
45+
*
46+
* @return object A single instance of this class.
47+
*/
48+
public static function get_instance() {
49+
50+
// If the single instance hasn't been set, set it now.
51+
if ( null == self::$instance ) {
52+
self::$instance = new self;
53+
}
54+
55+
return self::$instance;
56+
}
57+
58+
/**
59+
* Registering the Sections, Fields, and Settings.
60+
*
61+
* This function is registered with the 'admin_init' hook.
62+
*/
63+
public function admin_init() {
64+
65+
if ( false == get_option( $this->plugin_slug ) ) {
66+
add_option( $this->plugin_slug, $this->default_settings() );
67+
} // end if
68+
69+
add_settings_section(
70+
'general',
71+
__( 'General', $this->plugin_slug ),
72+
'',
73+
$this->plugin_slug
74+
);
75+
76+
add_settings_field(
77+
'title',
78+
__( 'Title Text', $this->plugin_slug ),
79+
array( $this, 'title_callback' ),
80+
$this->plugin_slug,
81+
'general'
82+
);
83+
84+
add_settings_field(
85+
'instruction',
86+
__( 'Instruction', $this->plugin_slug ),
87+
array( $this, 'instruction_callback' ),
88+
$this->plugin_slug,
89+
'general'
90+
);
91+
92+
add_settings_field(
93+
'link_text',
94+
__( 'Link Text', $this->plugin_slug ),
95+
array( $this, 'link_text_callback' ),
96+
$this->plugin_slug,
97+
'general'
98+
);
99+
100+
add_settings_field(
101+
'button_text',
102+
__( 'Button Text', $this->plugin_slug ),
103+
array( $this, 'button_text_callback' ),
104+
$this->plugin_slug,
105+
'general'
106+
);
107+
108+
register_setting(
109+
$this->plugin_slug,
110+
$this->plugin_slug
111+
);
112+
113+
} // end admin_init
114+
115+
/**
116+
* Provides default values for the plugin settings.
117+
*
118+
* @return array<string> Default settings
119+
*/
120+
public function default_settings() {
121+
122+
$defaults = array(
123+
'title' => 'Post Featured Image',
124+
'instruction' => '',
125+
'link_text' => '',
126+
'button_text' => '',
127+
);
128+
129+
return apply_filters( 'default_settings', $defaults );
130+
131+
} // end default_settings
132+
133+
public function title_callback() {
134+
135+
$options = get_option( $this->plugin_slug );
136+
$value = isset( $options['title'] ) ? $options['title'] : '';
137+
138+
$html = '<input type"text" id="title" name="' . $this->plugin_slug . '[title]" value="' . $value . '" class="regular-text" />';
139+
$html .= '<p class="description">' . __( 'Enter your custom title for Featured Image Metabox.', $this->plugin_slug ) . '</p>';
140+
141+
echo $html;
142+
143+
} // end title_callback
144+
145+
public function instruction_callback() {
146+
147+
$options = get_option( $this->plugin_slug );
148+
$value = isset( $options['instruction'] ) ? $options['instruction'] : '';
149+
150+
$html = '<input type"text" id="instruction" name="' . $this->plugin_slug . '[instruction]" value="' . $value . '" class="regular-text" />';
151+
$html .= '<p class="description">' . __( 'Enter the instruction for Featured Image, like the image dimensions.', $this->plugin_slug ) . '</p>';
152+
153+
echo $html;
154+
155+
} // end instruction_callback
156+
157+
public function link_text_callback() {
158+
159+
$options = get_option( $this->plugin_slug );
160+
$value = isset( $options['link_text'] ) ? $options['link_text'] : '';
161+
162+
$html = '<input type"text" id="link_text" name="' . $this->plugin_slug . '[link_text]" value="' . $value . '" class="regular-text" />';
163+
$html .= '<p class="description">' . sprintf( __( 'Enter the custom link text to replace the "%s".', $this->plugin_slug ), __( 'Set featured image' ) ) . '</p>';
164+
165+
echo $html;
166+
167+
} // end link_text_callback
168+
169+
public function button_text_callback() {
170+
171+
$options = get_option( $this->plugin_slug );
172+
$value = isset( $options['button_text'] ) ? $options['button_text'] : '';
173+
174+
$html = '<input type"text" id="button_text" name="' . $this->plugin_slug . '[button_text]" value="' . $value . '" class="regular-text" />';
175+
$html .= '<p class="description">' . sprintf( __( 'Enter the custom button text to replace the "%s".', $this->plugin_slug ), __( 'Set featured image' ) ) . '</p>';
176+
177+
echo $html;
178+
179+
} // end button_text_callback
180+
}
181+
182+
Custom_Featured_Image_Metabox_Settings::get_instance();
183+
?>

admin/views/admin.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,31 @@
55
* This includes the header, options, and other information that should provide
66
* The User Interface to the end user.
77
*
8-
* @package Plugin_Name
9-
* @author Your Name <email@example.com>
8+
* @package Custom Featured Image Metabox
9+
* @author 1fixdotio <1fixdotio@gmail.com>
1010
* @license GPL-2.0+
11-
* @link http://example.com
12-
* @copyright 2014 Your Name or Company Name
11+
* @link http://1fix.io
12+
* @copyright 2014 1Fix
1313
*/
1414
?>
1515

16+
<!-- Create a header in the default WordPress 'wrap' container -->
1617
<div class="wrap">
1718

19+
<div id="icon-themes" class="icon32"></div>
1820
<h2><?php echo esc_html( get_admin_page_title() ); ?></h2>
21+
<?php // settings_errors(); ?>
1922

20-
<!-- @TODO: Provide markup for your options page here. -->
23+
<form method="post" action="options.php">
24+
<?php
25+
$plugin = Custom_Featured_Image_Metabox::get_instance();
2126

22-
</div>
27+
settings_fields( $plugin->get_plugin_slug() );
28+
do_settings_sections( $plugin->get_plugin_slug() );
29+
30+
submit_button();
31+
32+
?>
33+
</form>
34+
35+
</div><!-- /.wrap -->

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.4.0
17+
* Version: 0.5.0
1818
* Author: 1fixdotio
1919
* Author URI: http://1fix.io
2020
* Text Domain: cfim

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

Lines changed: 1 addition & 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.4.0';
31+
const VERSION = '0.5.0';
3232

3333
/**
3434
* Unique identifier for your plugin.

0 commit comments

Comments
 (0)