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
1 Answer
Reset to default 0You 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.