2

I am trying to make my theme compatible with Co-Authors Plus plugin and show the co-authors of a post if had any. I spent about 30 minutes trying to convince myself that I understood the following code to display post author and date:

function posted_on() {
    global $post;
    $author_id=$post->post_author;
    $time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
    if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
        $time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
    }

    $time_string = sprintf( $time_string,
        esc_attr( get_the_date( 'c' ) ),
        esc_html( get_the_date() ),
        esc_attr( get_the_modified_date( 'c' ) ),
        esc_html( get_the_modified_date() )
    );


    $posted_on = sprintf(
        __( '<i class="entry-date">'. get_the_date( 'F d, Y' ) .'</i>' ),
        '<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>'
    );

    $byline = sprintf (
        __( 'by %s' ),


        '<span class="author vcard"><a class="url fn n" href="' . esc_url( 

            get_author_posts_url( $author_id )

        ) . '">' . esc_html( get_the_author_meta( 'display_name', $author_id ) ) . '</a>' . '</span>'

    );



    echo '<span class="posted-on">' . $posted_on . '</span><i><span class="byline"> ' . $byline . '</span></i>'; // WPCS: XSS OK.
} 

So I added a function:


        $byline = sprintf(
            __('by %s'),

            /*Added this*/
            if (function_exists('coauthors_posts_links')) {
                '<span class="author vcard">'.esc_html(coauthors_posts_links()).
                '</span>'
            } else {

                '<span class="author vcard"><a class="url fn n" href="'.esc_url(

                    get_author_posts_url($author_id)

                ).
                '">'.esc_html(get_the_author_meta('display_name', $author_id)).
                '</a>'.
                '</span>'
            }

        );

The function was supposed to work like...

IF THIS POST POST CONTAINS CO-AUTHORS,

DISPLAY AUTHORS AND CO-AUTHORS.

IF POST DOESN'T CONTAIN CO-AUTHORS,

SHOW THE DEFAULT AUTHOR LINK.

But that just made error. Could use a little advice.

5
  • "But that just made error" - what's the error?
    – Rup
    May 24, 2020 at 0:07
  • esc_html(coauthors_posts_links()) - it looks like coauthors_posts_links echoes by default rather than returns a string. The 'incorporating into your theme' help page calls coauthors_posts_links( null, null, null, null, false ) to get a return value. (And if the return value contains links I doubt you'd want to escape it.)
    – Rup
    May 24, 2020 at 0:10
  • The error is "Parse error: syntax error, unexpected 'if' (T_IF) in [WORDPRESS THEME DIRECTORY]/inc/template-tags.php on line 42 (here is line 42 if (function_exists('coauthors_posts_links')) {)"
    – Gusto
    May 24, 2020 at 22:26
  • Oh, I hadn’t noticed you’d written the if inside the sprintf argument list. You can’t do that - you could use a ?: ternary instead, or an if-else block to save the span HTML in a variable and use that in the sprintf.
    – Rup
    May 24, 2020 at 22:29
  • How could I do that (the "?: ternary" one)?
    – Gusto
    May 24, 2020 at 22:49

2 Answers 2

0

Your error is that you've written an if inside the sprintf argument list. You can’t do that. Instead, you could use the ternary operator, which has syntax

<condition> ? <result if true> : <result if false>

e.g.

$byline = sprintf(
        __('by %s'),
        function_exists('coauthors_posts_links')
            ? ('<span class="author vcard">' .
                    coauthors_posts_links( null, null, null, null, false ) . '</span>')
            : ('<span class="author vcard"><a class="url fn n" href="'.esc_url(
                    get_author_posts_url($author_id)
                ).'">'.esc_html(get_the_author_meta('display_name', $author_id)).
                '</a>'.'</span>')
    );

(brackets added for clarity - you probably don't need them all)
or use a separate variable for the post links and set that first using an if-else, e.g.

if (function_exists('coauthors_posts_links')) {
    $author_post_links = coauthors_posts_links( null, null, null, null, false );
} else {
    $author_post_links = '<a class="url fn n" href="' .
        esc_url( get_author_posts_url( $author_id ) ) .
        '">' . esc_html( get_the_author_meta( 'display_name', $author_id ) ) . '</a>';
}
$byline = sprintf(
    __('by %s'),
    '<span class="author vcard">' . $author_post_links . '</span>' );

As discussed in comments

  • by default coauthors_posts_links() will echo the links as well as returning them as a string, which is not what you want here. You'll need to pass $echo = false, which is the fifth argument, so we'll need to fill in the previous four with default null values.
  • I don't think you want to esc_html() the output of coauthors_post_links() so I've removed that too.
2
  • This code works but it creates duplicate links like this e.g.: Bob and Fredby Bob and Fred
    – Gusto
    May 25, 2020 at 20:50
  • My mistake - it needs the (null, null, null, null, false) arguments to coauthors_post_links that I mentioned in the question comments.
    – Rup
    May 25, 2020 at 20:52
0

You can create other function where you can store function exists code to make code easier to read like this.

   function custom_coauthors() 
    {
        $author = '';
        if (function_exists('coauthors_posts_links')) {
            $author .= '<span class="author vcard">' . esc_html(coauthors_posts_links()) . '</span>';
        } else {
            $author .= '<span class="author vcard"><a class="url fn n" href="'.esc_url( get_author_posts_url(get_the_author_meta( 'ID' ))).'">' . esc_html(get_the_author_meta('display_name', $author_id)) . '</a>' . '</span>';
        }

        return $author;
    }

     $byline = sprintf(
        __('by %s'),
        custom_coauthors()
    );

You can also use get_the_author_meta('ID') to get the id of author.

2
  • This code makes the error: Fatal error: Cannot redeclare custom_coauthors()
    – Gusto
    May 25, 2020 at 20:59
  • Make sure you don't use same function name if you have any other function with same name. Try renaming the function May 26, 2020 at 6:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.