Skip to content

Instantly share code, notes, and snippets.

@mhulse
Created June 25, 2013 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhulse/5861847 to your computer and use it in GitHub Desktop.
Save mhulse/5861847 to your computer and use it in GitHub Desktop.
A more robust version of this code, and code from my original question, can be found here: https://gist.github.com/mhulse/5860561
<?php
class Foo_Widget extends WP_Widget
{
public function __construct($arr = array()) { # If I don't set a default array() value, I get this notice: "Undefined variable: arr in /.../class.foo_widget.php on line 9"
$args = wp_parse_args(
$arr,
array(
'id_base' => '',
'name' => '',
)
);
extract($args, EXTR_SKIP);
$widget_ops = array('classname' => __CLASS__, 'description' => __('Description goes here.'));
$control_ops = array('width' => 400, 'height' => 350);
# THIS WORKS:
//parent::__construct('eeeeeeeeee', 'wtf son?', $widget_ops, $control_ops);
# `$id_base` and `$name` are not getting passed:
parent::__construct($id_base, $name, $widget_ops, $control_ops); # Why doesn't this work? Is it a timing thing?
add_action(
'widgets_init',
function() {
register_widget(__CLASS__);
}
);
}
public function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['text'] = $new_instance['text']; // Unfiltered HTML.
return $instance;
}
public function form($instance) {
$instance = wp_parse_args((array) $instance, array('text' => '',));
$text = esc_textarea($instance['text']);
?>
<textarea rows="16" cols="20" name="<?=$this->get_field_name('text')?>" id="<?=$this->get_field_id('text')?>" class="widefat"><?=$text?></textarea>
<?php
}
public function widget($args, $instance) {
extract($args);
$text = apply_filters(
'widget_text',
(empty($instance['text']) ? '' : $instance['text']),
$instance
);
echo $text;
}
}
<?php
include_once('class.foo_widget.php');
$foo_test_widget = new Foo_Widget(
array(
'id_base' => 'foo_test_widget',
'name' => 'Foo Test Widget'
)
);
@mhulse
Copy link
Author

mhulse commented Jun 25, 2013

Questions:

  1. In Foo_Widget's constructor, this works: parent::__construct('eeeeeeeeee', 'wtf son?', $widget_ops, $control_ops);, but this does not: parent::__construct($id_base, $name, $widget_ops, $control_ops);. Why can't I pass $id_base and $name as string variables to the parent WP_Widget constructor?
  2. In Foo_Widget's constructor, if I don't set a default array() value, I get this notice: "Undefined variable: arr in /.../class.foo_widget.php on line 9". Why?

@JDGrimes
Copy link

You probably also received this when you got the second error (or have warnings turned off): PHP Warning: Missing argument 1 for Foo_Widget::__construct(). You should only receive that if you didn't pass the parameter.

@dyordan1
Copy link

Something might be wrong with your PHP configuration. I just literally copied over everything you have (+changed $arr = array() to just $arr) and gave it a go and I didn't see any errors. Trust me, I would've seen something if there were errors, I have PHP setup to scream at me for every single warning/deprecated function/non-crucial error/etc. Can you check what version of PHP you have installed?

@JDGrimes
Copy link

The issue here is that you won't be instantiating the class like this. You'll be registering the widget with register_widget(), which will instantiate the class without passing any arguments. That will result in the error you are getting. In other words, you won't be able to use this class and register your widgets with register_widget(). I've been looking at the widgets API, but I'm still not sure how you would register a widget without calling that function.

@mhulse
Copy link
Author

mhulse commented Jun 25, 2013

@JDGrimes and @dyordan1, thanks so much for your replies, I really appreciate it! 😄

Something might be wrong with your PHP configuration. I just literally copied over everything you have (+changed $arr = array() to just $arr) and gave it a go and I didn't see any errors. Trust me, I would've seen something if there were errors, I have PHP setup to scream at me for every single warning/deprecated function/non-crucial error/etc. Can you check what version of PHP you have installed?

For my php.ini, here's my error logging details:

$ grep -i error php.ini
error_reporting = E_ALL & ~E_DEPRECATED
display_errors = Off
display_startup_errors = Off
log_errors = On
log_errors_max_len = 1024
ignore_repeated_errors = Off
track_errors = Off
html_errors = Off
mssql.min_error_severity = 10

... in my wp-config.php:

# WordPress debugging mode:
//define('WP_DEBUG', TRUE);
# http://codex.wordpress.org/Editing_wp-config.php#Increasing_memory_allocated_to_PHP
define('WP_MEMORY_LIMIT', '64M');

//--------------------------------------------------------------------

/**
 * This will log all errors notices and warnings to a file called
 * `debug.log` in `/wp-content`. If Apache does not have write permission,
 * you may need to create the file first and set the appropriate `666`
 * permissions.
 *
 * @see http://wordpress.stackexchange.com/a/69552/32387
 */

define('WP_DEBUG', TRUE);
define('WP_DEBUG_LOG', TRUE);
define('WP_DEBUG_DISPLAY', FALSE);
@ini_set('display_errors', 0);

... and finally, when checking phpinfo() from the root directory of my site:

Directive Local Value Master Value
display_errors Off Off
display_startup_errors Off Off
error_append_string no value no value
error_log no value no value
error_prepend_string no value no value
error_reporting 22527 22527
html_errors Off Off
ignore_repeated_errors Off Off
log_errors On On
log_errors_max_len 1024 1024
track_errors Off Off
xmlrpc_error_number 0 0
xmlrpc_errors Off Off

Anyway, not sure if that's of any help.

You probably also received this when you got the second error (or have warnings turned off): PHP Warning: Missing argument 1 for Foo_Widget::__construct(). You should only receive that if you didn't pass the parameter.

Yes! At one point I do remember seeing that warning. Thing is, I've always passed a parameter, so that's why I am a little confused.

The issue here is that you won't be instantiating the class like this. You'll be registering the widget with register_widget(), which will instantiate the class without passing any arguments. That will result in the error you are getting. In other words, you won't be able to use this class and register your widgets with register_widget(). I've been looking at the widgets API, but I'm still not sure how you would register a widget without calling that function.

Ahhh! I see. So, to clarify, what you're saying is that it's probably not possible for me to create new "custom" widgets like so:

$foo_test_widget = new Foo_Widget(array('id_base' => 'foo_test_widget', 'name' => 'Foo Test Widget'));

That would explain why I'm not able to pass variables to the parent class constructor.

So, it looks like I should just stick to the more traditional way of creating custom widgets?

Thanks again for the help guys, I really appreciate it!!!

@JDGrimes
Copy link

So, it looks like I should just stick to the more traditional way of creating custom widgets?

Yes, I think unfortunately you'll have to stick with the traditional methods.

@mhulse
Copy link
Author

mhulse commented Jun 25, 2013

@mhulse
Copy link
Author

mhulse commented Jun 25, 2013

Yes, I think unfortunately you'll have to stick with the traditional methods.

@JDGrimes sounds good. I don't mind the traditional methods ... I just thought it might be cool to setup something similar for widgets like WPAlchemy script does for meta boxes.

Honestly, more than anything, I'm glad to know that the results I'm getting are not just a limitation of my PHP knowledge. 😄

Ok, moving on now. Thank you! 👍

@mhulse
Copy link
Author

mhulse commented Jun 26, 2013

For those interested, this is my "traditional" sidebar widget setup:

https://gist.github.com/mhulse/5864117

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment