I have to create a plugin in which user will add that plugin it will add a page to take app_id
from the user and then I will use the App ID in my JavaScript widget. I have registered settings, diplay_option_page
and tried to retrieve the value but still here is some issue that my input field is not showing up on my setting page.
Any idea? This is my first time with WordPress and PHP.
I was following this tutorial and this is my code:
<?php
/*
Plugin Name: Interakt for WordPress
Plugin URI: http://interakt.co
Description: Integrate the <a href="http://interakt.co">Interakt</a> CRM and messaging app into your WordPress website.
Author: Peeyush Singla
Author URI: https://twitter.com/peeyush_singla
Version: 0.1
*/
class PS_Interakt{
public $options;
public function __construct(){
$this->options = get_option('interakt_plugin_options');
$this->register_setting_and_fields();
}
public function add_menu_page(){
add_options_page('Interakt Options', 'Interakt Options', 'administrator', __FILE__, array('PS_Interakt', 'display_options_page'));
}
public function display_options_page()
{
?>
<div class="wrap">
<h2>Interakt Options</h2>
<form method="post",action="option.php">
<?php settings_fields('interakt_plugin_options');?>
<?php do_settings_sections(__FILE__)?>
<p class="submit">
<input name="submit" type="submit" class="button-primary" value="Save" >
</p>
</form>
</div>
<?php
}
public function register_setting_and_fields(){
register_setting('interakt_plugin_options', 'interakt_plugin_options' );
add_settings_section('interakt_main_section', 'App Key Setting', array($this,'interakt_main_section_cb'), __FILE__);
add_settings_field('interakt_app_key', "Interakt App Key", array('PS_Interakt','interakt_app_key_setting'), __FILE__, 'interakt_main_section' );
}
public function interakt_main_section_cb(){
}
public function interakt_app_key_setting(){
echo "<input name = 'interakt_plugin_options[interakt_app_key]' type='text' value='{$this->options['interakt_app_key']}'/>";
}
}
add_action('admin_menu', function(){
PS_Interakt::add_menu_page();
});
add_action('admin_init', function(){
new PS_Interakt();
});
When registering fields for options you have to hook in
admin_menu
hook within your__construct
method.I also add the
admin_init
hook in__construct
method as well so it looks self contained.admin_init
needadmin_menu
to hook in. So in your case it should beTry this code:
is_admin()
will ensure that you’re in Admin area (Dashboard section)Better read docs
Your first problem is with OOP concepts, start reading some tutorials asap 😉
Without using static methods, you should only instantiate the class once wrapping everything inside the hook
plugins_loaded
:Then inside the constructor, call your working hooks:
The new
init_admin
method does the registering:Then, you have errors in your
add_options_page
,add_settings_section
andadd_settings_field
functions. Check the documentation for each one (Codex/Function_Reference), you’re using the parameter$page
wrong, it has to be your own page name. So, change all__FILE__
occurrences formy_opts
, for example.Finally, it’s much more readable if we use
printf
andsprintf
to build strings from PHP variables, function calls and conditional values: