3

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?

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.

3
  • Why not use a shortcode instead? Commented Nov 1, 2013 at 20:34
  • That is what I have now, the problem is that I have a search widget that may be on other pages and I need it to submit to the page containing the plugins output. So I posted the question how to detect the page a shortcode is embedded in and got the response why not let the user select the page with a simple option. wordpress.stackexchange.com/questions/120937/…
    – Russ
    Commented Nov 1, 2013 at 20:56
  • Can you explain this better: "how to create an option to allow admins to select a page that will display my plugins content". Also, how is your actual plugin page built, how this selection should happen, and based on what? And you are encouraged to edit the Question instead of clarifying things in Comments.
    – brasofilo
    Commented Nov 1, 2013 at 21:03

2 Answers 2

6

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;
}
1
  • Thank you Milo, this is just what I needed. Works like a charm.
    – Russ
    Commented Nov 2, 2013 at 17:05
1

Rather than reimplement the dropdown as @Milo suggested, it's easier to use

wp_dropdown_pages( array( 'name' => 'russ_options[page_id]', 'selected' => $options['page_id'] ) );

--as described at https://developer.wordpress.org/reference/functions/wp_dropdown_pages/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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