最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Masking external links with internal link for member-only

programmeradmin0浏览0评论

I'd like to know how I should implement this code, to filter URLs for members and non-members? Should I put the code into the functions.php file?

$user = wp_get_current_user();
if( $user->exists ) {
    add_filter( 'public_link_root', function() { return 'example'; });
}

// Whenever you have to output a link, instead of doing that check over and over, because it's already established that the user's logged in, if you wrote your system in the correct manner (and you can do additional checks), do:

$link_to_output = apply_filters( 'public_link_root', 'ourwebsite' ) . '/resources/whatever/here';

I'd like to know how I should implement this code, to filter URLs for members and non-members? Should I put the code into the functions.php file?

$user = wp_get_current_user();
if( $user->exists ) {
    add_filter( 'public_link_root', function() { return 'example'; });
}

// Whenever you have to output a link, instead of doing that check over and over, because it's already established that the user's logged in, if you wrote your system in the correct manner (and you can do additional checks), do:

$link_to_output = apply_filters( 'public_link_root', 'ourwebsite' ) . '/resources/whatever/here';
Share Improve this question edited May 12, 2019 at 17:32 Alexander Holsgrove 1,9091 gold badge15 silver badges25 bronze badges asked May 10, 2019 at 21:40 maxwassimmaxwassim 15 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You will want to add the hook function in your functions.php file:

$user = wp_get_current_user();
if($user->exists) {
    add_filter('public_link_root', function() { return 'example'; } );
}

Then whenever you are working with a URL in any of your template files, you can allow the URL to be modified by your customer filter public_link_root. The filter will only run if the user exists, otherwise the URL won't be altered.

So in your template files:

$some_link = apply_filters('public_link_root', 'ourwebsite') . '/resources';
echo '<a href="' . $some_link . '">View Resources</a>';

For this to make more sense, you should spend some time reading the documentation and example code on add_filter and apply_filters.

发布评论

评论列表(0)

  1. 暂无评论