Jump to content

Dropdown option in function


HPTOnline

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.