最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited Nov 1, 2013 at 21:19 Russ asked Nov 1, 2013 at 20:10 RussRuss 351 silver badge4 bronze badges 3
  • Why not use a shortcode instead? – Andrew Bartel 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/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
Add a comment  | 

2 Answers 2

Reset to default 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;
}

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/reference/functions/wp_dropdown_pages/

发布评论

评论列表(0)

  1. 暂无评论