-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmywidget-useful.php
159 lines (126 loc) · 3.85 KB
/
mywidget-useful.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/*
Plugin Name: My Useful Widget
Plugin URI: http://mydomain.com
Description: My useful widget
Author: Me
Version: 1.0
Author URI: http://mydomain.com
*/
// Block direct requests
if ( !defined('ABSPATH') )
die('-1');
add_action( 'widgets_init', function(){
register_widget( 'My_Useful_Widget' );
});
/**
* Adds My_Widget widget.
*/
class My_Useful_Widget extends WP_Widget {
/**
* Register widget with WordPress.
*/
function __construct() {
parent::__construct(
'My_Useful_Widget', // Base ID
__('My Useful Widget', 'text_domain'), // Name
array('description' => __( 'My useful widget!', 'text_domain' ),) // Args
);
}
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget( $args, $instance ) {
// get the excerpt of the required story
if ( $instance['story_id'] == 0 ) {
$gp_args = array(
'posts_per_page' => 1,
'post_type' => 'story',
'orderby' => 'post_date',
'order' => 'desc',
'post_status' => 'publish'
);
$posts = get_posts( $gp_args );
if ( $posts ) {
$post = $post[0];
} else {
$post = null;
}
} else {
$post = get_post( $instance['story_id'] );
}
if ( array_key_exists('before_widget', $args) ) echo $args['before_widget'];
if ( $post ) {
echo get_the_post_thumbnail( $post->ID, array(250,500), array('class'=>'story_featured_img') );
echo '<h3 class="story_widget_title">' . apply_filters( 'widget_title', $post->post_title ). '</h3>';
echo '<p class="story_widget_excerpt">' . $post->post_excerpt . '</p>';
echo '<p class="story_widget_readmore"><a href="' . get_permalink( $post->ID ) . '" title="Read the story, ' . $post->post_title . '">more...</a></p>';
} else {
echo __( 'No recent story found.', 'text_domain' );
}
if ( array_key_exists('after_widget', $args) ) echo $args['after_widget'];
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
if ( isset( $instance[ 'story_id' ] ) ) {
$story_id = $instance[ 'story_id' ];
}
else {
$story_id = 0;
}
?>
<p>
<label for="<?php echo $this->get_field_id( 'story_id' ); ?>"><?php _e( 'Story:' ); ?></label>
<select id="<?php echo $this->get_field_id( 'story_id' ); ?>" name="<?php echo $this->get_field_name( 'story_id' ); ?>">
<option value="0">Most recent</option>
<?php
// get the exceprt of the most recent story
$gp_args = array(
'posts_per_page' => -1,
'post_type' => 'story',
'orderby' => 'post_date',
'order' => 'desc',
'post_status' => 'publish'
);
$posts = get_posts( $gp_args );
foreach( $posts as $post ) {
$selected = ( $post->ID == $story_id ) ? 'selected' : '';
if ( strlen($post->post_title) > 30 ) {
$title = substr($post->post_title, 0, 27) . '...';
} else {
$title = $post->post_title;
}
echo '<option value="' . $post->ID . '" ' . $selected . '>' . $title . '</option>';
}
?>
</select>
</p>
<?php
}
/**
* Sanitize widget form values as they are saved.
*
* @see WP_Widget::update()
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['story_id'] = ( ! empty( $new_instance['story_id'] ) ) ? strip_tags( $new_instance['story_id'] ) : '';
return $instance;
}
} // class My_Widget