• Hey everybody,

    I am developing a plugin for WordPress using MVC Architecture(With the help of WordPress plugin boilerplate) and I am trying to register a settings and output its value in a certain field.

    the Variable is being output but when i try to equal the variable to the get_option function, its not outputting anything.

    I am new in the plugin developing zone so I need your help guys.

    This is my Admin Class:

    
    
    class Sticky_Contact_Admin {
    
    	/**
    
    	 * The options name to be used in this plugin
    
    	 *
    
    	 * @since  	1.0.0
    
    	 * @access 	private
    
    	 * @var  	string 		$option_name 	Option name of this plugin
    
    	 */
    
    	private $option_name = 'sticky_contact';
    
    	public $default_phone_number;
    	
    
    	/**
    
    	 * The ID of this plugin.
    
    	 *
    
    	 * @since    1.0.0
    
    	 * @access   private
    
    	 * @var      string    $plugin_name    The ID of this plugin.
    
    	 */
    
    	private $plugin_name;
    
    	/**
    
    	 * The version of this plugin.
    
    	 *
    
    	 * @since    1.0.0
    
    	 * @access   private
    
    	 * @var      string    $version    The current version of this plugin.
    
    	 */
    
    	private $version;
    
    	/**
    
    	 * Initialize the class and set its properties.
    
    	 *
    
    	 * @since    1.0.0
    
    	 * @param      string    $plugin_name       The name of this plugin.
    
    	 * @param      string    $version    The version of this plugin.
    
    	 */
    
    	public function __construct( $plugin_name, $version ) {
    
    		$this->plugin_name = $plugin_name;
    
    		$this->version = $version;
    
    	}
    
    	/**
    
    	 * Register the stylesheets for the admin area.
    
    	 *
    
    	 * @since    1.0.0
    
    	 */
    
    	public function enqueue_styles() {
    
    		/**
    
    		 * This function is provided for demonstration purposes only.
    
    		 *
    
    		 * An instance of this class should be passed to the run() function
    
    		 * defined in Sticky_Contact_Loader as all of the hooks are defined
    
    		 * in that particular class.
    
    		 *
    
    		 * The Sticky_Contact_Loader will then create the relationship
    
    		 * between the defined hooks and the functions defined in this
    
    		 * class.
    
    		 */
    
    		wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/sticky-contact-admin.css', array(), $this->version, 'all' );
    
    	}
    
    	/**
    
    	 * Register the JavaScript for the admin area.
    
    	 *
    
    	 * @since    1.0.0
    
    	 */
    
    	public function enqueue_scripts() {
    
    		/**
    
    		 * This function is provided for demonstration purposes only.
    
    		 *
    
    		 * An instance of this class should be passed to the run() function
    
    		 * defined in Sticky_Contact_Loader as all of the hooks are defined
    
    		 * in that particular class.
    
    		 *
    
    		 * The Sticky_Contact_Loader will then create the relationship
    
    		 * between the defined hooks and the functions defined in this
    
    		 * class.
    
    		 */
    
    		wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/sticky-contact-admin.js', array( 'jquery' ), $this->version, false );
    
    	}
    
    	/**
    
    	 * Add an options page under the Settings submenu
    
    	 *
    
    	 * @since  1.0.0
    
    	 */
    
    	public function add_options_page() {
    
    	
    
    		$this->plugin_screen_hook_suffix = add_options_page(
    
    			__( 'Sticky Contact Settings', 'sticky-contact' ),
    
    			__( 'Sticky Contact', 'sticky-contact' ),
    
    			'manage_options',
    
    			$this->plugin_name,
    
    			array( $this, 'display_options_page' )
    
    		);
    
    	
    
    	}
    
    	
    
    	/**
    
    	 * Render the options page for plugin
    
    	 *
    
    	 * @since  1.0.0
    
    	 */
    
    	public function display_options_page() {
    
    		include_once 'partials/sticky-contact-admin-display.php';
    
    	}
    
    	
    
    	public function register_setting(){
    
    	// Add a General section
    
    		add_settings_section(
    
    			$this->option_name . '_general',
    
    			__( 'General', 'sticky-contact' ),
    
    			array( $this, $this->option_name . '_general_cb' ),
    
    			$this->plugin_name
    
    		);
    
    		
    	// Adds Fields to General Section
    
    		add_settings_field(
    
    			$this->option_name . '_position',
    
    			__( 'Sticky Bar Position', 'sticky-contact' ),
    
    			array( $this, $this->option_name . '_position_cb' ),
    
    			$this->plugin_name,
    
    			$this->option_name . '_general',
    
    			array( 'label_for' => $this->option_name . '_position' )
    
    		);
    
    		
    
    		
    
    		
    
    		add_settings_field(
    
    			$this->option_name . '_default_phone',
    
    			__( 'Default Phone Number', 'sticky-contact' ),
    
    			array( $this, $this->option_name . '_phone_cb' ),
    
    			$this->plugin_name,
    
    			$this->option_name . '_general',
    
    			array( 'label_for' => $this->option_name . '_default_phone' )
    
    		);
    
    		
    
    		register_setting( $this->plugin_name, $this->option_name . '_position', array( $this, $this->option_name . '_sanitize_position' ) );
    
    		register_setting( $this->plugin_name, $this->option_name . '_phone');
    
    		$this->default_phone_number = get_option($this->plugin_name . '_phone');
    	}
    
    	
    
    	public function sticky_contact_general_cb() {
    
    		echo '<p>' . __( 'Please change the settings accordingly.', 'sticky-contact' ) . '</p>';
    
    		if ( ! is_plugin_active( 'parent-plugin/parent-plugin.php' ) and current_user_can( 'activate_plugins' ) ) {
    
    			// Stop activation redirect and show error
    
    			echo 'We would recommend you to install one of this plugins';
    
    		}
    
    	}
    
    	
    
    	
    
    	public function sticky_contact_position_cb() {
    
    		?>
    
    			<fieldset>
    
    				<label>
    
    					<input type="radio" name="<?php echo $this->option_name . '_position' ?>" id="<?php echo $this->option_name . '_position' ?>" value="bottom">
    
    					<?php _e( 'Beginning of Page', 'sticky-contact' ); ?>
    
    				</label>
    
    				<br>
    
    				<label>
    
    					<input type="radio" name="<?php echo $this->option_name . '_position' ?>" value="top">
    
    					<?php _e( 'Bottom of Page', 'sticky-contact' ); ?>
    
    				</label>
    
    			</fieldset>
    
    		<?php
    
    	}
    
    	
    
    	public function sticky_contact_phone_cb(){
    
    		echo '<input type="text" value="' . $this->default_phone_number . '" name="' . $this->option_name . '_default_phone' . '" id="'. $this->option_name . '_default_phone' . '"> '. __( 'phone', 'sticky-contact' );
    
    	}
    
    	
    
    	public function sticky_contact_day_cb() {
    
    		echo '<input type="text" name="' . $this->option_name . '_day' . '" id="' . $this->option_name . '_day' . '"> '. __( 'days', 'sticky-contact' );
    
    	}
    
    	
    
    	
    
    }
    
    

    As you can see, in the Register_Settings function, at the end I am registering an option called sticky_contact_phone and than right after that, I store it in a variable called default_phone_number. When I try to output this Var in sticky_contact_phone_cb function its not outputting the stored value in the db.

    Good to mention that I also tried to equal the Var to a random number in order to check if Its called right and its worked so my problem is in outputting a value from the db.

    What have I done wrong?
    Please Help

Viewing 3 replies - 1 through 3 (of 3 total)
  • You’re not using PHP classes correctly. PHP classes don’t literally *store* their variables so that you can access them across requests. In your sticky_contact_phone_cb() function you echo $this->default_phone_number, but it’s not being assigned anywhere.

    You need to use get_option to retrieve the option from the database. You’re doing that inside the register_setting function, but that function isn’t hooked anywhere. And even if it was, it should be hooked to admin_init, which means that the variable won’t be set to the value of get_option on the front-end.

    Before worrying about MVC patterns and stuff like that review the Settings API documentation.

    Thread Starter tszdev

    (@tszdev)

    Yes it was.

    The variable was assigned at the beginning

    
    public $default_phone_number;
    

    Also the register_settings function is hooked from another class file under admin_init hook like this:

    
    $this->loader->add_action( 'admin_init', $plugin_admin, 'register_setting' );
    
    

    You need to understand that my problem is not in the declaration of the Var but in getting the option from the db because if I declare the var as equal to 1234 It outputs it.

    for instance when I do this:

    
    $this->default_phone_number = 1234;
    

    everything works good but when I do this(as I posted in the original code):

    
    $this->default_phone_number = get_option($this->plugin_name . '_phone');
    

    nothing happens

    Yes it was.

    The variable was assigned at the beginning

    That’s not assigning it, that’s declaring it.

    The only place in your code that you are assigning get_option() to the variable is in register_setting(), which you tell me is hooked to admin_init. If you try to access the variable from the class on the front end you’re not going to get it because admin_init has not run which means that the variable is still empty.

    EDIT: Also, I see that the name attribute of the input is:

    this->option_name . '_default_phone'
    

    But the setting you’re registering is:

    $this->option_name . '_phone'
    

    And then the name you’re passing to get_option() is

    $this->plugin_name . '_phone'
    

    They’re all different. Is plugin_name even the same as option_name?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Creating option for my plugin’ is closed to new replies.