I am using wordpress 4.3.1 and I want to develop a simple example of widget. So I started with creating a plugin and then my widget class that extends WP_Widget. Moreover, I tried to add new table in database when the plugin is activated. Until now everything is ok and the widget shown in the widget admin zone and the table is added in my database. But the uninstall function don't drop the table from my database.
Furthermore, I added a same mail function in my widget classe to save the user mail in the database, but an error shown :
Plugin could not be activated because it triggered a fatal error. Parse error: syntax error, unexpected '$row' (T_VARIABLE) in ...
This is my enter file code:
class Wis_Plugin extends WP_Widget{
public function __construct(){
include_once plugin_dir_path( __FILE__ ).'/newsletter.php';
include_once plugin_dir_path( __FILE__ ).'/newsletterwidget.php';
register_activation_hook(__FILE__, array('Wise_Newsletter_Widget', 'install'));
//add_action('wp_loaded', array('Wise_Newsletter_Widget', 'save_email'));
new Wise_Newsletter();
//define('WP_DEBUG', true);
add_action('admin_menu', array('Wise_Newsletter', 'add_admin_menu'), 20);
register_uninstall_hook(__FILE__,array('Wis_Plugin','uninstall') );
}
public static function uninstall(){
global $wpdb;
$wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}another_wise_newsletter_email;");
}
}
new Wis_Plugin();
This is the enter file plugin, I used Wis_Plugin() to execute this class constructor, this is a little bit weird to don't be executed automatically.
My newsletter.php file is the following:
class Wise_Newsletter{
public function __construct()
{
add_action('widgets_init', function(){register_widget('Wise_Newsletter_Widget');});
register_activation_hook(__FILE__, array('Wise_Newsletter_Widget', 'install'));
}
My widget class looks like this:
class Wise_Newsletter_Widget extends WP_Widget{
public function __construct()
{
parent::__construct('wise_newsletter', 'Newsletter', array('description' => 'newletter inscription fomr.'));
}
public function widget($args, $instance)
{
echo $args['before_widget'];
echo $args['before_title'];
echo apply_filters('widget_title', $instance['title']);
echo $args['after_title'];
?>
<form action="" method="post">
<p>
<label for="wise_newsletter_email">Your email :</label>
<input id="wise_newsletter_email" name="wise_newsletter_email" type="email"/>
</p>
<input type="submit"/>
</form>
<?php
echo $args['after_widget'];
}
public function form($instance)
{
$title = isset($instance['title']) ? $instance['title'] : '';
?>
<p>
<label for="<?php echo $this->get_field_name( '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 $title; ?>" />
</p>
<?php
}
public static function install()
{
global $wpdb;
$wpdb->query("CREATE TABLE IF NOT EXISTS {$wpdb->prefix}another_wise_newsletter_email (id INT PRIMARY KEY, email VARCHAR(255) NOT NULL);");
}
public function save_email()
{
if (isset($_POST['wise_newsletter_email']) && !empty($_POST['wise_newsletter_email'])) {
global $wpdb;
$email = $_POST['wise_newsletter_email'];
$row = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}another_wise_newsletter_email WHERE email = '$email'");
if (is_null($row)) {
$wpdb->insert("{$wpdb->prefix}another_wise_newsletter_email", array('email' => $email));
}
}
}
}
So to sum up my problems:
- Why wordpress did not execute automatically my plugin enter file (the folder and the file contains the same name).
- why the uninstall function does not work.
- And what is the probleme with unixpected $row error.
Thank you
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire