Jump to content

Recommended Posts

I am in the process of converting a widget plugin where the user inputs the settings via the widget page to where the user inputs the settings via an admin page. Everything is working fine, apart from the dropdown option.

This is the settings field callback with the dropdown:

<?php
public function socialiconsselect_callback()
{
printf(
'<select id="socialiconsselect" name="social_contact_display_options[socialiconsselect]" />',
$options = array('Light', 'Dark', 'Modern Flat', 'Cute', 'Shaded', 'Simple Flat', 'Circle', 'Vintage', 'Retro', 'Retro 2', 'Wooden', 'CSS and HTML5 set 1', 'CSS and HTML5 set 2')
foreach ($options as $option) {
'<option value="' . $option . '" id="' . $option . '"', $socialiconsselect == $option ? ' selected="socialiconsselect"' : '', '>', $option, '</option>',
}
'</select>',
isset( $this->options['socialiconsselect'] ) ? esc_attr( $this->options['socialiconsselect']) : ''
);
}

Can anyone see what I have done wrong?

 

Put this through PHPStorm and corrected the syntax errors it stated, which gave me this:

public function socialiconsselect_callback()
{
    printf(
        '<select id="socialiconsselect" name="social_contact_display_options[socialiconsselect]" />',
        ($options = array('Light', 'Dark', 'Modern Flat', 'Cute', 'Shaded', 'Simple Flat', 'Circle', 'Vintage', 'Retro', 'Retro 2', 'Wooden', 'CSS and HTML5 set 1', 'CSS and HTML5 set 2')));
foreach ($options as $option) {
   '<option value="' . $option . '" id="' . $option . '"'; $socialiconsselect == $option ? ' selected="socialiconsselect"' : ''; '>'; $option;
    '</option>';
}
        '</select>';
        isset( $this->options['socialiconsselect'] ) ? esc_attr( $this->options['socialiconsselect']) : ''
    ;
}

The page now loads, but still does not show any options in the dropdown..

Link to comment
https://forums.phpfreaks.com/topic/284218-dropdown-option-in-function/
Share on other sites

You are using printf incorrectly

 

Change the function to

public function socialiconsselect_callback()
{
    $menu = '<select id="socialiconsselect" name="social_contact_display_options[socialiconsselect]" />';

    $options = array('Light', 'Dark', 'Modern Flat', 'Cute', 'Shaded', 'Simple Flat', 'Circle', 'Vintage', 'Retro', 'Retro 2', 
                     'Wooden', 'CSS and HTML5 set 1', 'CSS and HTML5 set 2');
    
    $socialiconsselect = isset( $this->options['socialiconsselect'] ) ? esc_attr( $this->options['socialiconsselect']) : '');
    foreach ($options as $option)
    {
        $menu .= '<option value="' . $option . '" id="' . $option . '"' . ($socialiconsselect == $option ? ' selected="socialiconsselect"' : '') . '>' .$option .'</option>';
    }

    $menu .= '</select>';

    return $menu; // return the html for the menu
}

 You can then echo the method

echo $obj->socialiconsselect_callback();

to display the menu

Edited by Ch0cu3r

Thanks for the reply. Sorry, I should of also pointed out I am currently using the following code to display the form:

public function create_admin_page()
    {
        // Set class property
        $this->options = get_option( 'social_contact_display_options' );
        ?>
        <div class="wrap">
            <?php screen_icon(); ?>
            <h2>Social Contact Display Settings</h2>
            <div id="scd-header"></div>
            <form method="post" action="options.php">
                <?php
                // This prints out all hidden setting fields
                settings_fields( 'social_contact_display_options_group' );
                do_settings_sections( 'social-contact-display-admin' );
                submit_button();
                ?>
            </form>
        </div>
    <?php
    }

It is being called within "social_contact_display_options" as part of "social_contact_display_options_group".

I wouldn't of known that.You just posted a few lines of code with no explanation of what does what.

 

 

 

I am starting to think it might just be easier to create the form directly within "public function create_admin_page()" instead of using callbacks..

No Idea.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.