I'm new to WordPress. I created my first ugly widget. It located in wp-content/plugins/my-awesome-widget/widget_init.php (no more files in this folder). And it's working. I created a sidebar and registered it in functions.php in my theme folder:
function register_page_widgets()
{
if (function_exists('register_sidebar')) {
register_sidebar(
array(
'name' => 'Page sidebar',
'id' => 'page_sidebar',
'description' => '',
'class' => '',
'before_widget' => '<div class="page-sidebar">',
'before_title' => '<div class="title"><span>',
'after_title' => '</span></div>',
'after_widget' => '</div>'
)
);
}
}
add_action( 'widgets_init', 'register_page_widgets' );
All widgets, that located in this sidebar, wrapped in elements, that I defined in array: before_widget, before_title etc. Besides my widget. In my code (in widget_init.php) I wrote:
print_r($args);
And I got the result:
Array
(
[name] => Page sidebar
[id] => page_sidebar
[description] =>
[class] =>
[before_widget] =>
[after_widget] =>
[before_title] =>
[after_title] =>
[widget_id] => my_first_widget
[widget_name] => My First Widget
)
Somehow before_widget, after_widget, before_title and after_title are empty. What's wrong?
My code:
<?php
/*
Plugin Name: My Own Recent Posts
Plugin URI: http://www.example.com/textwidget
Description: An example plugin to demonstrate widgets API in WordPress
Version: 0.1
Author: Author Name
Author URI: http://www.example.com
License: GPL2
*/
add_action( 'widgets_init', function(){
register_widget( 'My_Own_Recent_Posts' );
});
class My_Own_Recent_Posts extends WP_Widget {
public function __construct() {
parent::__construct(
'My_Own_Recent_Posts',
'My First Widget',
array( 'description' => __( 'My First Widget', 'text_domain' ), )
);
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = sanitize_text_field( $new_instance['title'] );
$instance['count_of_posts'] = sanitize_text_field( $new_instance['count_of_posts'] );
$instance['words_to_display'] = sanitize_text_field( $new_instance['words_to_display'] );
$instance['post_types'] = $new_instance['post_types'];
return $instance;
}
public function form( $instance ) { ?>
<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat"
id="<?php echo $this->get_field_id( 'title' ); ?>"
name="<?php echo $this->get_field_name( 'title' ); ?>"
type="text"
value="<?php echo $instance['title']; ?>" />
</p>
<p><label for="<?php echo $this->get_field_id( 'count_of_posts' ); ?>"><?php _e( 'Count of posts:' ); ?></label>
<input class="widefat"
id="<?php echo $this->get_field_id( 'count_of_posts' ); ?>"
name="<?php echo $this->get_field_name( 'count_of_posts' ); ?>"
type="number"
max="50"
min="1"
value="<?php echo $instance['count_of_posts']; ?>" />
</p>
<p><label for="<?php echo $this->get_field_id( 'words_to_display' ); ?>"><?php _e( 'Words from post to display:' ); ?></label>
<input class="widefat"
id="<?php echo $this->get_field_id( 'words_to_display' ); ?>"
name="<?php echo $this->get_field_name( 'words_to_display' ); ?>"
type="number"
max="50"
min="1"
value="<?php echo $instance['words_to_display']; ?>" />
</p>
<p><label for="<?php echo $this->get_field_id( 'post_types' ); ?>"><?php _e( 'Select post types:' ); ?><br></label>
<?php
$args = array('_builtin' => '', 'public' => true);
$post_types = get_post_types($args, 'object');
foreach($post_types as $post_type) : ?>
<?php
$checked='';
if(@in_array($post_type->name, $instance['post_types'])) $checked='checked';
?>
<input type="checkbox"
value="<?=$post_type->name?>"
name="<?php echo $this->get_field_name( 'post_types' ).'[]'; ?>"
id="<?php echo $this->get_field_id('post_types'); ?>"
<?=$checked?>><?=$post_type->labels->name?><br>
<? endforeach;
}
public function widget( $args, $instance ) {
echo '<div class="page-sidebar"><div class="title"><span>'.$instance['title'].'</span></div>';
function limit_words($string, $word_limit) {
$words=explode(" ",$string);
return implode(" ",array_splice($words,0,$word_limit));
}
function link_words($limited_string){
$words = explode(' ', $limited_string);
array_splice($words, 5, 0, "</a>");
return implode(' ', $words);
}
if(!empty($instance)) :
global $post;
$posts = get_posts(
array(
'numberposts' => $instance['count_of_posts'],
'offset' => 0,
'category' => '',
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => $instance['post_types'],
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish'
)
);
$shows=0;
foreach($posts as $post){ setup_postdata($post);?>
<a href="<?php the_permalink() ?>">
<?php echo link_words(limit_words(get_field('content'), $instance['words_to_display']))."...";
if($shows != ($instance['count_of_posts']-1)) echo '<hr>';
$shows++;
}
wp_reset_postdata();
endif;
echo "</div>";
}
}
print_r
?