WordPress not showing my setting in option.php

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();
});

Related posts

Leave a Reply

2 comments

  1. When registering fields for options you have to hook in admin_menu hook within your __construct method.

    add_action('admin_menu', array($this, 'register_setting_and_fields'));
    

    I also add the admin_init hook in __construct method as well so it looks self contained.

    admin_init need admin_menu to hook in. So in your case it should be

    add_action('admin_init', array($this, 'add_menu_page'));
    

    Try this code:

    class PS_Interakt{
    
        public $options;
    
        public function __construct(){
            $this->options = get_option('interakt_plugin_options');
    
            add_action('admin_init', array($this, 'add_menu_page'));
            add_action('admin_menu', array($this, 'register_setting_and_fields'));
        }
    
        public function add_menu_page() {
            add_options_page('Interakt Options', 'Interakt Options', 'administrator', '__FILE__', array($this, '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']}'/>";
        }
    }
    
    if ( is_admin() ) {
        new PS_Interakt();
    }
    

    is_admin() will ensure that you’re in Admin area (Dashboard section)

    Better read docs

  2. 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:

    add_action( 'plugins_loaded', function(){
        new PS_Interakt();
    });
    

    Then inside the constructor, call your working hooks:

    public function __construct(){
        $this->options = get_option('interakt_plugin_options');
        add_action( 'admin_init', array( $this, 'init_admin' ) );
        add_action('admin_menu', array( $this, 'add_menu_page' ) ); 
    }
    

    The new init_admin method does the registering:

    public function init_admin()
    {
        $this->register_setting_and_fields();
    }
    

    Then, you have errors in your add_options_page, add_settings_section and add_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 for my_opts, for example.

    Finally, it’s much more readable if we use printf and sprintf to build strings from PHP variables, function calls and conditional values:

    printf(
        "<input name = 'interakt_plugin_options[interakt_app_key]' type='text' value='%s'/>",
        $this->options['interakt_app_key']
    );