WordPress.org

Forums

Peter's Login Redirect
[resolved] Incorrect redirect address for users names with space. (BuddyPress) (8 posts)

  1. BackpackersUnion
    Member
    Posted 1 year ago #

    Hi Peter, Thanks for the plugin. It is exactly what I was looking for.

    The one problem I have encountered relates to users who have a space in their user name. For example, one user name is "katie m". The redirect should read .../members/katie-m/profile/. Instead the redirect link reads .../members/katie%20m/profile/ and results in a "Page Not Found" message.

    Do you know if there is a way to correct this and have the [variable]username[/variable] syntax insert "-" where there are spaces in the users name? Thanks again for your time and work on this plugin!

    My site uses both WordPress Multisite and BuddyPress (Latest Versions).

    https://wordpress.org/plugins/peters-login-redirect/

  2. Chris Dillon
    Member
    Posted 1 year ago #

    You can accomplish this with a filter. Add this to your theme's functions.php:

    function hyphenate_username_function( $bool, $variable, $user )
    {
      if( 'hyphenate_username' == $variable )
      {
        return sanitize_title( $user->user_login );
      }
    }
    add_filter( 'rul_replace_variable', 'hyphenate_username_function', 10, 3 );

    Then instead of [variable]username[/variable]
    use [variable]hyphenate_username[/variable].

    You can name that function and variable whatever you want.

    Codex: sanitize_title and sanitize_title_with_dashes

  3. BackpackersUnion
    Member
    Posted 1 year ago #

    Hi Chris, really appreciate you looking into this.

    I implemented the changes, and it seems the function removed the name from the URL. The result was;
    .../members//profile/

    and a page not found message. When using a non space username (i.e. "carl"), it redirected correctly.

    I looked at the codex, but modifying functions are still a little outside my skill set, but appreciate you including them.

    Do you have any other thoughts on how to get this working. Thanks again.

  4. BackpackersUnion
    Member
    Posted 1 year ago #

    BTW - wpmission.com is a cool site idea. Ended up checking out all your "favorite plugins" in your wp profile and found a few new ones to try. Looking forward to seeing your launch.

  5. Chris Dillon
    Member
    Posted 1 year ago #

    Thanks!

    Sorry it didn't work. I admit I didn't test that filter very well. Let me work on it.

  6. Chris Dillon
    Member
    Posted 1 year ago #

    The profile address in BuddyPress is like

    http://example.com/members/username/profile/

    so the filter should be

    function hyphenate_username_function( $bool, $variable, $user )
    {
      if( 'hyphenate_username' == $variable )
      {
        return $user->user_nicename );
      }
    }
    add_filter( 'rul_replace_variable', 'hyphenate_username_function', 10, 3 );

    (notice the nicename which is an already-hyphenated version of their username instead of the sanitize_title function I previously used)

    and the redirect rule becomes

    members/[variable]hyphenate_username[/variable]/profile
  7. BackpackersUnion
    Member
    Posted 1 year ago #

    That did the trick! You have no idea how much I appreciate your help.

    And thank you for explaining your work. Functions give WordPress so much versatility but I haven't been able to wrap my head around them. So, still working on them and every tip helps.

    Quick note for anyone else having this problem; At first I received a "Parse error: syntax error", with luck noticed one less '(' than the previous code. I guessed on the placement in Chris's code and got this,

    function hyphenate_username_function( $bool, $variable, $user )
    {
      if( 'hyphenate_username' == $variable )
      {
        return( $user-> user_nicename );
      }
    }
    add_filter( 'rul_replace_variable', 'hyphenate_username_function', 10, 3 );

    Added it right after "return". The error went away and Chris's code worked beautifully, but I have no idea if that's the correct placement.

    Thanks again Chris! I just signed up for your mailing list. Looking forward to it.

  8. Chris Dillon
    Member
    Posted 1 year ago #

    Sorry for the typo. Glad you got it working. It should actually be

    return $user->user_nicename;

    without a space after -> and without parentheses.

    I'm working on an add-on plugin for Peter's plugin and I will include this function.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Peter's Login Redirect
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic