guests-only-menu-items

redirecting…

Posted in general | Leave a comment

How to change a multi-site primary domain

Posted in multi-site | Tagged | Leave a comment

Moving posts between blogs

Posted in general | Leave a comment

wordpress 3.6, blockui.js problem and sidebar login

 

Posted in general | Leave a comment

Guest only menu items

I have posted before about a menu CSS snippet I found to only show menu items to logged in users. I have been looking for a suitably elegant way to reverse this and have a menu item ONLY visible to users who are NOT logged in.

Today I have arrived at a solution. This is not the only solution, and by all means, it’s not the best. But for my needs, this is perfect (see notes in the Conclusion).

This needs to be done in three parts and this will give you the option to have menu item(s) displayed for ONLY logged in users, as well as having them displayed for ONLY guests. The choice is yours and can be changed at anytime easily in the dashboard of wordpress single blog sites, or blogs a part of the multi-site setup–although some of the setup process might not be available to you in a multi-site setup if you are not the super-admin.

Step 1 — adding code to functions.php

Insert the following code into your functions.php file:

add_filter('body_class','not_logged_in_class');

function not_logged_in_class($classes = '')
{
	if ( !is_user_logged_in() ) {
		$classes[] = 'not-logged-in';
	}

	return $classes;
}

Step 2 — adding style to your theme’s CSS

Add the following CSS to your style sheet:

#menu-mainmenu li.logged-in-nav {
display: none;
} 

body.logged-in #menu-mainmenu li.logged-in-nav {
display: list-item;
}

body.not-logged-in #menu-mainmenu li.logged-out-nav {
display: list-item;
}

#menu-mainmenu li.logged-out-nav {
display: none;
}

IMPORTANT: The menu I use for my primary menu is called “MainMenu” ergo my CSS markup above includes “#menu-mainmenu”. Where I originally obtained the inspiration for this source used “#nav”. It may be prudent to first determine what your theme throws out as an ID for the navigation menu. Just check the page source and find out what it is. You want the name of the ID associated with the unordered list that is associated with the list items of where your menu is to be displayed. For example, if you named your primary menu “MyPrimaryMenu”, you should change the above code to “#menu-mymainmenu”. Values I have seen from various blogs include: #nav, #menu, #menu-MENUNAME (without commas of course). DON’T FORGET TO CHANGE THIS.

Step 3 — using the CSS

Create your page, and make sure it appears in your menu. Drag it to the desired location within the menu structure and open the the drop down to the right (downward pointing triangle). On the right under “Title Attribute” there is a text field with the heading “CSS Classes (Optional)”, insert “logged-in-nav” or “logged-out-nav” as appropriate into this box, and save the menu. For something to be visible to only logged-in users, use “logged-in-nav”. For something to only be visible to guests, use “logged-out-nav”.

That’s it! You’re done!

Since the object of this exercise was to have a menu item displayed for only guests, let’s test it out now. View your blog/website and confirm that the menu item is NOT there when you’re logged in. Log out, and check that it DOES appear. If it’s not behaving as expected go back and check your steps. Also, there are some quick things to check listed below in Troubleshooting.

Caveat and conclusion:

Once implemented, this setup gives a simple way to chop and change menu items visibility dependant on whether a user is logged in or not. This has not been tested in an environment where user access management is in place. I have tried various plugins for access management, but none gave me the option to SHOW ONLY to guests.

WARNING: There is one downside to this method. The end-user does not SEE the menu item, because the CSS “hides” it from them. It doesn’t altogether remove it from the page source and screen readers may in fact read the menu item–I have no personal knowledge on this. Anyone viewing the source will see these links — whether logged in or not. So unless you have the destination pages “controlled” in some other way, then maybe this method isn’t the best solution. This suits my purposes, but it might not suit yours.

I hope you find this snippet as useful as I did.

Troubleshooting:

  • Make sure you changed #menu-mainmenu to the appropriate ID name of the unordered list holding the list items for your menu!!! <– this is the most likely cause of it not showing up;
  • Clear the cache for your browser! Should be done first, you don’t want to spend the next hour debugging to find there’s no error! It’s possible the new style wasn’t loaded;
  • Check your spelling! A single simple typing mistake could be to blame, go and double check your work;
  • Make sure you’ve made the appropriate changes in the two files as required and SAVED/UPLOADED to the correct place;
  • If it’s still not there, make sure you’ve edited the correct theme’s files;
  • It could also be the fact your style sheet is not loading, try modifying some other part of the CSS and see if it changes in your browser.

Other notes and advice:

  • You can modify your theme’s style sheet and functions.php file directly–not ideal because subsequent style updates by the theme developer will see your style go out the window;
  • You can create a child theme of your current theme and make the changes there–create the functions.php file and add the code, as well as the styles to the CSS. This is a lot better, and the preferred method when dealing with a fixed theme, but this style and function will be lost when you change the theme;
  • Some theme’s have a CSS injection mechanism which is more ideal, but this also suffers from the lack of carry over to any new and subsequent theme;
  • Finally (and the way I have done it), I found a simple plugin years ago that allows custom CSS to be added and kept across all your themes (for that blog). The plugin allows you to save the CSS in the wp database and is not tied to any theme.

References:


Posted in general | Tagged , , , , | 8 Comments

Showing menu items to logged in users only

UPDATE: I have now determined how to extend this to show menu items to ONLY guests. Something I’ve been looking at for a while. 

I have been searching for a simple way to show particular menu items to only logged in users. I know there are ways of achieving this in php by hardcoding your menu structure within the template for your site, but this involves delving into the source code (which I don’t like doing), and then makes the menu less dynamic to changes. That is, if you later add a page, you have to re-edit the code to have the menu updated…

Until now!

I found this the other day (credit reference below) and with a little CSS injection and associating a class with the menu item, we can have the same end-user effect.

Firstly you need to determine the name of the ID associated with your menu. What you are looking for is the name of the ID of the unordered list, or the div immediately prior to this, that contains your menu items. It will be some derivative of your primary menu’s name. View your source, grab this info and change “#nav” below to “#menu-MYMENUNAME”. It will be all lowercase.

Once you have that, now you need to add some code to your style sheet:

#nav li.logged-in-nav {
  display: none;
} 

body.logged-in #nav li.logged-in-nav {
  display: list-item;
}

logged-in-navThen, simply add the “logged-in-nav” class to the optional CSS Classes field for the menu item (as per the screen cap on the right).

It’s really that simple!

Now any menu items that have this class added to the CSS will only be visible to the end user if they are logged into the site.

If only I could make this work so that a particular menu item was ONLY visible to a guest! Now THAT is my next goal. Unfortunately there is no body class associated with users who aren’t logged in.

Reference: I originally found this snippet and give all credit to Bill Erickson at:
http://www.billerickson.net/customizing-wordpress-menus/#logged-in
Thanks Bill!

NB: There is one downside to this method. The end-user does not SEE the menu item, because the CSS “hides” it from them. It doesn’t altogether remove it from the page source and screen readers may in fact read the menu item, or anyone viewing the source will see these links — whether logged in or not. So unless you have the destination pages “controlled” in some other way, then maybe this method isn’t the best solution. However, for what I want, this is perfect.

Posted in general | 19 Comments

Creating usernames of less than 4 characters

I wanted to create an account for my wife, her name is just three letters and so I dug around the interwebs and found this little gem Thanks very much to Lew Ayotte of FullThrotleDevelopment.com.

Supports Multi-sites perfectly. Simply copy the code (below) and place it in a file name of your choosing in the wp-content/mu-plugins directory. Don’t forget to enclose the function (the whole code selection below) in php tags (<?php … ?>)

<?php
function remove_username_char_limit($result) {
  if ( is_wp_error( $result[ 'errors' ] ) && !empty( $result[ 'errors' ]->errors ) ) {

    // Get all the error messages from $result
    $messages = $result['errors']->get_error_messages();
    $i = 0;
    foreach ( $messages as $message ) {

      // Check if any message is the char limit message
      if ( 0 == strcasecmp("Username must be at least 4 characters", $message)) {
        // Unset whole 'user_name' error array if only 1 message exists
        // and that message is the char limit error
        if ( 1 == count($messages) ) {
          unset( $result['errors']->errors['user_name'] );
        } else {
          // Otherwise just unset the char limit message
          unset( $result['errors']->errors['user_name'][$i] );
        }
      }	

      $i++;
    }
  }

  return $result;
}
add_action('wpmu_validate_user_signup', 'remove_username_char_limit');
?>

Please note, this is not my code, I have provided a link to the original source above. I have posted the code here in the event it is no longer available on the original site.

Posted in plugins | Leave a comment

Wordpress and “You do not have sufficient permissions to access this page.”

wordpress logocross post from dav3.net post on this subject

The other day I upgraded my wordpress sites using the backend upgrade and I lost all permissions to access ANY admin area. Any guest could use the site as normal, there were no problems there, but I couldn’t login. It took a few days searching on the web (without luck) and eventually, I jumped back into cpanel, and phpmyadmin and started digging.

The solution was a lot simpler than what I had envisioned.

During the update process, one of the plugins caused havoc in the backend, and when trying to display the dashboard, the plugin was breaking privileges. First I had to work out, which plugin it was, and if in fact if it was a plugin that was the cause. As it turned out, it was, and this is how I found it:

1. created a new folder called “old_plugins” *
2. moved all the plugins from the “plugins” folder to the “old_plugins folder” *
3. logged in – sweet – I was allowed and all appears good
4. visit the plugins page (you will get a warning that your plugins have been deactivated since they can’t be found.
5. put the plugins back into their original directory *
6. reactivate each plugin until you find the faulty one. For me it was a google reader plugin for the dashboard, which I never used anyway – so it’s now gone!

Once you find the faulty plugin, you’ll get the permission error again, simply go back to your ftp or file manager (I just used cpanel) and move the faulty plugin out…

* You could probably have just renamed the plugins folder, visited the plugins page, re-renamed the plugins folder, and gone through the reactivation process that way, this is just the way I did it.

Posted in general | Tagged , , , | 3 Comments

WordPress iPhone app – crashes ** FIXED **

Today I downloaded the Wordpress app from the Apple App store, for the iPhone. I have been looking forward to this for some time since that although my site looks good on a mobile device, it doesn’t lend itself to be uploaded to from a mobile device.

Until now… Enter the Wordpress application from the app store. errrrm… ok… But it didn’t work for me. It would consistently crash and restarting the app would just crash immediately. I determined this was because I am running a Wordpress multi-site. Whether it is or not, I found the fix:

Hi.
Please open “your/wordpress/directory/xmlrpc.php” in any text editor, and rewrite the line 477,
'blogid' => $blog_id,
to
'blogid' => (string) $blog_id,.
Yeah! Now, you can use WordPress for iOS!
Cheers!

Now I’m a happy camper…

BTW, I found this solution here, look for the first comment dated 05/07/2010.

edit: today I upgraded wordpress to the latest version 3.01 and I checked the above file, it has not been fixed. If you install the 3.01 update, you will also need to re-edit and include the string cast for the blogid.

on a side note: I also use tumblr, and the tumblr app for the iPhone is equally FANTASTIC! Highly suggested and recommended if you use tumblr.

Posted in general | Tagged , , , , | Leave a comment