Search code examples
phpwordpressadd-filter

WP add_filter using concatenation and echo to call WP function - WP function displays as text only


Attempting to add content to existing posts via an add_filter. Filter includes html and three WP function calls using echo. Add_filter works but displays the echoed functions as text only. Appreciate any help, direction or advice.

add_filter ('the_content', 'insertAuthorMetaData');
function insertAuthorMetaData($content) {
if(is_single()) {
$content.= '<div id="content" class="author-meta-info">';
$content.= '<hr/>';
$content.= '<h4 class="avatar-in-loop">';
$content.= '<h4 class="avatar-in-loop">';
$content.= '<h4 class="avatar-in-loop">';
$content.= '</h4>';
$content.= '<dl><dt></dt><dd>';
$content.= echo the_author_meta( 'description' );
$content.= ' </dd></dl><hr />';
$content.= '</div>';
 }
return $content;
}

thank you. bobp


Solution

  • You can't echo inside a filter. A filter's job is to modify the content rather than output something. Also you're using the_author_meta() which outputs the meta when you need a function that will return it. You need to use get_the_author_meta() instead.

    Change this:

    $content.= echo the_author_meta( 'description' );
    

    To:

    $content .= get_the_author_meta( 'description' );