Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Contributor etoilewebdesign

    (@etoilewebdesign)

    Hi mgason,

    If you’re comfortable with PHP, we have a public function available that would allow you to show a different WordPress menu to logged-in visitors. We created a helpful video that explains how to use this, which you can watch here: https://www.youtube.com/watch?v=bKiJAjq7PhQ.

    Hello Sirs,

    I ask you to forgive me but just now started using WordPress and my knowledge is very low .

    I have read and heard your explanation on Youtube.

    I plan to use the solution you have using a different menu for those who have logged in and another for those who did not.

    I know the code to use is the following:

    <?php

    $FEUP = new FEUP_User;

    /******************************************
    * DIFFERENT MENU FOR LOGGED IN USERS
    ******************************************/

    // Test if a user is logged in
    if ($FEUP->Is_Logged_In()) {
    // Display the menu named “Logged In Menu” if they are (created under “Appearances” -> “Menus” of the WordPress Admin)
    wp_nav_menu(array(‘menu’ => ‘Logged In Menu’));
    }
    else {
    // Display the menu named “Logged Out Menu” if they aren’t
    wp_nav_menu(array(‘menu’ => ‘Logged Out Menu’));
    }
    ?>

    My big problem is :
    Where is that exactly put this code ?

    Within an existing file ?
    What ? – Where is located the file ?
    Within a new file?
    What should I name the file ?
    Where should I save the file ?

    I would greatly your answer .

    Hi- You can find the file in Public_Functions.php, which you can access via cPanel (File Manager) or another framework that lets you access files relating to your wp site.

    Thanks for your answer !

    Unfortunately after adding that text at the end of Public_Functions.php file I get following error accessing my wordpress:

    Fatal error: Call to a member function get_page_permastruct() on a non-object in /home/pg20853/public_html/caisdequatro.com/wpc4/wp-includes/link-template.php on line 350

    Once time more, things are not so easy as people on manuals say. ??

    Any suggestion ?

    please show me where you inserted it on your php page

    At the very end… ??

    <?php
    if (!class_exists(‘FEUP_User’)){
    class FEUP_User {
    private $Username;
    private $User_ID;

    function __construct($params = array()) {
    global $wpdb, $ewd_feup_user_table_name;

    if (isset($params[‘ID’])) {
    $this->Username = $wpdb->get_var($wpdb->prepare(“SELECT Username FROM $ewd_feup_user_table_name WHERE User_ID=%d”, $params[‘ID’]));
    $this->User_ID = $wpdb->get_var($wpdb->prepare(“SELECT User_ID FROM $ewd_feup_user_table_name WHERE User_ID=%d”, $params[‘ID’]));
    }
    elseif (isset($params[‘Username’])) {
    $this->Username = $wpdb->get_var($wpdb->prepare(“SELECT Username FROM $ewd_feup_user_table_name WHERE Username=%s”, $params[‘Username’]));
    $this->User_ID = $wpdb->get_var($wpdb->prepare(“SELECT User_ID FROM $ewd_feup_user_table_name WHERE Username=%s”, $params[‘Username’]));
    }
    else {
    $CheckCookie = CheckLoginCookie();
    if ($CheckCookie[‘Username’] != “”) {
    $this->Username = $CheckCookie[‘Username’];
    $this->User_ID = $wpdb->get_var($wpdb->prepare(“SELECT User_ID FROM $ewd_feup_user_table_name WHERE Username=%s”, $this->Username));
    }
    }
    }

    function Get_User_Name_For_ID($id = null) {
    global $wpdb, $ewd_feup_user_table_name;

    if(!$id) {
    return null;
    }

    return $wpdb->get_var($wpdb->prepare(“SELECT Username FROM $ewd_feup_user_table_name WHERE User_ID=%d”, $id));
    }

    function Get_Field_Value_For_ID($Field, $id) {
    global $wpdb, $ewd_feup_user_fields_table_name;

    if(!$Field || !$id) {
    return null;
    }
    $Value = $wpdb->get_var($wpdb->prepare(“SELECT Field_Value FROM $ewd_feup_user_fields_table_name WHERE Field_Name=%s AND User_ID=%d”, $Field, $id));

    return $Value;
    }

    function Get_User_ID() {
    return $this->User_ID;
    }

    function Get_Username() {
    return $this->Username;
    }

    function Get_Field_Value($Field) {
    global $wpdb, $ewd_feup_user_fields_table_name;

    $Value = $wpdb->get_var($wpdb->prepare(“SELECT Field_Value FROM $ewd_feup_user_fields_table_name WHERE Field_Name=%s AND User_ID=%d”, $Field, $this->User_ID));

    return $Value;
    }

    function Is_Logged_In() {
    $CheckCookie = CheckLoginCookie();
    if ($this->Username == $CheckCookie[‘Username’] and isset($this->Username)) {return true;}
    else {return false;}
    }
    }
    }

    function EWD_FEUP_Get_All_Users() {
    global $wpdb, $ewd_feup_user_table_name;

    $WP_User_Objects = $wpdb->get_results(“SELECT User_ID FROM $ewd_feup_user_table_name”);

    foreach ($WP_User_Objects as $User_Object) {
    $User_Array[] = new FEUP_User(array(‘ID’ => $User_Object->User_ID));
    }

    return $User_Array;
    }

    /******************************************
    * DIFFERENT MENU FOR LOGGED IN USERS
    ******************************************/

    $FEUP = new FEUP_User;

    // Test if a user is logged in

    if ($FEUP->Is_Logged_In()) {
    // Display the menu named “Logged In Menu” if they are (created under “Appearances” -> “Menus” of the WordPress Admin)
    wp_nav_menu(array(‘menu’ => ‘Logged In Menu’));
    }
    else {
    // Display the menu named “Logged Out Menu” if they aren’t
    wp_nav_menu(array(‘menu’ => ‘Logged Out Menu’));
    }
    ?>

    Plugin Contributor etoilewebdesign

    (@etoilewebdesign)

    Hi Cais,

    The code needs to be placed in whichever of your theme template files controls the area you want to affect. For example, the menu is often found in the header.php template file within a theme. If that is the case with your theme, then that is where you’d place this code. Go into your theme files, find the code you want to modify and then place this there.

    Hey!

    I’ve done exactly like you have mentioned. I used a plugin called If menu. If you search it within this support forum of front end you’ll find it. Then you’ll just copy the pho code and adding it to the function.php of your theme!

    What you will get is an extra option at each menu item where you can choose wether the user is logged in through front end user only or not. Very handy!

    //Martin

    So, what you do is downloading and installing the If Menu plugin. It will tell you that it’s note compatible with the installed version of wordpress. But, it works anyways without any problems.

    After you have it installed you open the functions.php in your theme folder that you’re currently using. At the bottom of this page you copy the code below and past it in that file.

    When you’re done so far you visit the menues in wordpress and you’ll find a “Enable Conditional Logic”-checkbox. Tick this one and you’ll find two new option at the bottom of the drop down list. FEUP logged in and FEUP Level 1.

    Hope that solves the problem for you!

    add_filter( 'if_menu_conditions', 'my_new_menu_conditions' );
    function my_new_menu_conditions( $conditions ) {
      if (function_exists("EWD_FEUP_Get_All_Users")) {
        $conditions[] = array(
          'name'    =>   __( 'FEUP is logged in', 'if-menu' ),
          'condition' =>  'if_FEUP_is_logged_in'
        );
      }
        if (function_exists("EWD_FEUP_Get_All_Users")) {
        $conditions[] = array(
          'name'    =>   __( 'FEUP Level 1', 'if-menu' ),
          'condition' =>  'if_FEUP_Level_1'
        );
      }
      return $conditions;
    }
    
    function if_FEUP_is_logged_in() {
              $CheckCookie = CheckLoginCookie();
              $currentUser = $CheckCookie['Username'];
              if( $currentUser ) return true;
              return false;
    }
    
    function if_FEUP_Level_1() {
              global $wpdb;
              global $ewd_feup_user_table_name, $ewd_feup_levels_table_name;
              $FEUP = new FEUP_User;
                if($FEUP->Is_Logged_In()){
                $Level_ID = $wpdb->get_var("SELECT Level_ID FROM $ewd_feup_user_table_name WHERE User_ID='" . $FEUP->Get_User_ID() . "'");
                $Level = $wpdb->get_var("SELECT Level_Privilege FROM $ewd_feup_levels_table_name WHERE Level_ID='" . $Level_ID . "'");
                }
              if( $Level == 1 ) return true;
              return false;
    }
    Plugin Contributor etoilewebdesign

    (@etoilewebdesign)

    Thank you Martin! Your input is very much appreciated!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Menu visibility of restricted pages’ is closed to new replies.