0

I have a plugin and a part in it allows user to select the page plugin works on by specifying the id of the page. But I want that he should be able to select the page from the pages available in the website.

This can be implemented by a drop down or check boxes.

my code looks like this in the main file of plugin :

register_setting( 'rex_plug_options', 'rex_plug_options', 'rex_plug_settings_validate' );
add_settings_field('rex_plugbox_text_exit_pop_selected_pages', 'Select Pages (comma seprated page ID E.G.:  12,32,434)', 'rex_plugbox_text_exit_pop_selected_pages', 'rex_plug_box', 'rex_plug_main_box');
<?php
}
function rex_plugbox_text_exit_pop_selected_pages(){
global $rex_plug_options;
if(!isset($rex_plug_options["rex_plugbox_text_exit_pop_selected_pages"]))   {
    $rex_plug_options["rex_plugbox_text_exit_pop_selected_pages"] = "";
}
?>
    <textarea id="rex_plugbox_text_exit_pop_selected_pages" type="text" name="rex_plug_options[rex_plugbox_text_exit_pop_selected_pages]" ><?php echo sanitize_text_field( $rex_plug_options["rex_plugbox_text_exit_pop_selected_pages"] ); ?></textarea>

<?php
}

Now this works with the text area and all I want it to be a dropdown. how can I do that ?

I tried doing this :

 <select name="rex_plug_options[page_id]">
                        <?php
                        if( $pages = get_pages() ){
                            foreach( $pages as $page ){
                                echo '<option value="' . $page->ID . '" ' . selected( $page->ID, $options['page_id'] ) . '>' . $page->post_title . '</option>';
                            }
                        }
                        ?>
                    </select>

this display the select field but not sure how to update the database option array field with what is selected in this page.

0

Change your code to this

register_setting( 'rex_plug_options', 'rex_plug_options', 'rex_plug_settings_validate' );
add_settings_field('rex_plugbox_text_exit_pop_selected_pages', 'Select Pages', 'rex_plugbox_text_exit_pop_selected_pages', 'rex_plug_box', 'rex_plug_main_box');
//with selectbox you cannot select multiple pages. If you need that try checkbox

function rex_plugbox_text_exit_pop_selected_pages(){
    global $rex_plug_options;
    if(!isset($rex_plug_options["rex_plugbox_text_exit_pop_selected_pages"]))   {
        $rex_plug_options["rex_plugbox_text_exit_pop_selected_pages"] = "";
    }
?>
    <select id="rex_plugbox_text_exit_pop_selected_pages" name="rex_plug_options[rex_plugbox_text_exit_pop_selected_pages]">
    <?php
    if( $pages = get_pages() ){
        foreach( $pages as $page ){
            echo '<option value="' . $page->ID . '" ' . selected( $page->ID, $rex_plug_options["rex_plugbox_text_exit_pop_selected_pages"] ) . '>' . $page->post_title . '</option>';
        }
    }
    ?>
    </select>
<?php
}
|improve this answer|||||
  • I am using it on a plugin io the backend, what does after the form submitted means ? – Rishiraj Purohit Jun 1 '15 at 10:25
  • @RishiRajPurohit Where does user get the option to select page? After selecting the page, don't they submit a form? How do you get the selected page? – sakibmoon Jun 1 '15 at 12:31
  • its a plugin with options, once they write their id they can submit. The page-id gets saved in database. I want them to have a dropdown where they can select page – Rishiraj Purohit Jun 1 '15 at 19:56
  • @RishiRajPurohit Check my edit. I think if your textarea method works, this should work too. You should understand that I don't have any idea how your plugin works. – sakibmoon Jun 2 '15 at 1:34
  • its fine and thanks for your valuable time, I figured it out . I was missing the name field of select. Anyhow you know where I can learn about tag system. you type word a ajax dropdown shows possible values and then you select one and you get option to select more than one – Rishiraj Purohit Jun 2 '15 at 6:08

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.