仅在用户登录时显示超链接

时间:2017-01-10 作者:Agniswar Chakraborty

我是wordpress的新手。我的问题是,我想在我的主题中为管理员设置一个选项,这样,当管理员创建帖子并在帖子上插入超链接时,应该有一个选项可以使未登录的用户看不到超链接。怎么可能请帮助我。

1 个回复
最合适的回答,由SO网友:Kudratullah 整理而成

您可以使用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\' );

相关推荐

Wp-Content/Themes文件夹在哪里?

我想为我的wordpress站点创建一个子主题(仅供参考),这样我的更改就不会被每次更新所覆盖。显然,我需要在wp-content/themes文件夹中为子主题创建一个子文件夹。问题是,我找不到它。我曾尝试将wp内容和wp内容/主题添加到域中-它生成了一个空白的白色页面。我试着在spotlight文件搜索(是的,我有一台mac电脑)中搜索它(只是“wordpress”),它只找到了一些我在2010年安装的wordpress文件夹,我甚至都不记得做过这些。由于我最近在过去两周(2014年5月)建立了这个网站