I'm trying to make my first widget/plugin, the plugin works well, but, after the activation it sends me this error:
"The plugin has generated 2 unexpected output characters during activation. If you notice “headers already sent” messages, issues with syndication feeds, or other problems, try disabling or removing this plugin."
Then, when I enter to the site it sends me this other error:
"Content encoding error
The page you are trying to view cannot be displayed because it uses an invalid or unsupported compression format."
After that, if I refresh the page, it let my enter normally, but, the same issue is repeated every time I enter to a different page.
I don't understand what's happen, so I will appreciate your help to solve this.
This is the code:
<?php
/*
Plugin Name: Mx Energy Partners - Post Relacionados
Plugin URI:
Description: Añade Post Relacionados al sitio Mx Energy Partners
Version: 1.0.0
Author: Arturo Valverde
Author URI:
Text Domain: mxtechnologypartners
*/
?>
<?php
if (!defined('ABSPATH')) die();
class Mxnrgprtnrs_related_posts extends WP_Widget
{
public function __construct()
{
$widget_ops = array(
'classname' => 'mxnrgprtnrs_posts_relacionados',
'description' => 'Añade Post Relacionados al sitio Mx Energy Partners',
);
parent::__construct(
'mxnrgprtnrs_posts_relacionados',
'MX Energy Partners - Posts Relacionados',
$widget_ops
);
}
public function widget($args, $instance)
{
if (post_type_exists('post')) : ?>
<div >
<h4>Artículos Relacionados:</h4>
<ul >
<?php
$tags = get_the_tags();
if ($tags) :
$tag_ids = array();
foreach ($tags as $tag) {
$tag_ids[] = $tag->term_id;
}
$qryargs = array(
'post_type' => 'post',
'tag__in' => $tag_ids,
'post__not_in' => array(get_the_ID()),
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => 4,
'post_status' => 'publish',
);
$my_query = new WP_Query($qryargs);
if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post();
?>
<li >
<div >
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('thumbnail'); ?>
</a>
</div>
<div >
<div >
<a href="<?php the_permalink(); ?>">
<h4><?php the_title(); ?></h4>
</a>
<span>
Autor:
<a href="<?php echo get_author_posts_url(get_the_author_meta('ID')) ?>">
<?php echo get_the_author_meta('display_name'); ?>
</a>
</span>
<span>
<?php the_time('l d, F, Y'); ?>
</span>
</div>
</div>
</li>
<?php
endwhile;
endif;
else :
wp_reset_query();
endif;
wp_reset_query();
?>
</ul>
</div>
<?php endif; ?>
<?php
}
public function form($instance)
{
// outputs the options form on admin
}
public function update($new_instance, $old_instance)
{
// processes widget options to be saved
}
}
add_action('widgets_init', function () {
register_widget('mxnrgprtnrs_related_posts');
});
?>
CodePudding user response:
Remove the closing and re-opening PHP tags between the plugin information and the class declaration.:
?>
<?php
That code is essentially emitted white space which is where your extra characters are coming from.