我想知道我应该如何实现这段代码,以过滤成员和非成员的URL?我应该把代码放进functions.php
文件
$user = wp_get_current_user();
if( $user->exists ) {
add_filter( \'public_link_root\', function() { return \'example.com\'; });
}
// 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.com\' ) . \'/resources/whatever/here\';
SO网友:Alexander Holsgrove
您需要在函数中添加挂钩函数。php文件:
$user = wp_get_current_user();
if($user->exists) {
add_filter(\'public_link_root\', function() { return \'example.com\'; } );
}
然后,无论何时使用任何模板文件中的URL,都可以允许客户过滤器修改URL
public_link_root
. 仅当用户存在时,过滤器才会运行,否则不会更改URL。
因此,在模板文件中:
$some_link = apply_filters(\'public_link_root\', \'ourwebsite.com\') . \'/resources\';
echo \'<a href="\' . $some_link . \'">View Resources</a>\';
为了使其更有意义,您应该花一些时间阅读上的文档和示例代码
add_filter 和
apply_filters.