Creating option for my plugin
-
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
- The topic ‘Creating option for my plugin’ is closed to new replies.