0

I am trying to create a dynamic url that looks like this localhost/wordpress/slug/username

I am using the wp_redirect function to redirect the user to this url but the probleme is that i don't know how to write this url ?

I've tried to create a variable like so $user = wp_get_current_user(); to includ it like so wp_redirect( "http://localhost/wordpress/slug/$user->display_name" ); but the final result is http://localhost/wordpress2/slug/ so please someone help me

keep in mind that i'm writing my code in the function.php file in my theme folder

Thanks

4
  • Are you using the function inside a hook? I use the template_redirect action for actual redirects. The wp_get_current_user function may not be defined if you're calling it too soon. Commented Nov 5, 2015 at 13:41
  • sorry but i'm not a wp expert ,, so what is a hook? and how do i use 'template_redirect' .... well i think that the variable doesn't return any value and thats the probleme ... maybe as you said the function isn't defined but as i said i'm adding my code into function.php and this file is located under C:\wamp\www\wordpress\wp-content\themes\my_theme Commented Nov 5, 2015 at 14:20
  • WordPress provides loads of these hooks to tap into the WP core code execution. There are actions and filters. template_redirect is one of the available actions to hook onto. Commented Nov 5, 2015 at 14:27
  • thank you for the answer ,,, but while im reading more about this hooks now, can you plz tell me how to modify my code ( i know i'm stupid but i'm just begener wp user not even a wp developper ) ,,, thank you anyway Commented Nov 5, 2015 at 14:44

1 Answer 1

1

Change this

wp_redirect( "http://localhost/wordpress/slug/$user->display_name" );

to

wp_redirect( "http://localhost/wordpress/slug/".$user->display_name);

Try to use bloginfo('url') instead of using localhost/wordpress

Sign up to request clarification or add additional context in comments.

5 Comments

With regards to the first part of your answer, I had the same thought so I just looked it up: php.net/manual/en/… $user->display_name shouldn't be the problem (unless the variable itself is empty?)
tahts what i also think ,,, the variable is empty ,,, maybe i'm not writnig the complete code or i should show the location of the function i'm using ,,,,, oooohhhh thats too complicated for me ,,,, plz help
@raveenanigam $user = wp_get_current_user(); add_action( 'bp_setup_nav', 'create_tab3', 303 ); function create_tab3() { global $bp; bp_core_new_nav_item( array( 'name' => 'myname', 'slug' => 'slug', 'position' => 21, 'default_subnav_slug' => 'myname', 'screen_function' => 'redirect_user_to_tab3', ) ); } function redirect_user_to_tab3(){ global $bp; wp_redirect( "http://localhost/wordpress/slug/$user->display_name" ); exit; }
@raveenanigam comon buddy plzz help ,,,,,,, i've read about hooks and tried so many codes but none of theme worked ,,, the thing is i'm not a programmer ,,,, i started learn php with codecademy.com but ineed the solution now ,,,, plzzzzz
@raveenanigam,,,,, you see from my code that i try to add a tab in buddypress tabs ,,, but i don't know if my code is correct so that when someone visits other user profile and click this tab i want it to show the page of the user visited and not the visitor ;;;; i hope alot from you so plzzzz help ,,,, i already started to learn some php from codecademy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.