Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

i have a setup like

function form($instance) {
    $defaults = array(
        'title' => 'Recent Products',
        'number_products' => 3
    );
    $instance = wp_parse_args($instance, $defaults);

but my title is ok but number_products always does not show. i debuged in netbeans and found that form() is run 2 times and 1st time, variables look like

$instance = array(0)
// after wp_parse_args, 
$instance = array('title' => 'Recent Products', 'number_products' => 3);

on 2nd run of wp_parse_args, i dont know why it runs the 2nd time,

$instance = array('title' => 'Recent Products', 'number_products' => null);
// after wp_parse_args
$instance = array('title' => 'Recent Products', 'number_products' => null);

i dont really know whats happening ... can some1 enlighten me?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

i finally found the answer, its actually not a problem with the code i posted. its in the update() function. i have some validation code

function update($new_instance, $old_instance) {
    $instance['title'] = esc_attr(strip_tags($new_instance['title']));
    if (is_int($new_instance['number_products'])) {
        if ($new_instance['number_products'] > 0)
            $instance['number_products'] = $new_instance['number_products'];
        else
            $instance['number_products'] = 1;
    } else {
        $instance['number_products'] = $old_instance['number_product'];
    }

    return $instance;
}

the problem is at 2 places. i should be using is_numeric over is_int, the reason is

To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric().

then, in the else, $old_instance['number_product'] should have be plural $old_instance['number_products']

share|improve this answer

I am not sure about two runs either. Had you tried just dumping content inside function instead of debug? Something like this:

function form( $instance ) {

    var_dump( $instance );

    $defaults = array(
        'title' => 'Recent Products',
        'number_products' => 3
    );
    $instance = wp_parse_args($instance, $defaults);

    var_dump( $instance );
share|improve this answer
    
yes. that way, i will get just what i specified "on 2nd run" where both $instance-es are array('title' => 'Recent Products', 'number_products' => null); –  Jiew Meng Sep 15 '10 at 7:22
    
Then it's likely issue with what gets passed to function and not function itself. I am not too confident with widget form specifics will try to poke it later. Could you please update question with full code of your widget? Or at least enough that I can copy and test. –  Rarst Sep 15 '10 at 7:58
    
thanks rarst, its actually my mistake i have created an answer –  Jiew Meng Sep 15 '10 at 8:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.