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.

Im creating custom theme options and Im getting this PHP error: Warning: Illegal string offset in ... on line 75

// adding the page to menu
add_menu_page('Theme Options', 'Theme Options', 'manage_options', 'rm-options', 'rm_display_options_page');

// form to display options
<form method="post" action="options.php">
            <?php
                settings_fields('rm_theme_options'); // options name
                do_settings_sections('rm-options'); // page
                submit_button();
            ?>
</form>

// adding a section
add_settings_section('rm_general_section', 'General options', 'rm_general_section_callback', 'rm-options');

// register the options
register_setting( 'rm_theme_options', 'rm_theme_options', 'esc_attr' );

// register a field
$some_option_args = array('type' => 'text', 'id' => 'some_option_name', 'name' => 'some_option_name', 'desc' => 'Description');
add_settings_field('some_option_name', 'Some Option', 'pu_display_setting', 'rm-options', 'rm_general_section', $some_option_args);

// finally a function to display the fields
function pu_display_setting($args){
    extract( $args );
    $options = get_option( 'rm_theme_options' );

    switch ( $type ) {  
          case 'text':
              //$options[$id] = stripslashes($options[$id]);  
              //$options[$id] = esc_attr( $options[$id]);
              // this is line 75 where im getting the warning
              echo "<input type='text' id='$id' name='" . 'rm_theme_options' . "[$id]' value='$options[$id]'>";
              echo ($desc != '') ? "<br /><span class='description'>$desc</span>" : "";  
          break;  
    }
}

Now when Im trying to save the option, the error is still there and the value of the input is "A", when I check the database, the actual value is just "Array". Whats wrong with the line 75? In the tutorial where I got the display setting function, there were also two more lines which are stripping the slahes and esc_attr.

EDIT:

Thats now how my code actually is, Im using the function to display the page but I just didnt want to put every piece of code here so it isnt a big mess. This is how it looks complete:

function rm_display_options_page() { ?>
    <div class="wrap">
        <h2><?php echo 'Theme options: ' . wp_get_theme(); ?></h2>
        <div class="description">Theme options description</div>

        <?php settings_errors(); ?>

        <form method="post" action="options.php">
            <?php
                settings_fields('rm_theme_options'); // options name
                do_settings_sections('rm-options'); // page
                submit_button();
            ?>
        </form>
    </div>
<?php
}

And the line 75 is:

echo "<input type='text' id='$id' name='" . 'rm_theme_options' . "[$id]' value='$options[$id]'>";
share|improve this question

closed as off-topic by Chip Bennett, toscho Feb 26 at 22:49

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

1  
What line is 75? –  Brian Fegter Feb 25 at 22:31
    
@BrianFegter I added the comment above the line, before it was after :) This is the line: echo "<input type='text' id='$id' name='" . 'rm_theme_options' . "[$id]' value='$options[$id]'>"; –  Richard Mišenčík Feb 26 at 10:17

2 Answers 2

The code you posted has a major flaw*: it mixes HTML and PHP without properly setting them apart. This code chunk has a PHP function call followed by an HTML string, but since there's nothing telling the file that it should be treated as HTML, the parser says "Hey, <form isn't anything I recognize. I'm outta here!"

* That nearly everyone gets hit by at some time when using PHP and HTML. :)

The offending code here is

// adding the page to menu
add_menu_page('Theme Options', 'Theme Options', 'manage_options', 'rm-options', 'rm_display_options_page');

// form to display options
<form method="post" action="options.php">
        <?php
            settings_fields('rm_theme_options'); // options name
            do_settings_sections('rm-options'); // page
            submit_button();
        ?>
</form>

You have a couple of options here, one is to tell PHP to echo the HTML like this:

// adding the page to menu
add_menu_page('Theme Options', 'Theme Options', 'manage_options', 'rm-options', 'rm_display_options_page');

// form to display options
echo '<form method="post" action="options.php">';
            settings_fields('rm_theme_options'); // options name
            do_settings_sections('rm-options'); // page
            submit_button();
echo '</form>';

Another is to bracket out the PHP parts so the HTML isn't parsed by PHP:

// adding the page to menu
add_menu_page('Theme Options', 'Theme Options', 'manage_options', 'rm-options', 'rm_display_options_page');

// form to display options
?>
<form method="post" action="options.php">
        <?php
            settings_fields('rm_theme_options'); // options name
            do_settings_sections('rm-options'); // page
            submit_button();
        ?>
</form>
<?php
 ...

* Update *

There's a bit of string concatenation that I can't quite wrap my eyes around, so I've changed that here to make it easier for me to grasp. It may help with fixing any errors, too. It's also best practice to escape attributes in your HTML, so I updated that line to include those functions.

echo "<input type='text' id='$id' name='rm_theme_options[$id]' value='" . esc_attr( $options[$id] ) . "' />';
share|improve this answer
    
As a developer, the proper technique is to use php tags. –  Shawn Feb 25 at 22:51
1  
Since both are supported and useful, I'm not sure there's a "proper" technique. I do my best to follow the WordPress coding standards, but on this one there's not a standard yet (see make.wordpress.org/core/handbook/coding-standards/php/…), although many examples do use the opening and closing tags to offset the html (see codex.wordpress.org/Creating_Options_Pages). –  morganestes Feb 25 at 23:01
1  
Actually, in this case, that <form> needs to be inside a callback, hooked into admin_menu(), and the register_setting() and add_setting*() calls need to be in a callback, hooked into admin_init. –  Chip Bennett Feb 25 at 23:08
    
Also, there are other Codex examples of using echo for HTML, so that method is perfectly valid (if more challenging to read and maintain). codex.wordpress.org/Settings_API –  morganestes Feb 25 at 23:08
    
This is not really the place to discuss the relative merits of PHP tags vs echo to spit out markup. There are questions on Stack Overflow (for example) and the network has chat rooms were you want to argue your points if you wish. Thanks. –  s_ha_dum Feb 25 at 23:22

This appears to be a PHP syntax issue only:

echo "<input type='text' id='$id' name='" . 'rm_theme_options' . "[$id]' value='$options[$id]'>";

The first issue is mixing PHP variables and string concatenation. I assume that you need the string 'rm_theme_options[$id]', where $id is replaced with its value? If so, your string concatenation should reflect that:

echo "<input type='text' id='" . $id . "' name='rm_theme_options[" . $id . "]' value='" . $options[$id] . "'>";

It is subjective, but I recommend using double-quotes for HTML, and single-quotes for PHP strings. It's just far easier to follow:

echo '<input type="text" id="' . $id . '" name="rm_theme_options[' . $id . ']" value="' . $options[$id] . '">';

The main advantage of enclosing PHP strings in double-quotes is that you can pass variables directly into the string; but in complex strings that mix double- and single-quotes, that (as you've found out) can just get really complicated.

share|improve this answer
    
Thanks, I already did this when I was trying and editing the code to get it to work, I also replaced the double quotes with single quotes just like your third example in the answer above. I also removed the esc_attr from register_setting, without that it now seems to work and I dont get that error now. –  Richard Mišenčík Feb 27 at 15:08

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