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'm constantly getting the errors bellow when printing the radio button values from General Settings screen:

Illegal string offset 'service' in F:\wamp\www\plugin-tester\wp-content\themes\twentythirteen\functions.php on line 565

Illegal string offset 'service' in F:\wamp\www\plugin-tester\wp-content\themes\twentythirteen\functions.php on line 568

Here is the code which I added to functions.php file:

add_filter('admin_init', 'myservice_register_function');
function myservice_register_function(){
    register_setting('general', 'my_service', 'esc_attr');
    add_settings_field('my_service', '<label for="service_need">'.__('Do You need My    Service' , 'my_service' ).'</label>' , 'service_function', 'general');
}

function service_function(){
    $options = get_option( 'my_service', '');
    //  $options = get_settings( 'my_service');

    if($options['service'] == 'YES') { //line number 565
        echo 'Yes, Service Need'; 
    }
    if($options['service'] == 'NO') { // line number 568
        echo 'No Need Service';
    }
    $html = '<input type="radio"  name="my_service[service]" value="YES"/>'; 
    $html .= '<label> NEED </label>'; 

    $html .= '<input type="radio"  name="my_service[service]" value="NO"/>'; 
    $html .= '<label > NO NEED </label>'; 

    echo $html;
}

What's the mistake?

share|improve this question
    
You should be using PHP isset() function to check if key exists in array. This is just pure PHP and not specific to WP. –  Rarst Oct 18 '13 at 20:07
    
I put isset().. then no error showing but can not get the value. If i do print_r($options) then only output come: ARRAY –  mehedi doha Oct 18 '13 at 20:23

1 Answer 1

up vote 0 down vote accepted

Apart from the isset issue, pointed by Rarst, there's an error in the sanitization function. esc_attr seems not to work with radio buttons, using esc_sql does the job.
You're also missing the checked state for the buttons:

add_filter( 'admin_init', 'myservice_register_function' );

function myservice_register_function()
{
    register_setting( 'general', 'my_service', 'esc_sql' );
    add_settings_field(
        'my_service', 
        '<label for="service_need">'.__('Do You need My Service' , 'my_service' ).'</label>' , 
        'service_function', 
        'general'
    );
}

function service_function()
{
    $options = get_option( 'my_service', '');
    $yes = $no = '';
    if( isset( $options['service'] ) )
    {
        if( $options['service'] == 'YES') 
        {
            echo 'Yes, Service Need<br />'; 
            $yes = ' checked="checked"';
        }
        if( $options['service'] == 'NO') 
        {
            echo 'No Need Service<br />';
            $no = ' checked="checked"';
        }
    }
    ?>
    <input type="radio"  name="my_service[service]" value="YES"<?php echo $yes; ?> />
    <label> NEED </label>

    <input type="radio"  name="my_service[service]" value="NO"<?php echo $no; ?> />
    <label> NO NEED </label> 
    <?php
}
share|improve this answer
    
thanks a lot... it works. Really appreciated :) –  mehedi doha Oct 18 '13 at 20:41

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.