您可以使用the_content 筛选挂钩以使用登录url替换超链接。下面的代码片段应该可以做到这一点。
更新:此功能应检查用户是否登录(我的错,我忘了添加)
add_action( "the_content", "restrict_url_in_post_content" );
function restrict_url_in_post_content() {
$loginUrl = sprintf( \'<a href="%s" class="some-class">%s</a>\', wp_login_url( get_the_permalink() ), __( "You need to login to view this link", "text_domain" ) );
if( ! is_user_logged_in() )
$the_content = preg_replace( "/<a[^>]*>([^<]+)<\\/a>/", $loginUrl, $the_content );
return $the_content;
}
Update:
使用以下短代码对特定内容具有相同的效果。
用途:[restricted]<a href="#">url</a>[/restricted]
function restricted_content( $atts , $content = null ) {
$atts = shortcode_atts(
array(
\'show_login\' => \'1\',
\'message\' => __( "You need to login to view this", "text_domain" ),
\'login_redirect\' => get_the_permalink(),
),
$atts,
\'restricted\'
);
$loginUrl = sprintf( \'<a href="%s" class="some-class">%s</a>\', wp_login_url( $atts["login_redirect"] ), $atts["message"] );
if( is_user_logged_in() ) {
return $content;
} else if( $atts["show_login"] == 1 ) {
return $loginUrl;
} else {
return $atts["message"];
}
}
add_shortcode( \'restricted\', \'restricted_content\' );