3

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 :/

3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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