WordPress插件-找不到选项页面

时间:2018-12-11 14:23:39

标签: php wordpress wordpress-plugin-creation

我正在编写我的第一个插件,以便在Facebook和Instagram上分享帖子,并且正在编写插件选项页面。 我总是收到错误消息“找不到选项页面”。

我认为回调函数中的register_setting可以解决这个问题,但事实并非如此。

我在做什么错了?

这是我的代码:

<?php 

class Socialize {
    public function __construct() {
        // Hook into the admin menu
        add_action( 'admin_menu', array( $this, 'create_plugin_settings_page' ) );
        add_action( 'admin_init', array( $this, 'setup_sections' ) );
        add_action( 'admin_init', array( $this, 'setup_fields' ) );
    }

    public function setup_fields() {
        add_settings_field( 'facebook_account', 'Facebook Username', array( $this, 'fb_account_callback' ), 'smashing_fields', 'facebook_section' );
        add_settings_field( 'facebook_password', 'Facebook Password', array( $this, 'fb_pwd_callback' ), 'smashing_fields', 'facebook_section' );
        add_settings_field( 'instagram_account', 'Instagram Username', array( $this, 'insta_account_callback' ), 'smashing_fields', 'instagram_section' );
        add_settings_field( 'instagram_password', 'Instagram Password', array( $this, 'insta_pwd_callback' ), 'smashing_fields', 'instagram_section' );
    }

    public function setup_sections() {
        add_settings_section( 'facebook_section', 'Facebook Account', array( $this, 'section_callback' ), 'smashing_fields' );
        add_settings_section( 'instagram_section', 'Instagram Account', array( $this, 'section_callback' ), 'smashing_fields' );
    }

    public function fb_account_callback( $arguments ) {
        echo '<input name="fb_account" id="fb_account" type="text" value="' . get_option( 'facebook_account' ) . '" />';
        register_setting( 'smashing_fields', 'facebook_account' );
    }

    public function fb_pwd_callback( $arguments ) {
        echo '<input name="fb_pwd" id="fb_pwd" type="password" value="' . get_option( 'facebook_password' ) . '" />';
        register_setting( 'smashing_fields', 'facebook_password' );
    }

    public function insta_account_callback( $arguments ) {
        echo '<input name="insta_account" id="insta_account" type="text" value="' . get_option( 'instagram_account' ) . '" />';
        register_setting( 'smashing_fields', 'instagram_account' );
    }

    public function insta_pwd_callback( $arguments ) {
        echo '<input name="insta_pwd" id="insta_pwd" type="password" value="' . get_option( 'instagram_password' ) . '" />';
        register_setting( 'smashing_fields', 'instagram_password' );
    }

    public function section_callback( $arguments ) {
        switch ( $arguments['id'] ) {
            case 'facebook_section':
                echo 'This is the Facebook section';
                break;
            case 'instagram_section':
                echo 'This is the Instagram section';
                break;
        }    
}

public function create_plugin_settings_page() {
    // Add the menu item and page
    $page_title = 'Free Socialize';
    $menu_title = 'Free Socialize';
    $capability = 'manage_options';
    $slug = 'smashing_fields';
    $callback = array( $this, 'plugin_settings_page_content' );
    $icon = 'dashicons-admin-plugins';
    $position = 100;

    add_menu_page( $page_title, $menu_title, $capability, $slug, $callback, $icon, $position );
}

public function plugin_settings_page_content() { ?>
    <div class="wrap">
        <h2>Free Socialize Settings Page</h2>
        <form method="post" action="options.php">
            <?php
                settings_fields( 'smashing_fields' );
                do_settings_sections( 'smashing_fields' );
                submit_button();
            ?>
        </form>
    </div> <?php
}


}
new Socialize();

?>

1 个答案:

答案 0 :(得分:0)

如果我误解了,我无法发表评论以致歉意,但在这种情况下您不需要使用add_plugins_page()吗?

<?php
  add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $function);
?> 

文档:https://codex.wordpress.org/Function_Reference/add_plugins_page

希望能有所帮助,我已经了解了:)