• Resolved Odai Athamneh

    (@heyodai)


    I’m fairly new to WordPress development. I created a simple plugin that displays author info as a sidebar widget when viewing a single post.

    I’ve been working on an update, trying to add options to the widget. Here’s the code (sorry for length):

    <?php
    ;/*
    Plugin Name: Odai Author Widget
    Plugin URI: https://odai.me/author-widget/
    Description: Widget that displays the current post author's Gravatar photo, name, website, and biography.
    Version: 1.0.0
    Author: Odai
    Author URI: https://odai.me
    License: GPLv2
    */
    
    //establish the widget
    add_action( 'widgets_init', 'odai_author_widget_register' );
    
    function odai_author_widget_register() {
    	return register_widget( 'OdaiAuthorWidget' );
    	}
    
    class OdaiAuthorWidget extends WP_Widget {
    
    	function __construct() {
    		parent::__construct('odai_author_widget_', 'Odai Author Widget', array('description' => __( "Displays post author's Gravatar photo and biographical info", 'text_domain' ),) );
    	}
    
    	function widget () {
    		$title = get_option('odai_aw_title', 'About the Author');
    		$odai_aw_pic_size = 96;
    
    		if(is_single()) {
    		?>
    		<aside id="odai-author-widget" class="widget">
    		<h3 class="widget-title"><?php echo $title; ?></h3>
    		<?php get_avatar(get_the_author_meta('user_email'), $odai_aw_pic_size); ?>
    		<br />Name:&nbsp;<?php get_the_author_meta('display_name'); ?>
    		<br />Website:&nbsp;<a href="<?php get_the_author_meta('user_url'); ?>"><?php get_the_author_meta('user_url'); ?></a>
    		<p><?php get_the_author_meta('description'); ?></p>
    		</aside>
    		<?php
    		}
    	}
    
    	function update($newinstance,$oldinstance) {
    		$instance =  $oldinstance;
        		$instance['odai_aw_title'] =  $newinstance['odai_aw_title'];
        		return $instance;
    	}
    
    	function form ($config) {
    		?>
        		<label for='<?php echo $this->get_field_id("title");  ?>'>
        		<p>Title: <input type="text"  value="<?php echo $config['title']; ?>" name="<?php echo $this->get_field_name('title'); ?>" id="<?php echo $this->get_field_id('title') ?>"></p>
        		</label>
    		<?php
    	}
    }
    
    //adding options to the database
    register_activation_hook( __FILE__, 'odai_aw_add_options' );
    register_deactivation_hook( __FILE__, 'odai_aw_remove_options' );
    
    function odai_aw_add_options () {
    		update_option('title', 'About the Author');
    	}
    
    function odai_aw_remove_options () {
    		delete_option('title');
    	}

    So right now, I’m just trying to get the title option working. The form is there, I can save a new title (changes in the widget admin area), but on the website itself, it continues to display the default “About the Author” text. What am I doing wrong?

    Also, feel free to suggest improvements to any aspect of the code – I’m sure it’s far from perfect, as I’ve been learning as I go.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    You don’t directly get values for the widget (that were input on the admin panel) from the options table. The widget method is passed two parameters. The first is arguments from the side bar like before and after title, etc. You should extract() this and use the resulting variables where appropriate. Users will be miffed if you ignore their sidebar arguments.

    The second is the $instance array you returned in the update() method. This is where you should be getting the values input on the admin panel.

    Thread Starter Odai Athamneh

    (@heyodai)

    Hey bcworks, thanks for your reply!

    I tried changing the title-getting part from
    $title = get_option('odai_aw_title', 'About the Author');
    to
    $title = extract(array('odai_aw_title' => 'About the Author'));

    I know that’s wrong, because I’m getting a value of either 1 or 0 returned from it. I tried finding info about the extract() function on the Codex, but this was all I found.

    Regarding the before title part, I just need to put these before and after the title, right?

    <?php echo $before_title ?>
    //code
    <?php echo $after_title ?>

    Again, thanks for your help.

    Moderator bcworkz

    (@bcworkz)

    extract() is a php function. Check php.net for more info. It extracts array elements into variables named the same as the keys, with corresponding values. I’m not sure all of what’s in the first parameter’s array, you can do a vardump($parm); die(); to see what’s inside. You’ll need to view the source code of the output in your browser for it to make sense. If there’s a lot of page output as well, it’ll take some practice to learn how to find it amongst all the other code.

    You’re right about the before and after if the keys were ‘before_title’ and ‘after_title’. The vardump() will conform this, but I do believe you are correct about this part.

    Finally, your title value is in the second parameter, not the first. The first part of your widget() function should look something like this:

    function widget( $args, $instance ) {
       extract( $args );
       $title = $before_title . $instance['odai_aw_title'] . $after_title;

    Thread Starter Odai Athamneh

    (@heyodai)

    Thank you so much! I read about extract () on PHP.net, and with your advice, was able to retrieve the title setting.

    Now I need to add the rest of the options, but I think I understand the underlying concept, so I’ve got it.

    I’m going to go ahead and set this topic to [resolved]. Thanks again.

    try to change this

    add_action( 'widgets_init', 'odai_author_widget_register' );
    
    function odai_author_widget_register() {
    	return register_widget( 'OdaiAuthorWidget' );
    	}
    
    class OdaiAuthorWidget extends WP_Widget {
    
    	function __construct() {
    		parent::__construct('odai_author_widget_', 'Odai Author Widget', array('description' => __( "Displays post author's Gravatar photo and biographical info", 'text_domain' ),) );
    	}

    to this

    add_action( 'widgets_init', 'odai_author_widget_register' );
    
    function odai_author_widget_register() {
    	return register_widget( 'WP_Widget_Odai_Author_Widget' );
    	}
    
    class WP_Widget_Odai_Author_Widget extends WP_Widget {
    
    	function __construct() {
    		parent::__construct('odai_author_widget_', 'Odai Author Widget', array('description' => __( "Displays post author's Gravatar photo and biographical info", 'text_domain' ),) );
    	}

    sorry I have found a mistake there is an underscore after that shouldnt be there ( odai_author_widget_ )

    change to this

    add_action( 'widgets_init', 'odai_author_widget_register' );
    
    function odai_author_widget_register() {
    	return register_widget( 'WP_Widget_Odai_Author_Widget' );
    	}
    
    class WP_Widget_Odai_Author_Widget extends WP_Widget {
    
    	function __construct() {
    		parent::__construct('odai_author_widget', 'Odai Author Widget', array('description' => __( "Displays post author's Gravatar photo and biographical info", 'text_domain' ),) );
    	}
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Widget Options Saved, But Not Displaying On Site’ is closed to new replies.