WordPress.org

Forums

[resolved] Custom comment_author_url_link output (11 posts)

  1. dropshot
    Member
    Posted 11 months ago #

    I'm trying to change the comment_author_url_link output. I want to display the comment author url, but only if one exists.

    <?php comment_author_url_link('linktext', 'before', 'after'); ?>

    This one is supposed to display the url id one is provided. So if I use:

    <?php comment_author_url_link('', 'Website:', ''); ?>

    "Website:" is still displayed even if a url is not provided. And if empty it still generates a anchor tag.

    <a rel="external" href="" ></a>

    Is there someway to edit this output?

  2. Jose Castaneda
    THEME COFFEE MONKEY
    Posted 11 months ago #

    You can try adding a filter:

    add_filter( 'get_comment_author_url_link', 'custom_comment_author_url_link' );
    function custom_comment_author_url_link( $linktext, $before = '', $after = '' ){
        /** your code goes here **/
    }

    The core function is in the comment-template file.

  3. dropshot
    Member
    Posted 11 months ago #

    Thank you Jose.

    I found the comment-template file. I just don't know how to edit it.

    The adding a filter advise is a big help. But still I don't know how to edit it. I'm not sure what the original if statement does.

    if ( '/' == substr($display, -1) )
    	        $display = substr($display, 0, -1);
    	        $return = "$before<a href='$url' rel='external'>$display</a>$after";

    Another push in the right direction please...

  4. Jose Castaneda
    THEME COFFEE MONKEY
    Posted 11 months ago #

    No, do not edit the core files.

    What you do is add the filter in your themes functions file or plugin.

    Something like:

    add_filter( 'get_comment_author_url_link', 'custom_comment_author_url_link' );
    function custom_comment_author_url_link( $linktext, $before = '', $after = '' ){
        $url = get_comment_author_url();
        if ( $url == '' ) return;
    	$display = ( $linktext != '' ) ? $linktext : $url;
    	$display = str_replace( 'http://www.', '', $display );
    	$display = str_replace( 'http://', '', $display );
    	if ( '/' == substr( $display, -1) )
    		$display = substr( $display, 0, -1);
    	return = "$before<a href='$url' rel='external'>$display</a>$after";
    }
  5. dropshot
    Member
    Posted 11 months ago #

    Ok, I might not be clear enough. Sorry for that.

    Yes, I know I should not edit core files. That's why I was stuck.

    I'll try adding your filter.

    Thanks a lot Jose!

  6. dropshot
    Member
    Posted 11 months ago #

    That filter unfortunately white-screens the site...

    Any other suggestions?

  7. Jose Castaneda
    THEME COFFEE MONKEY
    Posted 11 months ago #

    Forgot to take out the = after return. Sorry. I feel really bad I didn't respond sooner. :(

  8. dropshot
    Member
    Posted 10 months ago #

    No need to feel bad!

    I will try this later.

    Thank you!

  9. dropshot
    Member
    Posted 10 months ago #

    Ok.

    I've tried it now and this code does two things.

    As we were attempting to do it doesn't produce the empty anchor tag. Thats great!

    But. Something in the code makes the author url a link inside my site.

    Instead of
    authorurl.com

    the link is
    mysite.com/authorurl.com

    I can't really figure out what line does what. Help please.

  10. dropshot
    Member
    Posted 10 months ago #

    Update:

    I deleted

    $display = str_replace( 'http://www.', '', $display );
    $display = str_replace( 'http://', '', $display );

    So now, the correct links are returned.

    But... When there is no url no anchor is returned. But when there is a url the anchor is returned twice with different output...

    Like this

    <a rel="external" href="http://authorurl.com"> </a>
    <a rel="external" href="http://authorurl.com">authorurl.com</a>
  11. dropshot
    Member
    Posted 10 months ago #

    Ok. Think I got it now. This is what I ended up with.

    add_filter( 'get_comment_author_url_link', 'custom_comment_author_url_link' );
    function custom_comment_author_url_link( $linktext, $before = '', $after = '' ){
        $url = get_comment_author_url();
        if ( $url == '' ) return;
    	$display = ( $linktext != '' ) ? $linktext : $url;
    		if ( '/' == substr( $display, -1) )
    		$display = substr( $display, 0, -1);
    	return "$display";
    }

Reply

You must log in to post.

About this Topic