I use the following code in my plugin [[LINK href="http://mapsmarker.com"]]mapsmarker.com[[/LINK]]:
[...]
class Leafletmapsmarker
{
function leafletmapsmarker() {
[...]
<strong>add_action('widgets_init', create_function('', 'return register_widget("lmm_recent_marker_widget");'));</strong>
}
[...]
} //info: end class Leafletmapsmarker
$run_leafletmapsmarker = new Leafletmapsmarker();
class lmm_recent_marker_widget extends WP_Widget {
public function __construct() {
$widget_options = array(
'classname' => 'lmm_recent_marker_widget',
'description' => __('Widget to show the most recent Leaflet Maps Marker entries', 'lmm'));
$control_options = array();
$this->WP_Widget('lmm_recent_marker_widget', __('Leaflet Maps Marker - recent markers', 'lmm'), $widget_options, $control_options);
}
[...]
}//info: END class lmm_recent_marker_widget extends WP_Widget
unset($run_leafletmapsmarker);
Problem is, that if I run this code on WordPress 3.0/3.1 (didnt check 3.2/3.3) I get an 500 Internal Server Error - but on 3.4 it works.
If I disable the line in bold (add_action(...)), the plugin works (but without the widget).
As I am not that good in object orientated coding, I wonder if it has to do something with class referencing which might not me right with my code...
I found [[LINK href="http://stackoverflow.com/questions/5247302/php-namespace-5-3-and-wordpress-widget/5247436#5247436"]]this thread[[/LINK]] which could have to do with my problem but I dont really know if that´s right.
Any help is appreciated!
AdamGold answers:
Well in continue of Daniel's answer, try:
function register_my_widget() {
register_widget("lmm_recent_marker_widget");
}
add_action('widgets_init', 'register_my_widget');
Robert Seyfriedsberger comments:
better, but not quite there :-/ I added the following:
[...]
class Leafletmapsmarker
{
function leafletmapsmarker() {
[...]
add_action('widgets_init', array(&$this,'lmm_register_my_widget'));
}
function lmm_register_my_widget() {
register_widget("lmm_recent_marker_widget");
}
[...]
} //info: end class Leafletmapsmarker
$run_leafletmapsmarker = new Leafletmapsmarker();
class lmm_recent_marker_widget extends WP_Widget {
public function __construct() {
$widget_options = array(
'classname' => 'lmm_recent_marker_widget',
'description' => __('Widget to show the most recent Leaflet Maps Marker entries', 'lmm'));
$control_options = array();
$this->WP_Widget('lmm_recent_marker_widget', __('Leaflet Maps Marker - recent markers', 'lmm'), $widget_options, $control_options);
}
[...]
}//info: END class lmm_recent_marker_widget extends WP_Widget
unset($run_leafletmapsmarker);
now I get the error
Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'Leafletmapsmarker::register_my_widget' was given in /xxx/html/mapsmarker/wp-30/wp-includes/plugin.php on line 395
I still dont understand how to reference the second class <strong>lmm_recent_marker_widget</strong> which is outside the class <strong>Leafletmapsmarker</strong> :-/
AdamGold comments:
Something is wrong here, in the code you call "lmm_register_my_widget" in the add_action hook, but in the error it says you called register_my_widget. Can you re-check your code please?
AdamGold comments:
There are some things wrong with your code:
Replace:
$this->WP_Widget('lmm_recent_marker_widget'
with:
parent::__construct( __CLASS__
Replace:
function leafletmapsmarker() {
with:
function __construct
Robert Seyfriedsberger comments:
that solved the problem - thanks!
Daniel Yoen answers:
try
add_action('widgets_init', create_function('', 'return register_widget("lmm_recent_marker_widget");'));
to
add_action('widgets_init', create_function('', 'return register_widget("lmm_recent_marker_widget")'));
Robert Seyfriedsberger comments:
works partly, but I still get the following error:
Parse error: syntax error, unexpected '}' in /xxx/html/mapsmarker/wp-30/wp-content/plugins/2.4/leaflet-maps-marker.php(69) : runtime-created function on line 1
Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, '' was given in /xxx/html/mapsmarker/wp-30/wp-includes/plugin.php on line 395
Robert Seyfriedsberger comments:
No, does not work - I restarted and now get the following error:
Parse error: syntax error, unexpected '}' in /xxx/html/mapsmarker/wp-30/wp-content/plugins/2.4/leaflet-maps-marker.php(69) : runtime-created function on line 1
Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, '' was given in /xxx/html/mapsmarker/wp-30/wp-includes/plugin.php on line 395
Warning: Cannot modify header information - headers already sent by (output started at /xxx/html/mapsmarker/wp-30/wp-content/plugins/2.4/leaflet-maps-marker.php(69) : runtime-created function:1) in /xxx/html/mapsmarker/wp-30/wp-includes/pluggable.php on line 890
any other tipps?
Daniel Yoen comments:
It seems your plug-in is trying to hook a function onto the admin_init, but the the function name they've given is not valid. Usually because the function doesn't exist.
Luis Abarca answers:
Yep,
add_action( 'widgets_init', create_function( '', 'return register_widget("lmm_recent_marker_widget");' ) );
without the return
add_action( 'widgets_init', create_function( '', 'register_widget("lmm_recent_marker_widget");' ) );
or
add_action( 'widgets_init', function(){
return register_widget( 'lmm_recent_marker_widget' );
});
What PHP version are you running ?
Robert Seyfriedsberger comments:
doesnt work without the return.
if I add your second solution, Dreamweaver says "Syntax Error" as it cant be added here :-/
it has to work on PHP >5.2 as it is used by more than 12500 users...