-
-
Save mhulse/5861847 to your computer and use it in GitHub Desktop.
<?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' | |
) | |
); |
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.
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?
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.
@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!!!
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.
FYI to future readers, the wp-hackers discussion can be found here: http://wordpress-hackers.1065353.n5.nabble.com/Need-WP-PHP-guru-eyeballs-feedback-on-my-WP-Widget-wrapper-class-td41674.html
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! 👍
For those interested, this is my "traditional" sidebar widget setup:
Questions:
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 parentWP_Widget
constructor?Foo_Widget
's constructor, if I don't set a defaultarray()
value, I get this notice: "Undefined variable: arr in /.../class.foo_widget.php on line 9". Why?