DevinVinson / WordPress-Plugin-Boilerplate

[WordPress] A foundation for WordPress Plugin Development that aims to provide a clear and consistent guide for building your plugins.
http://wppb.io
7.66k stars 2.25k forks source link

How to show the admin page (admin/partials/pluginname-admin-display.php)? #298

Open lazharichir opened 9 years ago

lazharichir commented 9 years ago

Hello all,

A bit confused, I created my plugin pretty much and now need to create my settings admin page. I added a hook in pluginname-class.php as follows:

// Settings
$this->loader->add_action( 'admin_init', $plugin_admin, 'display_admin_page' );

And in my class-pluginname-admin.php, I have my function:

public function display_admin_page() {

    add_settings_section(
        'eg_setting_section',
        'Example settings section in reading',
        'eg_setting_section_callback_function',
        'reading'
    );

    add_settings_field(
        'eg_setting_name',
        'Example setting Name',
        'eg_setting_callback_function',
        'reading',
        'eg_setting_section'
    );

    register_setting( 'reading', 'eg_setting_name' );

    //require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/partials/pluginname-admin-display.php'; i tried uncommenting this one line but still no luck

}

public function eg_setting_section_callback_function() {
    echo '<p>Intro text for our settings section</p>';
}

public function eg_setting_callback_function() {
    echo '<input name="eg_setting_name" id="eg_setting_name" type="checkbox" value="1" class="code" ' . checked( 1, get_option( 'eg_setting_name' ), false ) . ' /> Explanation text';
}

However, I get the error Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'display_admin_page' not found or invalid function name in /Users/dev/wp-includes/plugin.php on line 496 and the admin page is not displayed (the page is in admin/partials/pluginname-admin.display.php)

How can I get to display my admin settings page?!

slushman commented 9 years ago

The references to the functions with in your admin file need to use an array since you're using a class. More like:

add_settings_section( 'eg_setting_section', 'Example settings section in reading', array( $this, 'eg_setting_section_callback_function' ), 'reading' );

add_settings_field(
    'eg_setting_name',
    'Example setting Name',
   array( $this,  'eg_setting_callback_function' ),
    'reading',
    'eg_setting_section'
);

I have an example plugin that shows all this here: https://github.com/slushman/now-hiring

lazharichir commented 9 years ago

It does work, however, once I save my options by submitting my form (to options.php) it comes back saying ERROR: options page not found.)

Googled it and apparently a common error with OOP so still trying to get my head around as solutions found on stackoverflow didn't work for me......

On Mon, 1 Jun 2015 at 14:13 Chris Wilcoxson notifications@github.com wrote:

The references to the functions with in your admin file need to use an array since you're using a class. More like:

add_settings_section( 'eg_setting_section', 'Example settings section in reading', array( $this, 'eg_setting_section_callback_function' ), 'reading' );

add_settings_field( 'eg_setting_name', 'Example setting Name', array( $this, 'eg_setting_callback_function' ), 'reading', 'eg_setting_section' );

I have an example plugin that shows all this here: https://github.com/slushman/now-hiring

— Reply to this email directly or view it on GitHub https://github.com/DevinVinson/WordPress-Plugin-Boilerplate/issues/298#issuecomment-107449598 .

Lazhar

erayit commented 9 years ago

Based on what I read you have to create the options before you try to use them.. You could use something like this....

if( false == get_option( 'reading_options' ) ) {
add_option( 'reading_options', apply_filters( 'default_reading_options',
$this->default_reading_options() ) ); }

add_settings_section( 'eg_setting_section', 'Example settings section in reading', array( $this, 'eg_setting_section_callback_function' ), 'reading_options' );

function default_reading_options(){ $defaults = array( 'some option' => '', 'another option' => '', 'third option' => '', );

return apply_filters( 'default_reading_options', $defaults );

}

... then continue with adding fields as you were

I did NOT figure all of this out by myself, I went through this tutorial by Tom McFarlin.- https://tommcfarlin.com/the-complete-guide-to-the-wordpress-settings-api/

It was very helpful finding much of what I was doing manually is already wired up in the core. It works great and the tutorial has a completed project you can download for reference. You will have to change a few things from theme to plugin but nothing to be concerned with.

Also, your issue title is somewhat misleading as I thought you were having issues displaying the partials page (as I was). If you get past these settings issues and want more info about using the /admin/partials page the closed issue is here - https://github.com/DevinVinson/WordPress-Plugin-Boilerplate/issues/300

Good luck

erayit commented 9 years ago

Oh and you might want to use an option name that is more specific to your plugin.. randomplugin_reading_options so you dont run into conflicts with other plugins.. reading or reading_options (which I changed it to) is very generic.

Slushman's now-hiring plugin is a very nice example of how to implement the BoilerPlate.

lazharichir commented 9 years ago

I tried the now-hiring example, and it works up until I save the options, I either get options page not found if I use action="options.php" or it just reloads itself if I leave the action="" blank.

I used the exact same lines and hooks to try it first, before changing to suit my plugin.

slushman commented 9 years ago

Hey @lazharichir, I'm actually in the middle of updating that plugin, so it may be semi-functional. :)

I'll take a look tonight and get it corrected.