WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So I want to have defaults set up for each of my plugins settings, and I'm using wp_parse_args to do so which I believe is the correct way, but I'm hitting a roadblock.

Here's my code at the moment:

function eddslider_options_each( $edd_slider_options ) {

$edd_slider_options = get_option( 'eddslider_options' );

/**
 * Define the array of defaults
 */ 
$defaults = array(
    'slider_insert'     => 0,
    'slider_arrows'     => 0,
    'slider_bullets'    => 0,
    'slider_effect'     => 'fade',
    'slider_theme'      => 'default'
);
/**
 *  Parse incomming $args into an array and merge it with $defaults
 */ 
$edd_slider_options = wp_parse_args( $edd_slider_options, $defaults );

/**
 * OPTIONAL: Declare each item in $args as its own variable i.e. $type, $before.
 */ 
extract( $edd_slider_options, EXTR_SKIP );

}

Now if I add something like:

$output = $slider_insert;
return $output;

I'm able to use <?php echo eddslider_options_each('slider_insert'); ?> and it will have the defined default set and work well. How do I do this for every option though? Thank you.

share|improve this question
up vote 2 down vote accepted

Try:

function eddslider_options_each( $key ) {

    $edd_slider_options = get_option( 'eddslider_options' );

     /* Define the array of defaults */ 
    $defaults = array(
        'slider_insert'     => 0,
        'slider_arrows'     => 0,
        'slider_bullets'    => 0,
        'slider_effect'     => 'fade',
        'slider_theme'      => 'default'
    );

    $edd_slider_options = wp_parse_args( $edd_slider_options, $defaults );

    if( isset($edd_slider_options[$key]) )
         return $edd_slider_options[$key];

    return false;
}
share|improve this answer
    
Awesome - works perfectly. Thanks for that. – bryceadams Oct 12 '12 at 0:46

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.