• I have created the snippet below and it doesn’t seem to work. Am I missing something important about how snippets work? For example, when i log in as the user ‘fred’ the behaviour does not change if the snippet is active or not.
    `/**
    * When a user is logged in, tell WordPress to use ‘page’ on front page of the site
    * @param string $value
    *
    * @return string
    */
    add_filter( ‘pre_option_show_on_front’, function(){
    if (!is_user_logged_in()) {return;}
    if ( is_user_logged_in() )
    {
    global $wpdb;
    $tbl=$wpdb->prefix.”posts”;
    $current_user = wp_get_current_user();
    switch ($current_user->user_login)
    {
    case ‘fred’:$page=’mbc_crm_all_functions’;break;
    case ‘faae’:$page=’mbc_crm_all_functions’;break;
    case ‘bob’:$page=’mbc_menu_all_functions’;break;
    default: $page=””;
    }
    if ($page<>””)
    {

    $sql=”select ID from $tbl where post_type=’page’ and post_name='”.”$page'”;
    $value=intval($wpdb->get_var($sql)); //set page ID
    }
    }
    return $value;
    });

Viewing 1 replies (of 1 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    It looks like there are a few issues with your snippet. I have hopefully fixed them up – please try out the code below and let me know if it works.

    /**
     * When a user is logged in, tell WordPress to use 'page' on front page of the site
     *
     * @param string $value
     *
     * @return string
     */
    add_filter( 'pre_option_show_on_front', function ( $value ) {
    
    	if ( ! is_user_logged_in() ) {
    		return $value;
    	}
    
    	global $wpdb;
    	$current_user = wp_get_current_user();
    
    	$pages = array(
    		'fred' => 'mbc_crm_all_functions',
    		'faae' => 'mbc_crm_all_functions',
    		'bob'  => 'mbc_menu_all_functions',
    	);
    
    	if ( ! isset( $pages[ $current_user->user_login ] ) ) {
    		return $value;
    	}
    
    	$page = $pages[ $current_user->user_login ];
    
    	$value = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = 'page' AND post_name = '%s';", $page ) );
    	$value = intval( $value );
    
    	return $value;
    } );
Viewing 1 replies (of 1 total)

The topic ‘Code to display different logon pages to users’ is closed to new replies.