$instance in wordpress widget form method is null - wordpress

I am new to wordpress widget development and learning from tutsplus videos on the go. I am trying to create a simple widget for my website. So far I am following every steps from the video but still I am getting following error and the form is not saving the values.
Warning: extract() expects parameter 1 to be array, null given in C:\Program Files\Ampps\www\test\wp-content\plugins\first\index.php on line 50
This is the from method
public function form($instance){
extract($instance);
?>
<p>
<lable for="<?php echo $this->get_field_id('ap_title_text');?>">Title Text: </lable>
<input type="text" class="widefat" id="<?php echo $this->get_field_id('ap_title_text');?>" name="<?php echo $this->get_field_name('ap_title_text');?>" value="<?php if(isset($ap_title_text)) echo esc_attr($ap_title_text);?>" />
</p>
<?php
So far, I have just created the constructor and registered the widget. There was no error displayed till this step in the video. And every website I googled for this problem showed similar steps. I don't understand why it is showing error on my system.
I am using wordpress 3.5.2 downloaded yesterday and using php5.3.
EDIT ::
Here is the code.
class SidebarWidget extends WP_Widget{
function __construct()
{
$options = array(
'description' => 'Simplest way to start working from any page',
'name' => 'Sidebar Widget'
);
parent::__construct('appSidebarWidget', '', $options);
}
public function form($instance)
{
extract($instance);
?>
<p>
<label for="<?php echo $this->get_field_id('ap_title_text'); ?>" >Title Text: </lable>
<input type="text" class="widefat" id="<?php echo $this->get_field_id('ap_title_text');?>" name="<?php echo $this->get_field_name('ap_title_text');?>" value="<?php if(isset($ap_title_text)) echo esc_attr($ap_title_text);?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('ap_app_name'); ?>" >app Name: </lable>
<input type="text" class="widefat" id="<?php echo $this->get_field_id('ap_app_name');?>" name="<?php echo $this->get_field_name('ap_app_name');?>" value="<?php if(isset($ap_app_name)) echo esc_attr($ap_app_name);?>" />
</p>
<?php
}
public function widget($args, $instance)
{
extract($args);
extract($instance);
$display_gadget = "<iframe src='http://$appURL' width='150px' height='200px' scrolling='auto' frameborder='0' allowtransparency='true'></iframe>";
if(empty($ap_title_text)){$ap_title_text = "Schedule Now";}
echo $before_widget;
echo $before_title.$ap_title_text.$after_title;
echo $display_gadget;
echo $after_widget;
}
}
add_action('widgets_init','appRegisterWidget');
function appRegisterWidget()
{
register_widget('appSidebarWidget');
}
I still haven't been able to get it working. However the actual project for which i was learning this, works as expected. I would still like to know what is wrong with this one. I nearly lost my job because of this.
Also, can anyone guide me to call a custom javascript function from a button on widget being displayed. Basically my purpose is to display a button on the widget rendered by the information on the form. When someone click the button, It should display an overlay with message.

In the following code
add_action('widgets_init','appRegisterWidget');
function appRegisterWidget()
{
register_widget('appSidebarWidget'); <--
}
Change it to
register_widget('SidebarWidget');
I've added a url text box in the widget setup form and it's working, (full modified code given below)
class SidebarWidget extends WP_Widget{
function __construct()
{
$options = array(
'description' => 'Simplest way to start working from any page',
'name' => 'Sidebar Widget'
);
parent::__construct('appSidebarWidget', '', $options);
}
public function form($instance)
{
extract($instance);
?>
<p>
<label for="<?php echo $this->get_field_id('ap_title_text'); ?>" >Title Text: </lable>
<input type="text" class="widefat" id="<?php echo $this->get_field_id('ap_title_text');?>" name="<?php echo $this->get_field_name('ap_title_text');?>" value="<?php if(isset($ap_title_text)) echo esc_attr($ap_title_text);?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('ap_app_name'); ?>" >App Name: </lable>
<input type="text" class="widefat" id="<?php echo $this->get_field_id('ap_app_name');?>" name="<?php echo $this->get_field_name('ap_app_name');?>" value="<?php if(isset($ap_app_name)) echo esc_attr($ap_app_name);?>" />
</p>
<!-- New text box added -->
<p>
<label for="<?php echo $this->get_field_id('ap_app_url'); ?>" >App Url: </lable>
<input type="text" class="widefat" id="<?php echo $this->get_field_id('ap_app_url');?>" name="<?php echo $this->get_field_name('ap_app_url');?>" value="<?php if(isset($ap_app_url)) echo esc_attr($ap_app_url);?>" />
</p>
<?php
}
public function widget($args, $instance)
{
extract($args);
extract($instance);
$display_gadget = "<iframe src='http://" . $ap_app_url . "' width='150px' height='200px' scrolling='auto' frameborder='0' allowtransparency='true'></iframe>";
if(empty($ap_title_text)){$ap_title_text = "Schedule Now";}
echo $before_widget;
echo $before_title.$ap_title_text.$after_title;
echo $display_gadget;
echo $after_widget;
}
}
add_action('widgets_init','appRegisterWidget');
function appRegisterWidget()
{
register_widget('SidebarWidget');
}
Screen shot of working example given below

The code is crashing because the variable $instance is not an array. The extract() function has to receive an array in order to do it's job. That much is clear and very simple.
The problem is that nobody can help you explain why $instance is null unless you provide the code which calls the form() function. There could be a million reasons why $instance is not an array.

The form() function is being called by the WP_Widgets object.

$instance could also always be null in the form method if you have an empty update function. This may not relate to this question, but please check whether that is the issue if you run into a similar issue.

Related

How to correctly implement a basic contact form via wordpress theme?

I've created a wordpress child theme for the theme beat-mix-lite. There I've created a contact-form.php file where I have the template and a mail.php (in an other folder) with an function to send an email.
I've tried sending it without wordpress and it worked locally totally fine. However, with wordpress I always get a 404 error, no matter the action of the form itself.
The functon sendmail() is being called as I tried a vardump() inside it as I submitted the form.
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/wordpress/wp-load.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/wordpress/wp-content/themes/custom-child/functions/mail.php');
?>
<form action="<?php echo sendMail(); ?>" method="post">
<label for="name">
<?php echo getTranslatedText('Name'); ?>: <span>*</span>
</label>
<input type="text" name="name" placeholder="<?php echo getTranslatedText('NameExample'); ?>: ">
<label for="email">
<?php echo getTranslatedText('Email'); ?>: <span>*</span>
</label>
<input type="text" name="email" placeholder="<?php echo getTranslatedText('EmailExample'); ?>: ">
<label for="textarea">
<?php echo getTranslatedText('MessageExample'); ?>: <span>*</span>
</label>
<textarea name="textarea" rows="8" placeholder="<?php echo getTranslatedText('Matter'); ?>: ...">
<?php echo esc_textarea($_POST['textarea']); ?>
</textarea>
<input type="submit" name="submit" value="<?php echo getTranslatedText('Submit'); ?> " type="reset" value="Clear">
</form>
// function to send the mail
<?php
function sendMail() {
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "myemail#domain.com";
$email_subject = "Your email subject line";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['textarea'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$textarea = $_POST['textarea']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
if(strlen($textarea) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Nachricht: ".clean_string($textarea)."\n";
// create email headers
$headers = 'Von: '.$email_from."\r\n".
'Antworten an: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
echo "Thank you for contacting us. We will be in touch with you very soon.";
}
}
I expected to reach the same page with a success message later on, and having the email send to an address of mine (changed it in the code). I feel stupid for even asking, would appreciate help a lot. :)
The parent theme can be downloaded here
I think your action is not working with the wordpress permalink structure.
try this function for the form action :
action="<?php echo get_the_permalink(); ?>"
this will point your form to the current site.
then call your mail function like this:
<?php if(isset($_POST['email']) && isset($_POST['name']) && isset($_POST['MessageExample'])){
sendMail();
} ?>

Facebook login Oauth issue: “URL Blocked"

My website is: https://www.countrygag.com
The link into my Facebook Login button is: https://www.countrygag.com/wp-login.php?action=wordpress_social_authenticate&mode=login&provider=Facebook&redirect_to=https%3A%2F%2Fwww.countrygag.com%2F
The key and secret key is correct on my website. I have added all possible variations on the Valid Oauth URL but still no success. Any idea what I am doing wrong?
The link is probably incorrect, but how can I find the correct link? Thanks!
I got the right link on the Wordpress plugin instrucitons. Thanks all!
You can use this as a plugin or can paste the entire code in functions.php
<?php
require_once("inc/facebookoauth.php");
class Facebook_Login_Widget extends WP_Widget
{
public function __construct()
{
parent::__construct("facebook_login_widget", "Facebook Login", array("description" => __("Display a Facebook Login Button")));
}
public function form( $instance )
{
// Check values
if($instance)
{
$title = esc_attr($instance['title']);
$app_key = $instance['app_key'];
$app_secret = $instance['app_secret'];
}
else
{
$title = '';
$app_key = '';
$app_secret = '';
}
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title', 'facebook_login_widget'); ?></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('app_key'); ?>"><?php _e('App ID:', 'facebook_login_widget'); ?></label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id('app_key'); ?>" name="<?php echo $this->get_field_name('app_key'); ?>" value="<?php echo $app_key; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('app_secret'); ?>"><?php _e('App Secret:', 'facebook_login_widget'); ?></label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id('app_secret'); ?>" name="<?php echo $this->get_field_name('app_secret'); ?>" value="<?php echo $app_secret; ?>" />
</p>
<?php
}
public function update( $new_instance, $old_instance )
{
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['app_key'] = strip_tags($new_instance['app_key']);
$instance['app_secret'] = strip_tags($new_instance['app_secret']);
update_option("facebook_app_id", $new_instance['app_key']);
update_option("facebook_app_secret", $new_instance['app_secret']);
return $instance;
}
public function widget( $args, $instance )
{
extract($args);
$title = apply_filters('widget_title', $instance['title']);
echo $before_widget;
if($title)
{
echo $before_title . $title . $after_title ;
}
if(is_user_logged_in())
{
?>
<input type="button" value="Logout" />
<?php
}
else
{
?>
<input type="button" value="Login Using Facebook" />
<?php
}
echo $after_widget;
}
}
register_widget("Facebook_Login_Widget");

Wordpress how to set default values for widgets?

Im trying to make my own widget. But i found out that this is not so easy. I'm trying to set default values but i have no idea how to do this.
Yes, i have Googled a lot. I'm almost trying for 1 week to get my own options page and custom widgets, but im not succeeding.
so back to my question, this is my code now:
class Superdeal_Widget extends WP_Widget {
public function __construct()
{
parent::__construct(
'Superdeal-widget',
'Superdeal Widget',
array(
'description' => 'Superdeal widget'
),
array (
'width' => 400,
'height' => 350
)
);
}
public function widget( $args, $instance )
{
// basic output just for this example
echo '<p>'.$instance['titel'].' '.$instance['superdeal'].'</p>';
}
public function form( $instance )
{
// removed the for loop, you can create new instances of the widget instead
?>
<p>
<label for="<?php echo $this->get_field_id('titel'); ?>">Titel</label><br />
<input type="text" name="<?php echo $this->get_field_name('titel'); ?>" id="<?php echo $this->get_field_id('titel'); ?>-title" value="<?php echo $instance['titel']; ?>" class="widefat" />
</p>
<p>
<label for="<?php echo $this->get_field_id('url'); ?>">Url</label><br />
<input type="text" name="<?php echo $this->get_field_name('url'); ?>" id="<?php echo $this->get_field_id('url'); ?>-url" value="<?php echo $instance['url']; ?>" class="widefat" />
</p>
<p>
<label for="<?php echo $this->get_field_id('superdeal'); ?>">Superdeal tekst</label><br />
<input type="text" name="<?php echo $this->get_field_name('superdeal'); ?>" id="<?php echo $this->get_field_id('superdeal'); ?>-title" value="<?php echo $instance['superdeal']; ?>" class="widefat" />
</p>
<?php
}
}
// end class
// init the widget
add_action( 'widgets_init', create_function('', 'return register_widget("Superdeal_Widget");') );
PS: if you know a good tutorial which shows how to make you own options page / declare properties / makeing custom widgets, i would love to hear from you. Because all the tutorials i have followd are old, obscure or way too complicated.
Thank you guys!
This should help you → widget tutorial
Note on this line from form() function:
$instance = wp_parse_args( (array) $instance, array( 'title' => 'DEFAULT_VALUE_HERE' ) );
You can add several instances like:
array( 'title' => 'DEFAULT_VALUE_HERE', 'name' => 'DEFAULT_VALUE_HERE')
Hope this would help you going.

Wordpress widget defaults are saved but the display won't change values

I made this plugin yelp reviews ticker
My problem is in the admin interface when you change the values such as: (anything that involves text)
Widget Title
Speed
Pause
-
They get saved and everything works great, only when those values are changed to "0" they get saved to the db but on the admin interface for the widget the default value shows up.
So I don't have a bug displaying the widget at all, it just won't show "0" when that is set.
Anyone has an idea of how to fix that?
Here is the portion of the code (I've only posted the part that I have problems with)
function form( $instance ) { //<- set default parameters of widget
$title = empty($instance['title']) ? Reviews : $instance['title'];
$speed = empty($instance['speed']) ? 2500 : $instance['speed'];
$pause = empty($instance['pause']) ? 6000 : $instance['pause'];
~~~~~~~~~
?>
<p>
<label for="<?php echo $this->get_field_id('title');?>">Widget Title</label><br />
<input 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('speed');?>">Speed</label><br />
<input id="<?php echo $this->get_field_id('speed');?>" name="<?php echo $this->get_field_name('speed');?>" type="text" value="<?php echo $speed; ?>"/>
</p>
<p>
<label for="<?php echo $this->get_field_id('pause');?>">Pause</label><br />
<input id="<?php echo $this->get_field_id('pause');?>" name="<?php echo $this->get_field_name('pause');?>" type="text" value="<?php echo $pause; ?>"/>
</p>
I think the main problem is within these lines
$title = empty($instance['title']) ? Reviews : $instance['title'];
$speed = empty($instance['speed']) ? 2500 : $instance['speed'];
$pause = empty($instance['pause']) ? 6000 : $instance['pause'];
Thanks!
My problem was this:
'' == NULL = true
0 == NULL = true
false == NULL = true
So:
$speed = empty($instance['speed']) ? 2500 : $instance['speed'];
With a single zero will come back "true"
meaning it will use the "2500" value.
I changed the code to "isset" wich fixed it
if (isset($instance['speed'])) {
$speed = $instance['speed'];
} else {
$speed = "2500"; //default
}
Solved it, thanks

Wordpress plugin form issue

I am new to wordpress plug in development. I have designed a search form however I have no idea where to handle and print the submitted form data.
it is a wedget based plug in and the plugin form section code is here:
function widget( $args, $instance ) {
extract( $args );
$title = apply_filters( 'widget_title', $instance['title'] );
$message = apply_filters( 'widget_content', $instance['content'] );
echo $before_widget;
//if ( $title )
// echo $before_title . $title . $after_title;
echo '<div class="shk_location_form_holder">
<span class="shk_loc_title">'.$title.'
<form mthod="post">
<input type="text" name="shk_inp_search_locations" id="shk_inp_search_locations" /><br>
<div style="height:5px"></div>
<input type="submit" Value="Search Locations" />
</form></div>';
echo $after_widget;
if(isset($_REQUEST['shk_inp_search_locations'])){
add_filter('the_content','handle_content');
}
}
In WP plugins you usually have an empty action="" in a form, and handle it in the same function (by the way, as wordpress procedural code becomes very messy, it's better to write plugins using OOP), because, anyway plugins are loaded before any content is outputted in WP (this is the reason why writing ajax plugins is so easy in wp). So you can have everything structured like this:
function draw_form() {
handle_submit();
?>
<div class="shk_location_form_holder">
<span class="shk_loc_title"><?php echo $title; ?></span>
<form mthod="post" action="">
<input type="text" name="shk_inp_search_locations" id="shk_inp_search_locations" /><br>
<div style="height:5px"></div>
<input type="submit" Value="Search Locations" />
</form>
</div>
<?
}
function handle_submit() {
if(isset($_POST['shk_inp_search_locations']) && $_POST['shk_inp_search_locations'] == 'test') {
echo 'you may want to end your program here, especially if it\'s ajax!';
exit;
}
}

Resources