-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
90 lines (72 loc) · 3.4 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
if ( function_exists('register_sidebar'))
register_sidebar();
class Newsletter_Signup_Widget extends WP_Widget
{
// Register the widget
function Newsletter_Signup_Widget() {
// Set some widget options
$widget_options = array( 'description' => 'Allow users to signup to your newsletter easily', 'classname' => 'newsletter-signup' );
// Set some control options (width, height etc)
$control_options = array( 'width' => 300 );
// Actually create the widget (widget id, widget name, options...)
$this->WP_Widget( 'newsletter-signup-widget', 'Newsletter Signup', $widget_options, $control_options );
}
// Output the content of the widget
function widget($args, $instance) {
extract( $args ); // Don't worry about this
// Get our variables
$title = apply_filters( 'widget_title', $instance['title'] );
$text = $instance['text'];
$code = $instance['code'];
// This is defined when you register a sidebar
echo $before_widget;
// If our title isn't empty then show it
if ( $title ) {
echo $before_title . $title . $after_title;
}
// If our description isn't empty then show it
if ( $text ) {
echo '<p>'. $text .'</p>';
}
// If we have some code then output it
if ( $code ) {
echo $code;
}
// This is defined when you register a sidebar
echo $after_widget;
}
// Output the admin options form
function form($instance) {
// These are our default values
$defaults = array( 'title' => 'Example', 'text' => 'Subscribe to our newsletter using the form below.', 'code' => '' );
// This overwrites any default values with saved values
$instance = wp_parse_args( (array) $instance, $defaults );
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
<input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" type="text" class="widefat" />
</p>
<p>
<label for="<?php echo $this->get_field_id('text'); ?>"><?php _e('Description:'); ?></label>
<input id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>" value="<?php echo $instance['text']; ?>" type="text" class="widefat" />
</p>
<p>
<label for="<?php echo $this->get_field_id('code'); ?>"><?php _e('Code:'); ?></label>
<textarea id="<?php echo $this->get_field_id('code'); ?>" name="<?php echo $this->get_field_name('code'); ?>" rows="10" class="widefat"><?php echo $instance['code']; ?></textarea>
</p>
<?php
}
// Processes the admin options form when saved
function update($new_instance, $old_instance) {
// Get the old values
$instance = $old_instance;
// Update with any new values (and sanitise input)
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['text'] = strip_tags( $new_instance['text'] );
$instance['code'] = $new_instance['code'];
return $instance;
}
}
add_action('widgets_init', create_function('', 'return register_widget("Newsletter_Signup_Widget");'));
?>