Forums

[resolved] Missing argument 1 for ::control() (9 posts)

  1. notturnal666
    Member
    Posted 1 month ago #

    Hi, i'm new to WP... i'm trying to write a plugin that enable a widget to display on the sidebar a charts of bestseller book from a website.

    I've read some documentation and i start with a test:

    <?
    // init widget
    add_action("widgets_init", array('gdl_charts', 'register'));
    //include jQuery
    function my_init() {
    	if (!is_admin()) {
    		wp_enqueue_script('jquery');
    	}
    }
    add_action('init', 'my_init');
    //main class
    class gdl_charts {
    	// build controls
    	function control($data){
    	$data = get_option('gdl_charts');
      	?>
    	<p><label>Website <select name="gdl_charts_website">
        <option value="www.website.com" <?php if ($data['website']=='www.website.com'){echo 'selected';} ?> >WebSite.com</option>
    	...
    	...
      	<?php
      	 if (isset($_POST['gdl_charts_website'])){
        	$data['website'] = attribute_escape($_POST['gdl_charts_website']);
    		...
    		...
    		update_option('gdl_charts', $data);
    		}
    	}
    	//print widget
    	function widget($args, $instance){
    	////////////////////////////////////
    	$instance = get_option('gdl_charts');
    	$gdl_website = $instance['website'];
    	..
    	..
    
    	echo $args['before_widget'];
        echo $args['before_title'] . 'GDL Charts' . $args['after_title'];
    	?>
    	<div class="gdl_chart_show">
      	<h3 style="background-color:#ccc;padding:3px;margin:3px 0;">Books Chart</h3>
        <?php
    	echo "<ul>";
    	echo "<li><strong>" . $gdl_website . "</strong> </li>";
    	echo "</ul>";
    	?>
        </div>
    	<?php
    	echo $args['after_widget'];
    	}
    	//register widget
    	function register(){
    	register_sidebar_widget('GDL Charts', array('gdl_charts', 'widget'));
        register_widget_control('GDL Charts', array('gdl_charts', 'control'));
    	}
    
    }
    ?>

    It seems to work, but i get 2 errors for missing arguments.
    The first is on the backend, it says "Missing argument 1 for gdl_charts::control() in /home/mhd-01/htdocs/wp-content/plugins/gdl-charts/gdl-charts.php on line 37".
    Then on the frontend, it says "Missing argument 2 for gdl_charts::widget() in /home/mhd-01/htdocs/wp-content/plugins/gdl-charts/gdl-charts.php on line 74".

    I'm sure i miss something in the plugin syntax... someone can help me?
    Thanx in advance.

  2. apljdi
    Member
    Posted 1 month ago #

    If you are building a new widget maybe you should try to build it using the new widget API. Honestly, its worth learning. The new way is many times better than the old way.

  3. notturnal666
    Member
    Posted 1 month ago #

    great, it was really helpfull! with this new structure i get no errors...
    anyway, how i can set a session variable?

    i've tryed using something like $_SESSION['foo'] = "test"; but each time i reload the page i lost this variable.

    thanks again!

  4. apljdi
    Member
    Posted 1 month ago #

    how i can set a session variable?

    You need to run session_start() before anything is sent to the browser. Placing <?php session_start(); ?> at the very beginning of your theme's header.php should get it working.

  5. notturnal666
    Member
    Posted 1 month ago #

    Thanx again apljdi! I thought that the session was started in the core of WP... so, how i can start it with a widget?
    There is any function like wp_enqueue_script(''); to start the session? Or is better to use cookie?

  6. apljdi
    Member
    Posted 1 month ago #

    You could try hooking to the earlier hooks in the load sequence but I believe that running it any time before content is sent to the browser should work.

  7. notturnal666
    Member
    Posted 1 month ago #

    i've solved by adding session_start() with the init function, something like:

    function my_init_method() {
        session_start();
        wp_enqueue_script('jquery');
        }
        add_action('init', my_init_method);

    it seems to work... but is it correct for a good compatibility with other wp version? (i'm using 2.8.4)

  8. apljdi
    Member
    Posted 1 month ago #

    It looks OK to me. I was thinking the init() hook might be the thing. It does probably load on admin pages too, which might be more than you want.

  9. notturnal666
    Member
    Posted 1 month ago #

    thanx again for your help apljdi!

Reply

You must log in to post.

About this Topic