Modify get_permalink with a specific filter

I’m trying to modify the get_permalink function to return a custom url, as:

function my_function( $permalink, $postID ) {
    $external_link = get_post_meta( $postID, 'external_link', true );
    if( !empty( $external_link ) ) {
        $permalink = $external_link;
    }

    return $permalink;
}
add_filter( 'post_link', 'my_function', 10, 2 );

So, when I will call the function as:

<a href="' . esc_attr( get_permalink( $post->ID ) ) . '"></a>

I think that the result depends of the “external_link” value, and if this value is not empty I need to return this value.

But it not works :/

1 Answer
1

Solved!,

Change $postID by $post->ID, so, rewriting function:

function noticias_print_permalink( $permalink, $post, $leavename ){
    $external_link = get_post_meta( $post->ID, 'external_link', true );
    if( !empty( $external_link ) ) {
        $permalink = $external_link;
    }
    return $permalink;
}

Regards.

Leave a Comment