What's wrong with my plugin?
-
Hi,
I’m creating a widget for my theme, but when I activate it WordPress is telling me that it’s generating 659 characters of unexpected output. I also can’t upload thumbnails when my widget is activated.
This is my code: `<?php
/*
Plugin Name: NightVision Tour Dates
Plugin URI: http://www.elexode.nl
Description: Display the tour dates in the sidebar
Version: 1.0
Author: Julian Quispel
Author URI: http://www.elexode.nl
License: GPL2
*/class nv_tourdates extends WP_Widget {
function __construct() {
parent::__construct(false, $name = __(‘Social Buttons’, ‘nv_textdomain’), array( ‘description’ => __( ‘Display your social media in the sidebar’,’nv_textdomain’ ) ) );
}function form( $instance) {
if( $instance) {
$title = esc_attr($instance[‘title’]);
$range = esc_attr($instance[‘range’]);
} else {
$title = ‘Tour Dates’;
$range = 5;
}
?><p>
<label for=”<?php echo $this->get_field_id(‘title’); ?>”><?php _e(‘Title:’, ‘nv_textdomain’); ?></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 $title; ?>” />
</p><p>
<label for=”<?php echo $this->get_field_id(‘range’); ?>”><?php _e(‘Number of Dates:’, ‘nv_textdomain’); ?></label>
<input class=”widefat” id=”<?php echo $this->get_field_id(‘range’); ?>” name=”<?php echo $this->get_field_name(‘range’); ?>” min=”1″ type=”number” value=”<?php echo $range; ?>” />
</p>
<?php
}function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance[‘title’] = strip_tags($new_instance[‘title’]);
$instance[‘range’] = strip_tags($new_instance[‘range’]);
return $instance;
}function widget($args, $instance) {
echo $args[‘before_widget’];
echo $args[‘before_title’];
echo $instance[‘title’];
echo $args[‘after_title’];
$my_query = new WP_Query(‘posts_per_page=’.$range.’&post_type=tourdates’);
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div class=”side-tour-time”><?php
$date = rwmb_meta(‘nv_fdate’);
$date = str_replace(‘/’, ‘-‘, $date);
$time = strtotime($date);
echo date(‘j F Y’, $time);
?> </div>
<h3 class=”side-tour-title”><?php the_title(); ?></h3>
<?php endwhile; wp_reset_query(); ?>
<div class=”side-tour-more”>
<a href=”<?php echo get_post_type_archive_link( ‘tourdates’ ); ?>”>More dates</a>
</div>
<?php echo $args[‘after_widget’];}
}// register widget
add_action(‘widgets_init’, function(){
register_widget(‘nv_tourdates’);
}
);
?><style type=”text/css”>
input[type=”number”], input[type=”text”] {
margin:5px 0;
}.side-tour-time {
padding:5px;
background:rgba(161,40,145,0.5);
border-radius:3px;
font-weight:bold;
margin:2px 0;
}.side-tour-title {
margin:8px 0;
}.side-tour-more {
display:table;
}.side-tour-more a {
text-decoration:none;
float:left;
border-radius:3px;
background:rgba(161,40,145,0.5);
color:white;
padding:5px;
margin-top:5px;
-o-transition:.5s;
-ms-transition:.5s;
-moz-transition:.5s;
-webkit-transition:.5s;
transition:.5s;
}.side-tour-more a:hover {
background:white;
color:purple;
}
</style>`Hope you can help me 🙂
-
First off, its best practice to use a code pasting tool such as http://pastebin.com/ for large chunks of code
The problem with the plugin is with the style block at the end. shouldn’t be there at all.
It should be in a function hooked into wp_head.
function nvtd_styles(){
?>
<style type=”text/css”>…….
</style>
<?php
}
add_action( ‘wp_head’, ‘nvtd_styles’ );this will load that CSS directly into the head once on every page call, without any unwanted errors.
Let me know if you have any questions.
Cheers
BenThanks man, it worked. I will remember this for other widgets.
Not a problem. Don’t forget to mark this thread as resolved 😉
Cheers
Ben
The topic ‘What's wrong with my plugin?’ is closed to new replies.