I’m doing some work on an existing site that is based on the WordPress theme, but uses about 15 plugins (Including Buddypress). One in particular is the WP Sliding Login|Dashboard plugin, which has a link to the user’s activity feed. I found the code that creates that link in the wp-sliding-login-dashboard.php file:
<?php
if ( is_user_logged_in() ) {
global $current_user;
$username = $current_user->user_login;
echo ' <li><a href="http://www.mywebsitename.com/members/' . $username . '/activity/">Activity Feed</a></li>';
}
?>
I want to use this code to send the user to the same location, but using the a link at the top of the home page. Unfortunately, the home page links are all created using WordPress menus, which as far as I can tell, only allow for the use of static links attached to existing pages.
Do I create a dummy page to link to that exits only to execute the above code? Is that even possible? Picture a five-year-old trying to read Shakespeare, and you have an idea of my ability as a coder, so feel free to engage me as such – i.e. if you say, “oh just create a scoping function instead of creating a global function”, i would stare at you drooling and confused.
Images for clarity: The sliding login menu (WP-Sliding Login|Dashboard Plugin), showing the target URL in the status bar as www.ferrignofit.com/members/FerrignoFit/activity/ (the current logged in user is FerrignoFit):
http://i.imgur.com/NPvmCXU.jpg
The main page WordPress-based menu, which i want to go to the above URL, but is currently going to www.ferrignofit.com/activity/, a different page:
http://i.imgur.com/dIiFpDC.jpg
So here’s a jQuery solution for this specific issue. From seeing the images you have, what you want to do is target a specific anchor in your dynamic WordPress menu.
As you may be aware, you can create custom links for the WordPress menu function… it simply lets you set a label and a URL. You should create such item and just give it a hash for the URL for now.
Now set a class for that specific menu item so you can have a nice handle for jQuery to target it (otherwise you can use the dynamic class that WordPress creates for each specific menu-item).
One you have a class set or you know what you need to target then you can add this block of code before your menu.
Please not that I am using PHP to get the current username in WordPress, after I get the username and I store it in the
userName
variable I use it in theuserUrl
to set it with the path that I want.On another note, I’m using
is_user_logged_in()
so you have the option of making the link something else if the user is in fact not logged in. So with thisif
statement one of the two blocks of code will be output by PHP.As soon as my two variables are set, I simply target my menu item with jQuery and modify the
href
attribute to replace the hash with the path and dynamic username.Though this is not very good practice to mix JS and PHP, I’d like to learn myself here what other solutions someone can suggest so I’m posting this as a solution for your very specific issue.
I tested this code with my own WordPress site and it works, just remember that this code NEEDS to be in a PHP file otherwise the PHP used in the
<script>
tags won’t mean anything to the server if it’s in a.js
file.