Search code examples
phpwordpresspermalinkscode-snippets

Set #Wordpress Permalink with PHP with Conditions


What im trying to accomplish is set different wordpress permalink for logged in users For logged in users use: /loggedin/%post_id%/%postname%/ and for others use /post/%post_id%/%postname%/

Here is the PHP code which Im trying but not working

add_action( 'init', 'smartest_set_permalinks' );
function smartest_set_permalinks() {
global $wp_rewrite;
if(is_user_logged_in) {
$wp_rewrite->set_permalink_structure( '/loggedin/%post_id%/%postname%/' );
} else {
$wp_rewrite->set_permalink_structure( '/post/%post_id%/%postname%/' );
}};

What am i missing here can somebody point out or fix this?


Solution

  • You were missing the '()' after is_user_logged_in. is_user_logged_in() is a default wordpress function. And the semicolon in the end of function brace is not needed.

    add_action( 'init', 'smartest_set_permalinks' );
    function smartest_set_permalinks() {
        global $wp_rewrite;
        if(is_user_logged_in()) {
            $wp_rewrite->set_permalink_structure( '/loggedin/%post_id%/%postname%/' );
        } else {
            $wp_rewrite->set_permalink_structure( '/post/%post_id%/%postname%/' );
        }
    }