php code added to head not executing (6 posts)

  1. atlcr
    Posted 3 years ago #

    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);
    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?

  2. Chris
    Posted 3 years ago #

    Your echo statement seem to be wrong. Replace
    echo '<link rel="author" href="<?php echo $curauth->google; ?>" />';
    echo '<link rel="author" href="' . $curauth->google . '" />';

  3. atlcr
    Posted 3 years ago #

    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?

  4. halferdev
    Posted 3 years ago #

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

  5. atlcr
    Posted 3 years ago #

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

  6. Chris
    Posted 3 years ago #

    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";

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.