Creating option to allow user to select the page my plugin content will display on

I have an options page for my plugin, but cannot seem to figure out how to create an option to allow admins to select a page that will display my plugins content.

Does anyone have an example of how this is done?

Read More

EDIT:
I created a plugin and have a widget to search the content of my plugin. However I need the widget to post to the page that my plugins content is being displayed on. Since the widget is on the sidebar it could be submitted from any page, I need to be able to detect which page the plugin content is being displayed on and se the to that page.
1. detect the shortcode on a given page, and use that.
OR
2. Create an option from a settings page to allow the admin to select which page the plugin content will be displayed on and get rid of the shortcode.
I hope that clarifies.

I have a settings page for my plugin, I just do not understand how to get a list of the available pages and save/update the option in the wp_options table.

Related posts

2 comments

  1. Here’s a quick options page that will give you a dropdown select for choosing a page, using the get_pages function. The Settings API takes care of saving the options for you. You can then use get_option to load the options array in your template, and get_post to load the post data associated with the ID saved in your option.

    add_action( 'admin_init', 'russ_options_init' );
    add_action( 'admin_menu', 'russ_options_page' );
    
    function russ_options_init(){
        register_setting(
            'russ_options_group',
            'russ_options',
            'russ_options_validate'
        );
    }
    
    function russ_options_page() {
        add_options_page(
            'Russ Options',
            'Russ Options',
            'manage_options',
            'russ_options',
            'russ_render_options'
        );
    }
    
    function russ_render_options() {
        ?>
        <div class="wrap">
            <form method="post" action="options.php">
                <?php
                settings_fields( 'russ_options_group' );
                $options = get_option( 'russ_options' );
                ?>
                <table class="form-table">
                    <tr valign="top"><th scope="row">Choose a page</th>
                        <td>
                            <select name="russ_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>
                        </td>
                    </tr>
                </table>
                <p class="submit">
                    <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
                </p>
            </form>
        </div>
        <?php   
    }
    
    function russ_options_validate( $input ) {
        // do some validation here if necessary
        return $input;
    }
    

Comments are closed.