• Hello need to change the subscribed user levels of access of my Divi / WP.
    I have added a Divi child theme.
    I now need a code snippet to insert into the functions file.
    This is what I tried but it failed. Can anyone explain if this can done?

    /*
    * Make Private Posts visible to Subscribers
    * Typically only visible to admin or editor
    */
    <?php
    function?whitespider_private_posts_subscribers(){
    ?$subRole?= get_role(?‘subscriber’?);
    ?$subRole->add_cap(?‘read_private_posts’?);
    $subRole->add_cap(?‘read_private_pages’?);
    }
    add_action(?‘init’,?‘ws_private_posts_subscribers’?);
    ?>

    Than you for looking, Best Nick

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter nicallen

    (@nicallen)

    I found a snippet that works as the Divi Child functions file. Thanks
    <?php

    function my_et_enqueue_styles() {
    wp_enqueue_style( ‘parent-style’, get_template_directory_uri() . ‘/style.css’ );
    wp_enqueue_script( ‘divi’, get_stylesheet_directory_uri() . ‘/js/scripts.js’, array( ‘jquery’, ‘divi-custom-script’ ), ‘0.1.1’, true );
    }
    add_action( ‘wp_enqueue_scripts’, ‘my_et_enqueue_styles’ );

    /* === Add your own functions below this line ===
    * ——————————————– */
    // Allow subscribers to see Private posts and pages

    $subRole = get_role( ‘subscriber’ );
    $subRole->add_cap( ‘read_private_posts’ );
    $subRole->add_cap( ‘read_private_pages’ );
    // Redirect to home page on login
    function loginRedirect( $redirect_to, $request_redirect_to, $user ) {
    if ( is_a( $user, ‘WP_User’ ) && $user->has_cap( ‘edit_posts’ ) === false ) {
    return get_bloginfo( ‘siteurl’ );
    }
    return $redirect_to; }

    add_filter( ‘login_redirect’, ‘loginRedirect’, 10, 3 );

    Thread Starter nicallen

    (@nicallen)

    How would I extend this to include subscribers as well as authors, but still allow them to post?
    Would this work?

    $subRole = get_role( ‘author’ );
    $subRole->add_cap( ‘read_private_posts’ );
    $subRole->add_cap( ‘read_private_pages’ );
    // Redirect to home page on login
    function loginRedirect( $redirect_to, $request_redirect_to, $user ) {
    if ( is_a( $user, ‘WP_User’ ) && $user->has_cap( ‘edit_posts’ ) === true ) {
    return get_bloginfo( ‘siteurl’ );
    }
    return $redirect_to; }

    add_filter( ‘login_redirect’, ‘loginRedirect’, 10, 3 );

    Moderator bcworkz

    (@bcworkz)

    Why not try it and see? ??

    Yes, it should work. Adding caps straight from functions.php like you did is not ideal though. WP can still be a bit unstable at that point, though it’s apparently late enough to add caps. It’s better to hook an action like in your OP. The reason that didn’t work is the passed name in add_action() does not match your function declaration. (“ws” vs. “whitespider”)

    You don’t really need to add caps on every request, which placing code in “init” or straight into functions.php does. Such code is usually hooked into theme or plugin activation code. Alternately, you could leave the code where it is. Let it execute once, then comment it out. The added caps will persist until explicitly removed.

    BTW, when you post code in these forums, please demarcate with backticks or use the code button. Otherwise your code gets corrupted and it’s difficult for others to test your code for themselves as the code is now presented. Note that the straight quotes converted to curly quotes will cause syntax errors.

    Thread Starter nicallen

    (@nicallen)

    Thanks for taking your time to explain this. I’ll use a plugin instead. Best

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Get subscribers to see private posts’ is closed to new replies.