I have been working on adding options to a theme I am making. I have run into trouble having multiple options in a select box. I am looking to echo a code, depending on what value is saved in the option field.


function nikon_woocommerce_shop_width() {
    $shop_width = esc_attr( get_option( 'nikon_woocommerce_shop_width' ) );

    <select name="nikon_woocommerce_shop_width" value="<?php $nikon_woocommerce_shop_width; ?>">
        <option value="1" <?php selected( $shop_width, 1 ); ?> >1/4</option>
        <option value="2" <?php selected( $shop_width, 2 ); ?> >1/3</option>
        <option value="3" <?php selected( $shop_width, 3 ); ?> >1/2</option>
        <option value="4" <?php selected( $shop_width, 4 ); ?> >Full</option>



$woocomemrce_shop_width = esc_attr( get_option( 'nikon_woocomemrce_shop_width' ) );

function nikon_woocommerce_shop_width_preoutput() {

    if( selected( 1, $woocommerce_shop_width, false ) ) {
        echo 'class="col-3';
    } else if( selected( 2, $woocommerce_shop_width, false ) ) {
        echo 'class="col-4';
    } else if( selected( 3, $woocommerce_shop_width, false ) ) {
        echo 'class="col-6';
    } else if( selected( 4, $woocommerce_shop_width, false ) ) {
        echo 'class="col-12';

add_action( 'nikon_woocommerce_shop_width_output', 'nikon_woocommerce_shop_width_preoutput' );



$woocommerce_shop_class = esc_attr( get_option( 'nikon_woocommerce_shop_class' ) );
$woocomemrce_shop_width = esc_attr( get_option( 'nikon_woocommerce_shop_width' ) );


<div id="woocommerce_shop">

    <?php if( selected( 2, $woocommerce_shop_class, false) ) { echo ''; } else { echo '<div class="container">'; }  ?>

        <div class="row">

            <div <?php echo $woocomemrce_shop_width; ?> <?php do_action( 'nikon_woocommerce_shop_width_output' ); ?> >

                <?php woocommerce_content(); ?>


            <div class="col">

                <?php get_sidebar( 'woocommerce' ); ?>



    <?php if( selected( 2, $woocommerce_shop_class, false) ) { echo ''; } else { echo '</div>'; }   ?>


I can echo just the option and it will show the related number to the saved option, however I cannot seem to work out how to make the if statement work.

If you need further details, please let me know!


selected is just an equals if statement that outputs selected or not. It doesn't perform any special checks, it just makes the code for <option> tags a bit nicer. Without the selected function, you'd need to use a ternary operator, or temporary variables, the code for outputting forms would be awful.

As the official docs say:

Outputs the html selected attribute.


Compares the first two arguments and if identical marks as selected


So just compare the first two arguments.

What you tried to do here:

} else if( selected( 3, $woocommerce_shop_width, false ) ) {

Is just a very complicated and broken way of doing this:

} else if( 3 == $woocommerce_shop_width ) {
|improve this answer|||||
  • I thought there was an easier way! Still getting the hang of this PHP stuff lol – Jesse Feb 24 at 1:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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