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.

In the interest of automation, instead of making a long list of code like this

<td valign="top"width="50%"><p><label for="Facebook"><strong>Facebook URL</strong></label><input  type="text" name="Facebook" id="Facebook" size="52" value="<?php echo get_option('Facebook_url'); ?>"/></p></td></tr><tr>

I'd like to echo all of that out from PHP to generate the page. I have build an array containing all the social networks, twitter/facebook etc. As of now, this can read the saved settings but does not allow it to save new inputs.

function display_settings_fields($value,$key){
$keyurl = $key.'_url';
echo "The key $key has the value $value<br />";
echo '<td valign="top"width="50%"><p><label for="'.$key.'"><strong>'.$key .'URL</strong></label><input  type="text" name="'.$key.'" id="'.$key.'" size="52" value="';
echo get_option(''.$keyurl.'');
echo '"/></p></td></tr><tr>';

    }
    $social_icon_set = array(
    "Twitter" =>"$twitter_url",
    "Facebook" =>"$facebook_url",
    "GooglePlus" =>"$googleplus_url",
    "Yelp" =>"$yelp_url",
    "Youtube" =>"$youtube_url",
    "Vimeo" =>"$vimeo_url",
    "Flickr" =>"$flickr_url",
    "Tumblr" =>"$tumblr_url",
    "Picplz" =>"$picplz_url",
    "Foursquare" =>"$foursquare_url",
    "RSS" =>"$rss_url",
    "Behance" =>"$behance_url",
    "Goodreads" =>"$goodreads_url",
    "Github" =>"$github_url",
    "Formspring" =>"$formspring_url",
    "Myspace" =>"$mypsace_url",
    "Klout" =>"$klout_url",
    "Diaspora" =>"$diaspora_url",
    "LinkedIn" =>"$linkedin_url"
    );
    array_walk($social_icon_set,"display_settings_fields");
    ?>

The problem comes about with the "echo get_option(''.$keyurl.'');. Right now, its echoing the correct value, the saved value for it, but it does not allowing saving of anything edited in the text field. Is this even possible to echo/print this out, where its savable.

(This is mostly PHP but if there's another way around this, because it is using wordpress's get_options, im open to ideas)

share|improve this question
2  
I think a general how do I use the Settings API question (which, upon further reading, is what I think you're asking) is too broad-scope for the WPSE Q & A format. –  Chip Bennett Feb 21 '12 at 23:03

1 Answer 1

Use the Settings API. A look at my latest plugin may help you to understand how to register a save function for your fields. Most relevant excerpt:

/**
 * Register custom settings for the fields.
 *
 * @see    save_settings()
 * @see    print_input_field()
 * @return void
 */
public function add_contact_fields()
{
    register_setting(
        'general',
        $this->option_name,
        array ( $this, 'save_settings' )
    );

    foreach ( $this->fields as $type => $desc )
    {
        $handle   = $this->option_name . "_$type";
        $args     = array (
            'label_for' => $handle,
            'type'      => $type
        );
        $callback = array ( $this, 'print_input_field' );

        add_settings_field(
            $handle,
            $desc,
            $callback,
            'general',
            'default',
            $args
        );
    }
}

/**
 * Callback for 'register_setting()'.
 *
 * @see    add_contact_fields()
 * @param  array $settings
 * @return array $settings
 */
public function save_settings( array $settings = array () )
{
    $default  = get_option( $this->option_name );
    $settings = array_map( 'trim', $settings );
    $settings = $this->prepare_mail_save( $settings, $default );
    $settings = $this->prepare_phone_save( $settings );

    return $settings;
}

And … welcome to WordPress Stack Exchange!

share|improve this answer
1  
Thanks! I just discovered the WP SX, its amazing! –  alpha1 Feb 21 '12 at 23:58
    
I actually have all that, everything saves and works, minus the part about the $key. Just didnt want to post everything. –  alpha1 Feb 22 '12 at 2:43
    
@alpha1 Without the code which should save your $key it is impossible to tell what went wrong. –  toscho Feb 22 '12 at 2:48
    
$key exists solely there. echo get_options('Twitter'); works fine. What im trying to do is generate that based on an array. –  alpha1 Feb 22 '12 at 6:43
    
I'm rewriting it from scratch, but still running into the same problem. I need the "get_options('Twitter');" to be generated by php and put into the code where as the save button understands and saves it in the db. but echoing the function with a variable like $twitter inside the quotes doesn't work, probably because they're both being run at the time the page loads. –  alpha1 Feb 22 '12 at 6:47

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.