• I’m trying to write my first plugin and am running into several problems. The first one I want to tackle is how to echo a line to the head of a post page. The line should look like this “<link rel=”author” “” />” and instead it looks like this “<link rel=”author” href=”<?php echo $curauth->google; ?>” />” when I view the page source.

    Here’s some of the code I’ve been using:

    /*Get current author
    
    if (is_admin()) require_once(ABSPATH . 'wp-includes/pluggable.php');
    
    if(isset($_GET['author_name'])) : $curauth = get_userdatabylogin($author_name);
    else : $curauth = get_userdata($author);
    endif;
    
    Add Google profile to HEAD
    */
    
    add_action('wp_head', 'add_google_rel_author');
    function add_google_rel_author() {
    echo '<link rel="author" href="<?php echo $curauth->google; ?>" />';
    }

    The first part is commented out because it returns an error when I enable it – Function get_userdata is undefined.

    What am I doing wrong?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Your echo statement seem to be wrong. Replace
    echo '<link rel="author" href="<?php echo $curauth->google; ?>" />';
    with
    echo '<link rel="author" href="' . $curauth->google . '" />';

    Thread Starter atlcr

    (@atlcr)

    I tried that and I’m getting errors.

    Notice: Undefined variable: curauth in /home/user/wp-content/plugins/me-wpafg/me-wpafg1.php on line 44
    
    Notice: Trying to get property of non-object in /home/user/wp-content/plugins/me-wpafg/me-wpafg1.php on line 44

    Also, it prints out the following in the head.

    <link rel="author" href="" />

    So I’m thinking something is going wrong when I try to call author info?

    Move the if statement that defines $curauth to the start of your callback function.

    Thread Starter atlcr

    (@atlcr)

    @halferdev – Can you elaborate? Maybe with some code examples?

    As the notice says: $curauth has not been specified and therefore cannot be accessed. There are multiple ways to get user (meta) data so here’s one you might play with

    add_action( 'wp_head', 'add_google_rel_author' );
    
    function add_google_rel_author() {
    
      global $post;
    
      if( isset( $_GET['author_name'] ) && !empty( $_GET['author_name'] ) ) {
    
        $author_name  = strip_tags( $_GET['author_name'] );
    
        $User         = get_user_by( 'login', $author_name );
    
        $curauth      = isset( $User->ID ) ? $User->ID : null;
    
      }
      else {
    
        $curauth      = isset( $post->post_author ) ? $post->post_author : null;
    
      }
    
      if( $curauth !== null ) {
    
        // You might have to check which key your 'googleplus' meta data is using?!
        $googlePlus   = get_user_meta( $curauth, 'googleplus' );
    
        // If no google+ profile URL has been set use the normal author URL (if it exists)
        $curauthlink  = !empty( $googlePlus ) ? $googlePlus[0] : get_author_posts_url( $curauth );
    
        if( $curauthlink !== '' ) {
    
          echo '<link rel="author" href="' . $curauthlink . '" />' . "\n";
    
        }
    
      }
    
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘php code added to head not executing’ is closed to new replies.