WordPress.org

Ready to get started?Download WordPress

Forums

JSON API
Adding email address (user_email) to author object (for gravatars) (1 post)

  1. Steve
    Member
    Posted 2 years ago #

    Hey, I just wanted to let everyone know that you can easily add the email address to the authors object via the /models/author.php file. This is very useful for finding the Gravatar image for any authors.

    <?php
    
    class JSON_API_Author {
    
      var $id;          // Integer
      var $slug;        // String
      var $name;        // String
      var $first_name;  // String
      var $last_name;   // String
      var $nickname;    // String
      var $url;         // String
      var $description; // String
      <strong>var $email_address; //String</strong>
    
      // Note:
      //   JSON_API_Author objects can include additional values by using the
      //   author_meta query var.
    
      function JSON_API_Author($id = null) {
        if ($id) {
          $this->id = (int) $id;
        } else {
          $this->id = (int) get_the_author_meta('ID');
        }
        $this->set_value('slug', 'user_nicename');
        $this->set_value('name', 'display_name');
        $this->set_value('first_name', 'first_name');
        $this->set_value('last_name', 'last_name');
        $this->set_value('nickname', 'nickname');
        $this->set_value('url', 'user_url');
        $this->set_value('description', 'description');
        <strong>$this->set_value('email_address', 'user_email');</strong>
        $this->set_author_meta();
        //$this->raw = get_userdata($this->id);
      }
    
      function set_value($key, $wp_key = false) {
        if (!$wp_key) {
          $wp_key = $key;
        }
        $this->$key = get_the_author_meta($wp_key, $this->id);
      }
    
      function set_author_meta() {
        global $json_api;
        if (!$json_api->query->author_meta) {
          return;
        }
        $protected_vars = array(
          'user_login',
          'user_pass',
          <strong>//'user_email',</strong>
          'user_activation_key'
        );
        $vars = explode(',', $json_api->query->author_meta);
        $vars = array_diff($vars, $protected_vars);
        foreach ($vars as $var) {
          $this->set_value($var);
        }
      }
    
    }
    
    ?>

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags