Open lazharichir opened 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
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
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
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.
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.
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.
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:
And in my class-pluginname-admin.php, I have my function:
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?!