• Hello,
    can someone please check if this code is right? It is working as far as I can see it. Do I need to have a global $post in my function?

    I like to redirect the author link to a custom post type that will be / that is the porfile.

    in my functions.php

    function profile_link() {
    	$profile_id = get_the_author_meta( 'profile_id' );
    	$profile_url = get_permalink( $profile_id );
    	return $profile_url; // home_url( '/', 'https' )
    }
    add_filter( 'author_link', 'profile_link' );

    Thanks a lot and cheers,
    Denis

    • This topic was modified 6 years, 10 months ago by DenisCGN.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    You only need to declare $post global if your own code needs to use it. get_the_author_meta() declares $post global internally for itself. It would not help functions for you to do so anyway, your declaration is out of scope to any functions called.

    Thread Starter DenisCGN

    (@deniscgn)

    @bcworkz

    Hello bcworkz,

    thanks for your answer. Where can I check if the function already uses the $post global?

    And, is there a way to change the link name of author_link? Preferably I like to change it to the title of the re-directed custom post type.

    Cheers,
    Denis

    Moderator bcworkz

    (@bcworkz)

    Check the source code in the Code Reference. Even if it didn’t, there’s little you can do unless the function accepts the post ID as a parameter. A global declaration in your code is out of scope to functions. And declarations within functions are out of scope for your code.

    It may seem odd that globals can be out of scope. It’s not the global itself, but the declaration. Globals are indeed global, but they need to be declared, and declarations are subject to scoping rules. It’s how PHP works. Other languages handle globals differently.

    It gets weirder. You’ll see in the above link that $post is in fact not declared global. A related global $authordata is where the function gets its data. Both globals, and a few more, are all set when the_post() is called at the start of each loop. But you will not find the declaration in that source code either. the_post() merely calls WP_Query::the_post(), which does declare $post global. But where’s the global $authordata and the others I mentioned? Farther down, WP_Query::setup_postdata() is called, where you will finally see global $authordata and its many relatives declared. Whew!

    Clearly, checking source code can get pretty convoluted. It’s not unheard of to drill down through a half dozen sub-functions before you reach what you’re looking for. I’ve found the Uses and Used By sections below the source code very useful in finding what I need in source.

    I’m not sure I’m following what you’re asking about author_link link name. This filter only affects the URL used, the part assigned to the anchor tag’s href attribute. Anything else about the link is managed elsewhere. Is it something in the URL you want to change, or is it the link text that appears on the page? I think you mean the link text. In that case, look on the responsible template for how that is determined.

    If it’s not a static string like “Author’s Posts”, it’s probably another function like get_the_author_meta('display_name'). Find the function’s source code and see if there is a filter applied to the return value. If there is, hook that filter and return your desired value. Or alter the template code to get the value through some other means.

    Thread Starter DenisCGN

    (@deniscgn)

    Hi @bcworkz,

    I searched Google and found some code that I customized like I need it. And, I deleted my Global $Post 🙂 And it works without.

    function guest_author_name( $authorLinkName ) {
    	$localhero_profile_id = get_the_author_meta( 'localhero_profile_id' );
    	$localhero_profile_title = get_the_title( $localhero_profile_id );
    	if ( $localhero_profile_title )
    		$authorLinkName = $localhero_profile_title;
    	return $authorLinkName;
    }
    add_filter( 'the_author', 'guest_author_name' );
    add_filter( 'get_the_author_display_name', 'guest_author_name' );

    Why I need to use 2 filters, I dont knwo, it works.

    Thanks and Cheers,
    Denis

    • This reply was modified 6 years, 10 months ago by DenisCGN.
    Moderator bcworkz

    (@bcworkz)

    You’re welcome.

    The two filters are because there are different ways to get author names, each filter addresses a different way so you’re covered no matter what.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Redirect author link’ is closed to new replies.