Ondersteuning » Algemeen WordPress » WP.Widget fout in widgets.php __construct

  • Hallo,

    Ik heb een kritieke fout op mijn website.
    Ik heb ondertussen al gevonden dat mijn themes een verouderde versie is, PHP4.0 aangemaakt met Artisteer.
    Door nu PHP8.0 te activeren krijg ik onderstaande fout:

    Fatal error: Uncaught ArgumentCountError: Too few arguments to function WP_Widget::__construct(), 0 passed in /customers/1/2/c/site/httpd.www/wp-includes/class-wp-widget-factory.php on line 61 
    and at least 2 expected in /customers/1/2/c/site/httpd.www/wp-includes/class-wp-widget.php:162 
    
    Stack trace:
     #0 /customers/1/2/c/site/httpd.www/wp-includes/class-wp-widget-factory.php(61): WP_Widget->__construct()
    #1 /customers/1/2/c/site/httpd.www/wp-includes/widgets.php(115): WP_Widget_Factory->register('VMenuWidget')
     #2 /customers/1/2/c/site/httpd.www/wp-content/themes/HF_test1/library/widgets.php(251): register_widget('VMenuWidget')
     #3 /customers/1/2/c/site/httpd.www/wp-includes/class-wp-hook.php(307): artWidgetsInit('') 
    #4 /customers/1/2/c/site/httpd.www/wp-includes/class-wp-hook.php(331): WP_Hook->apply_filters(NULL, Array) 
    #5 /customers/1/2/c/site/httpd.www/wp-includes/plugin.php(476): WP_Hook->do_action(Array) 
    #6 /customers/1/2/c/site/httpd.www/wp-includes/widgets.php(1854): do_action('widgets_init') 
    #7 /customers/1/2/c/site/httpd.www/wp-includes/class-wp-hook.php(307): wp_widgets_init('') 
    #8 /customers/1/2/c/site/httpd.www/wp-includes/class-wp-hook.php(331): WP_Hook->apply_filters(NULL, Array)
     #9 /customers/1/2/c/site/httpd.www/wp-includes/plugin.php(476): WP_Hook->do_action(Array) 
    #10 /customers/1/2/c/site/httpd.www/wp-settings.php(598): do_action('init') 
    #11 /customers/1/2/c/site/httpd.www/wp-config.php(80): require_once('/customers/1/2/...') 
    #12 /customers/1/2/c/site/httpd.www/wp-load.php(50): require_once('/customers/1/2/...') 
    #13 /customers/1/2/c/site/httpd.www/wp-admin/admin.php(34): require_once('/customers/1/2/...') 
    #14 /customers/1/2/c/site/httpd.www/wp-admin/index.php(10): require_once('/customers/1/2/...') 
    #15 {main} thrown in /customers/1/2/c/site/httpd.www/wp-includes/class-wp-widget.php on line 162
    

    Ziehier mijn code van widget.php onder mijn themes:

    <?php
    // widget extra options
    global $theme_widgets_style;
    $theme_widgets_style = array(
    	'default' => __('sidebar default', THEME_NS),
    	'block'   => __('block', THEME_NS),
    	'post'    => __('post', THEME_NS),
    	'simple'  => __('simple text', THEME_NS)
    );
    
    function theme_get_widget_style($id, $style = null) {
    
    	$result = theme_get_widget_meta_option($id, 'theme_widget_styles');
    	global $theme_widgets_style;
    	if (!in_array($result, array_keys($theme_widgets_style))) {
    		$result = 'default';
    	}
    	if ($style != null) {
    		if (!in_array($style, array('block', 'post', 'simple'))) {
    			$style = 'block';
    		}
    		if ($result == 'default') {
    			$result = $style;
    		}
    	}
    	return $result;
    }
    
    function theme_set_widget_style($id, $style) {
    	global $theme_widgets_style;
    	if (!in_array($style, array_keys($theme_widgets_style))) {
    		$style = 'default';
    	}
    	theme_set_widget_meta_option($id, 'theme_widget_styles', $style);
    }
    
    function theme_widget_expand_control($id) {
    	global $wp_registered_widget_controls;
    	$controls = &$wp_registered_widget_controls[$id];
    	if (!is_array($controls['params'])) {
    		$controls['params'] = array($controls['params']);
    	}
    	$controls['params'][] = $id;
    	if (isset($controls['callback'])) {
    		$controls['callback_redirect'] = $controls['callback'];
    	}
    	$controls['callback'] = 'theme_widget_extra_control';
    }
    
    function theme_update_widget_additional($instance) {
    	global $theme_widget_meta_options;
    	foreach ($theme_widget_meta_options as $value) {
    		$id = theme_get_array_value($value, 'id');
    		$val = stripslashes(theme_get_array_value($_POST, $id));
    		$type = theme_get_array_value($value, 'type');
    		$options = theme_get_array_value($value, 'options');
    		switch ($type) {
    			case 'checkbox':
    				$val = ($val ? 1 : 0);
    				break;
    			case 'numeric':
    				$val = (int) $val;
    				break;
    			case 'select':
    				if (!in_array($val, array_keys($options))) {
    					$val = reset(array_keys($options));
    				}
    				break;
    		}
    		$instance[$id] = $val;
    	}
    	return $instance;
    }
    function theme_widget_process_control() {
    	global $wp_registered_widget_controls;
    	if ('post' == strtolower($_SERVER['REQUEST_METHOD']) && isset($_POST['widget-id'])) {
    		theme_widget_expand_control($_POST['widget-id']);
    		return;
    	}
    	foreach ($wp_registered_widget_controls as $id => $widget) {
    		theme_widget_expand_control($id);
    	}
    }
    
    function theme_widget_extra_control() {
    	global $wp_registered_widget_controls, $theme_widgets_style, $theme_widget_meta_options;
    	$_theme_widget_meta_options = $theme_widget_meta_options;
    	$params = func_get_args();
    	$widget_id = $params[count($params) - 1]; // see theme_widget_expand_control func
    	$widget_controls = theme_get_array_value($wp_registered_widget_controls, $widget_id, array());
    	if (isset($widget_controls['callback_redirect'])) {
    		$callback = $widget_controls['callback_redirect'];
    		if (is_callable($callback)) {
    			call_user_func_array($callback, $params);
    		}
    	}
    	if (!preg_match('/^(.*[^-])-([0-9]+)$/', $widget_id, $matches) || !isset($matches[1]) || !isset($matches[2])) {
    		return false;
    	}
    	$id = $matches[1] . '-' . $params[0]['number'];
    	
    	?>
    	<h3 style="margin-bottom:3px;"><?php _e('Theme Options', THEME_NS); ?></h3>
    	<?php
    	theme_print_meta_box($id, $_theme_widget_meta_options);
    }
    
    // widgets
    
    class VMenuWidget extends WP_Widget {
    
    	function VMenuWidget() {
    		$widget_ops = array('classname' => 'vmenu', 'description' => __('Use this widget to add one of your custom menus as a widget.', THEME_NS));
    		parent::WP_Widget(false, __('Vertical Menu', THEME_NS), $widget_ops);
    	}
    
    	function widget($args, $instance) {
    		extract($args);
    		$title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base);
    		echo $before_widget;
    		echo $before_title . $title . $after_title;
    		echo theme_get_menu(array(
    			'source' => $instance['source'],
    			'depth'  => theme_get_option('theme_vmenu_depth'),
    			'menu'   => wp_get_nav_menu_object($instance['nav_menu']),
    			'class'  => ''
    		));
    		echo $after_widget;
    	}
    
    	function update($new_instance, $old_instance) {
    		$instance['title'] = strip_tags($new_instance['title']);
    		$instance['source'] = $new_instance['source'];
    		$instance['nav_menu'] = (int) $new_instance['nav_menu'];
    		return $instance;
    	}
    
    	function form($instance) {
    		//Defaults
    		$instance = wp_parse_args((array) $instance, array('title' => '', 'source' => 'Pages', 'nav_menu' => ''));
    		$title = esc_attr($instance['title']);
    		$source = $instance['source'];
    		$nav_menu = $instance['nav_menu'];
    
    		// Get menus
    		$menus = get_terms('nav_menu', array('hide_empty' => false));
    		$sources = array('Pages', 'Categories', 'Custom Menu');
    		?>
    		<p>
    			<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', THEME_NS) ?></label>
    			<input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $title; ?>" />
    		</p>
    		<p>
    		<label for="<?php echo $this->get_field_id('source'); ?>"><?php echo __('Source', THEME_NS) . ':'; ?></label>
    		<select class="widefat" id="<?php echo $this->get_field_id('source'); ?>" name="<?php echo $this->get_field_name('source'); ?>" onchange="var s = jQuery('.p-<?php echo $this->get_field_id('nav_menu'); ?>'); if (this.value == 'Custom Menu') s.show(); else s.hide();">
    		<?php
    		foreach ($sources as $s) {
    			$selected = ($source == $s ? ' selected="selected"' : '');
    			echo '<option' . $selected . ' value="' . $s . '">' . __($s, THEME_NS) . '</option>';
    		}
    		?>
    		</select>
    		</p>
    		<p class="p-<?php echo $this->get_field_id('nav_menu'); ?>" <?php if ($source !== 'Custom Menu') echo ' style="display:none"' ?>>
    		<?php
    		// If no menus exists, direct the user to go and create some.
    		if (!$menus) {
    			printf(__('No menus have been created yet. <a href="%s">Create some</a>.', THEME_NS), admin_url('nav-menus.php'));
    		} else {
    			?>
    			<label for="<?php echo $this->get_field_id('nav_menu'); ?>"><?php _e('Select Menu:', THEME_NS); ?></label><br />
    			<select class="widefat" id="<?php echo $this->get_field_id('nav_menu'); ?>" name="<?php echo $this->get_field_name('nav_menu'); ?>">
    			<?php
    			foreach ($menus as $menu) {
    				$selected = $nav_menu == $menu->term_id ? ' selected="selected"' : '';
    				echo '<option' . $selected . ' value="' . $menu->term_id . '">' . $menu->name . '</option>';
    			}
    			?>
    			</select>
    			<?php
    		}
    		?>
    		</p>
    		<?php
    	}
    
    }
    
    class LoginWidget extends WP_Widget {
    
    	function LoginWidget() {
    		$widget_ops = array('classname' => 'login', 'description' => __('Login form', THEME_NS));
    		$this->WP_Widget(false, __('Login', THEME_NS), $widget_ops);
    	}
    
    	function widget($args, $instance) {
    		global $user_ID, $user_identity, $user_level, $user_email, $user_login;
    		extract($args);
    		echo $before_widget;
    		echo $before_title;
    		if ($user_ID):
    			echo $user_identity;
    			echo $after_title;
    			?>
    			<ul>
    			<li><a href="<?php bloginfo('wpurl') ?>/wp-admin/"><?php _e('Dashboard', THEME_NS); ?></a></li>
    			<?php if ($user_level >= 1): ?>
    				<li><a href="<?php bloginfo('wpurl') ?>/wp-admin/post-new.php"><?php _e('Publish', THEME_NS); ?></a></li>
    				<li><a href="<?php bloginfo('wpurl') ?>/wp-admin/edit-comments.php"><?php _e('Comments', THEME_NS); ?></a></li>
    			<?php endif; ?>
    			<li><a href="<?php echo wp_logout_url() ?>&redirect_to=<?php echo urlencode(theme_get_current_url()); ?>"><?php _e("Log out", THEME_NS); ?></a></li>
    			</ul>
    		<?php
    		else:
    			_e('Log In', THEME_NS);
    			echo $after_title;
    			?>
    			<form action="<?php bloginfo('wpurl') ?>/wp-login.php" method="post" name="login" id="form-login">
    				<fieldset class="input" style="border: 0 none;">
    					<p id="form-login-username">
    						<label for="log"><?php _e('Username', THEME_NS) ?></label>
    						<br>
    						<input type="text" name="log" id="log" value="<?php echo esc_attr(stripslashes($user_login), 1) ?>" size="20" />
    					</p>
    					<p id="form-login-password">
    						<label for="pwd"><?php _e("Password", THEME_NS); ?></label>
    						<br>
    						<input type="password" name="pwd" id="pwd" size="20" /><br />
    					</p>
    					<p id="form-login-remember">
    						<label for="rememberme"><?php _e('Remember Me', THEME_NS); ?></label>
    						<input name="rememberme" id="rememberme" type="checkbox" checked="checked" value="forever" />
    					</p>
    					<input class="hf-button" type="submit" name="submit" value="<?php echo esc_attr(__('Log In', THEME_NS)); ?>" />
    				</fieldset>
    				<input type="hidden" name="redirect_to" value="<?php echo theme_get_current_url(); ?>"/>
    			</form>
    			<ul>
    				<?php if (get_option('users_can_register')) { ?>
    				<li><a href="<?php bloginfo('wpurl') ?>/wp-register.php"><?php _e("Register", THEME_NS); ?></a></li>
    				<?php } ?>
    				<li><a href="<?php bloginfo('wpurl') ?>/wp-login.php?action=lostpassword"><?php _e("Lost your password?", THEME_NS); ?></a></li>
    			</ul>
    		<?php endif;
    		echo $after_widget;
    	}
    }
    
    // init widgets
    function artWidgetsInit() {
    	register_widget('VMenuWidget');
    	register_widget('LoginWidget');
    }
    
    add_action('widgets_init', 'artWidgetsInit');

    Ik heb dit ondertussen al gewijzigd:

    function VMenuWidget() {
    parent::__construct( false, ‘ VMenuWidget’ );

    Maar nu geeft hij me de fout op lijn 327:

    // init widgets
    function WidgetsInit() {
        register_widget('VMenuWidget');
        register_widget('LoginWidget');
    }

    Wie kan me helpen?
    De site werkt, het is enkel mijn menu die niet meer tevoorschijn komt.
    Binnenkort zal mijn host ook PHP 7.4 afsluiten, dus ik heb een oplossing nodig.

    Alvast bedankt!

9 reacties aan het bekijken - 1 tot 9 (van in totaal 9)
  • Thread starter heidienfern

    (@heidienfern)

    Nog een aanvulling:

    De pagina’s worden niet ingevuld in de menu.

    <nav class="hf-nav desktop-nav">
        <div class="hf-nav-inner">
        
    <a href="#" class="hf-menu-btn"><span></span><span></span><span></span></a><ul class="hf-hmenu menu-151">
    	<li class="menu-item-"><a href="#"></a>
    	</li>
    	<li class="menu-item-"><a href="#"></a>
    	</li>
    	<li class="menu-item-"><a href="#"></a>
    	</li>

    Enig idee?

    Guido

    (@guido07111975)

    Hoi,

    Wijzig dit eens:
    parent::WP_Widget(false, __('Vertical Menu', THEME_NS), $widget_ops);

    In dit:
    parent::__construct('v_menu_widget', __('Vertical Menu', THEME_NS), $widget_ops);

    Volgens mij werkt false niet, dat zou de naam vd widget moeten zijn, bv v_menu_widget.

    Guido

    Thread starter heidienfern

    (@heidienfern)

    Bedankt Guido! @guido07111975

    Maar dit werkt ook niet en krijg de fout dat die lijn incorrect is.

    Dit is de fout:
    Warning: Undefined variable $widget_ops

    Guido

    (@guido07111975)

    Hoi,

    Dat is vreemd want die $widget_ops is aanwezig. Staan erboven in jouw code:

    function VMenuWidget() {
    	$widget_ops = array('classname' => 'vmenu', 'description' => __('Use this widget to add one of your custom menus as a widget.', THEME_NS));
    	parent::__construct('v_menu_widget', __('Vertical Menu', THEME_NS), $widget_ops);
    }

    Wordt wel verwezen naar die functie? Kun je uit het vermelde regelnummer opmaken.

    Guido

    Thread starter heidienfern

    (@heidienfern)

    Dag Guido,

    Ik heb het helemaal nog eens aangepast zoals je opgaf.
    Nu krijg ik de fout bij register_widget (‘VMenuWidget’)

    function WidgetsInit() {
        register_widget('VMenuWidget');
        register_widget('LoginWidget');
        
    }
    
    add_action('widgets_init', 'WidgetsInit');
    Guido

    (@guido07111975)

    Hoi,

    Ben de hele code nogmaals bij langs gegaan er is meer aan de hand. Deze functie is verouderd:

    function VMenuWidget() {
    	$widget_ops = array('classname' => 'vmenu', 'description' => __('Use this widget to add one of your custom menus as a widget.', THEME_NS));
    	parent::WP_Widget(false, __('Vertical Menu', THEME_NS), $widget_ops);
    }

    Moet worden:

    public function __construct() {
    	$widget_ops = array('classname' => 'vmenu', 'description' => __('Use this widget to add one of your custom menus as a widget.', 'THEME_NS'));
    	parent::__construct('VMenuWidget', __('Vertical Menu', 'THEME_NS'), $widget_ops);
    }

    THEME_NS heb ik tussen ‘ gezet, dit moet je tekstdomein (voor vertaling) zijn. Ik weet niet of vertaling werkt, als dit tussen ‘ staat.

    Ook de LoginWidget code moet je aanpassen, is verouderd.

    Guido

    Thread starter heidienfern

    (@heidienfern)

    Bedankt Guido!

    Fouten zijn weg, maar mijn menu is nog steeds leeg.

    <nav class="hf-nav desktop-nav">
        <div class="hf-nav-inner">
        
    <a href="#" class="hf-menu-btn"><span></span><span></span><span></span></a><ul class="hf-hmenu menu-3">
    	<li class="menu-item-"><a href="#"></a>
    	</li>
    	<li class="menu-item-"><a href="#"></a>
    	</li>
    	<li class="menu-item-"><a href="#"></a>

    In WP heb ik het menu nagekeken, ook bij de widgets, daar staat het goed.
    Nog een idee?

    Heidi

    Guido

    (@guido07111975)

    Hoi,

    Ik denk dat je sowieso THEME_NS overal moet vervangen door 'THEME_NS'. Of door je tekstdomein, staat meestal in bestand functions, in de functie load_theme_textdomain().

    Daarnaast wordt het menu aan de voorkant opgeroepen door de functie theme_get_option() die ergens in je thema wordt gemaakt. Dus ik kan het hier niet uittesten.

    Los hiervan, is het niet mogelijk om de code via Artisteer te updaten? Want dit kost zo best veel tijd.

    Guido

    Thread starter heidienfern

    (@heidienfern)

    Bedankt Guido!

    Ik zal dit bekijken en ook eens op Artisteer, maar het probleem is dat Artisteer PHP 4 gebruikte en we zitten nu al op 8.
    Er zijn code’s gewijzigd maar ik weet niet wat ik allemaal moet wijzigen.

    Ik kom er later nog op terug.

    Heidi

9 reacties aan het bekijken - 1 tot 9 (van in totaal 9)