difference between $name and $name() in this code?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
baseballplayr
Forum Newbie
Posts: 4
Joined: Wed Nov 19, 2008 9:36 pm

difference between $name and $name() in this code?

Post by baseballplayr »

Hi, I have tried altering the wordpress plugin co-authors. The main file is co-authors.php and I was able to alter it all except for the email address. Whenever I use:

Code: Select all

echo '<a href="mailto:emailaddy1@domain.com">' . $name . '</a>';
I get that email address yet the name that appears is the_author_link (should show what the_author_link points to which is the actaul first and last name of the author).

Whenever I use:

Code: Select all

echo '<a href="mailto:emailaddy1@ncsu.edu">' . $name() . '</a>';
The actual author name(what should appear jumps infront of the <a> tag and nothing where the name should be => no email link to click on.

$name(); //This alone prints the authors name (the_author_link), just not in the email tag...why?
The relevant code is below with the function code that is not working highlighted in yellow(around line 200). Any help is greatly appreciated!! Thank you!

Code: Select all

<?php
/*
Plugin Name: Co-Authors
Plugin URI: http://wordpress.org/extend/plugins/co-authors/
Description: Allows multiple authors to be assigned to a post. Co-authored posts appear on a co-author's posts page and feed. New template tags allow listing of co-authors. Editors may assign co-authors to a post via the 'Post Author' box.  <em>This plugin is developed at <a href="http://www.shepherd-interactive.com/" title="Shepherd Interactive specializes in web design and development in Portland, Oregon">Shepherd Interactive</a> for the benefit of the community.</em>
Version: 1.0 (beta 5)
Author: Weston Ruter
Author URI: http://weston.ruter.net/
Copyright: 2008, Weston Ruter, Shepherd Interactive
 
Inspired by 'Multiple Authors' plugin by Mark Jaquith (2005).
 
GNU General Public License, Free Software Foundation <http://creativecommons.org/licenses/GPL/2.0/>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
 
define('COAUTHORS_DEFAULT_BEFORE', '');
define('COAUTHORS_DEFAULT_BETWEEN', '<br /> ');
define('COAUTHORS_DEFAULT_BETWEEN_LAST', ' <br /> ');
define('COAUTHORS_DEFAULT_AFTER', '');
define('COAUTHORS_VERSION', '1.0beta2');
 
if(floatval(get_option('coauthors_version')) < 1.0)
    coauthors_updatedb();
 
add_option('coauthors_version', COAUTHORS_VERSION);
 
function coauthors_updatedb(){
    global $wpdb;
    //Update old versions of plugin to use new hidden meta key
    $wpdb->query("UPDATE $wpdb->postmeta SET meta_key = '_coauthor' WHERE meta_key = 'coauthor';");
}
register_activation_hook(__FILE__, 'coauthors_updatedb');
 
# Load up the localization file if we're using WordPress in a different language
# Place l10n files named like "co-authors-[value in wp-config].mo" in the "localization" folder
load_plugin_textdomain('co-authors', PLUGINDIR . '/co-authors/localization');
 
function coauthors_user_has_cap($allcaps, $caps, $args){
    $current_user = wp_get_current_user();
    
    $relatedCaps = array(
        'edit_post',
        'edit_others_posts',
        'publish_posts',
        'edit_page',
        'edit_others_pages',
        'publish_pages',
        'edit_pages',
        'edit_posts',
        'edit_published_pages',
        'edit_published_posts',
        //delete_post
        //delete_page
        //moderate_comments
    );
    
    foreach($caps as $cap){
        if(!in_array($cap, $relatedCaps))
            return $allcaps;
    }
    
    //Get the post in question
    $postID = $args[2] ? $args[2] : $_POST['ID'];
    $post = get_post($postID);
    $post_status = $_POST['post_status'] ? $_POST['post_status'] : $post->post_status;
    
    //Disallow if post is published and they are a contributor
    if(!in_array($post_status, array('draft','pending')) && (($post->post_type == 'post' && !$allcaps['edit_published_posts']) || ($post->post_type == 'page' && !$allcaps['edit_published_pages']))){
        return $allcaps;
    }
    
    $coauthors = get_post_custom_values('_coauthor', $postID);
    if(!empty($coauthors) && in_array($current_user->ID, $coauthors)){
        foreach($relatedCaps as $cap){
            $allcaps[$cap] = true;
        }
    }
    
    return $allcaps;
}
add_filter('user_has_cap', 'coauthors_user_has_cap', 10, 3);
 
 
//Modify the author query posts SQL to include posts co-authored
function coauthors_posts_join_filter($join){
    global $wpdb,$wp_query;
    if(is_author()){
        $join .= " LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.id "
               . "AND $wpdb->postmeta.meta_key = '_coauthor' "
               . "AND $wpdb->postmeta.meta_value = '" . $wp_query->query_vars['author'] . "' "; //this condition removes need for DISTINCT
    }
    return $join;
}
add_filter('posts_join', 'coauthors_posts_join_filter');
function coauthors_posts_where_filter($where){
    global $wpdb;
    if(is_author())
        $where = preg_replace('/(\b(?:' . $wpdb->posts . '\.)?post_author\s*=\s*(\d+))/', '($1 OR (' . $wpdb->postmeta . '.meta_value = \'$2\'))', $where, 1); #' . $wpdb->postmeta . '.meta_id IS NOT NULL AND 
    return $where;
}
add_filter('posts_where', 'coauthors_posts_where_filter');
 
//function coauthors_posts_query_filter($sql){ //this was needed when meta_value = query_vars[author] was not tested in the LEFT JOIN ON clause
//  return preg_replace("{^(\s*SELECT\b(?!\s*DISTINCT))}i", '$1 DISTINCT', $sql);
//}
//add_filter('posts_request', 'coauthors_posts_query_filter');
 
 
class CoAuthorsIterator {
    var $position = -1;
    var $original_authordata;
    var $authordata_array;
    var $count;
    
    function CoAuthorsIterator($postID = 0){
        global $post, $authordata, $wpdb;
        $postID = (int)$postID;
        if(!$postID && $post)
            $postID = (int)$post->ID;
        if(!$postID)
            trigger_error(__('No post ID provided for CoAuthorsIterator constructor. Are you not in a loop or is $post not set?', 'co-authors')); //return null;
        if(empty($authordata))
            $authordata = get_userdata($wpdb->get_var("SELECT post_author FROM $wpdb->posts WHERE ID = $postID;"));
        
        //Gather all of the co-authors
        $this->authordata_array = array($authordata);
        $userids = get_post_meta($post->ID, '_coauthor', false);
        if(!empty($userids)){
            if(!is_array($userids))
                $userids = array($userids);
            foreach($userids as $user_id){
                $a = get_userdata($user_id);
                if(!empty($a)) //in case the user has been deleted while plugin was deactivated
                    $this->authordata_array[] = $a;
            }
        }
        $this->count = count($this->authordata_array);
    }
    
    function iterate(){
        global $authordata;
        $this->position++;
        
        //At the end of the loop
        if($this->position > $this->count-1){
            $authordata = $this->original_authordata;
            $this->position = -1;
            return false;
        }
        
        //At the beginning of the loop
        if($this->position == 0 && !empty($authordata))
            $this->original_authordata = $authordata;
        
        $authordata = $this->authordata_array[$this->position];
        
        return true;
    }
    
    function get_position(){
        if($this->position === -1)
            return false;
        return $this->position;
    }
    function is_last(){
        return  $this->position === $this->count-1;
    }
    function is_first(){
        return $this->position === 0;
    }
    function count(){
        return $this->count;
    }
    function get_all(){
        return $this->authordata_array;
    }
}
 
[color=#FFFF40]//Helper function for the following new template tags
function coauthors__echo($name, $email, $description, $between, $betweenLast, $before, $after){
    $i = new CoAuthorsIterator();
    echo $before;
    if($i->iterate())
        echo '<a href="mailto:emailaddy1@ncsu.edu">' . $name() . '</a>';
    while($i->iterate()){
        echo $i->is_last() ? $betweenLast : $between;
        echo '<a href="mailto:emailaddy2@ncsu.edu">' . $name() . '</a>';
    }
    echo $after;[/color]
}
 
 
//Provide co-author equivalents to the existing author template tags
function coauthors_description($between = null, $betweenLast = null, $before = null, $after = null){
    if($between === NULL)
        $between = __(COAUTHORS_DEFAULT_BETWEEN, 'co-authors');
    if($betweenLast === NULL)
        $betweenLast = __(COAUTHORS_DEFAULT_BETWEEN_LAST, 'co-authors');
    if($before === NULL)
        $before = COAUTHORS_DEFAULT_BEFORE; //__(COAUTHORS_DEFAULT_BEFORE, 'co-authors');
    if($after === NULL)
        $after = COAUTHORS_DEFAULT_AFTER; //__(COAUTHORS_DEFAULT_AFTER, 'co-authors');
    coauthors__echo( 'the_author_link','the_author_email','the_author_description', $between, $betweenLast, $before, $after);
}
koen.h
Forum Contributor
Posts: 268
Joined: Sat May 03, 2008 8:43 am

Re: difference between $name and $name() in this code?

Post by koen.h »

if $name = 'baseballplayr' then $name() will call the function baseballplayr().

Isn't there a template tag you can use?
baseballplayr
Forum Newbie
Posts: 4
Joined: Wed Nov 19, 2008 9:36 pm

Re: difference between $name and $name() in this code?

Post by baseballplayr »

I never saw one. I'm not very fluent in php, I am just going off of basic coding principles learns through C language and stuff. But yes, if I were the author, $name() would display baseballplayr. If I use $name, it prints out the_author_link, which that typed alone will give you my name. Does this mean $name should be a function the prints the_author_link? Thanks you for your reply :)
koen.h
Forum Contributor
Posts: 268
Joined: Sat May 03, 2008 8:43 am

Re: difference between $name and $name() in this code?

Post by koen.h »

baseballplayr wrote:I never saw one. I'm not very fluent in php, I am just going off of basic coding principles learns through C language and stuff. But yes, if I were the author, $name() would display baseballplayr. If I use $name, it prints out the_author_link, which that typed alone will give you my name. Does this mean $name should be a function the prints the_author_link? Thanks you for your reply :)
http://php.mirrors.ilisys.com.au/manual ... ctions.php

Looked at http://codex.wordpress.org/Template_Tag ... uthor_link and it displays a link so if you call that function within an anchor element that wouldn't make much sense. Wouldn't the_author be more appropriate on line 216?
baseballplayr
Forum Newbie
Posts: 4
Joined: Wed Nov 19, 2008 9:36 pm

Re: difference between $name and $name() in this code?

Post by baseballplayr »

What exactly do you suggest with line 216?
that line calls the function above it giving it the name email and description of the author
'the_author_link','the_author_email','the_author_description'
Could I call that function and what it holds in a variable and use that(how?)?
Thanks again.
koen.h
Forum Contributor
Posts: 268
Joined: Sat May 03, 2008 8:43 am

Re: difference between $name and $name() in this code?

Post by koen.h »

What do you use in your template to get the co-author list? (I mean what template tag)
baseballplayr
Forum Newbie
Posts: 4
Joined: Wed Nov 19, 2008 9:36 pm

Re: difference between $name and $name() in this code?

Post by baseballplayr »

I was using coauthor_description. I missed around with the iterate function and worked it out. I left that file the same and just iterated the original author info each line. Thank you for all your help, you pointed me in the right direction. Thanks again!
Post Reply