WordPress.org

Ready to get started?Download WordPress

Forums

Peter's Login Redirect
[resolved] Custom Variable - cant seem to set (4 posts)

  1. housebg
    Member
    Posted 2 years ago #

    I'm trying to set a custom variable, so that I can redirect to a specific page on login.

    I'm setting the user role of Contributor to do a custom redirect to /path/[variable]charter[/variable]

    Then, I'm trying to put in the function for defining this variable. It requires doing a database query to pull the post_name based on a custom post type and the current user id == post_author.

    I've tested that the query is pulling the correct information into $postname with a var_dump - but I can't seem to assign that string correctly to the variable I'm trying to create and then pass into the redirect url. So my result is no variable on the end, just the path.

    Here's the code I'm using

    global $current_user,$wpdb;
    get_currentuserinfo();
    
    $pullcharter = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE post_author= $current_user->ID AND post_type='captain'");
    $postname = $pullcharter->post_name;
    
    function customRULVariableCharter( $custom_redirect_to, $variable, $user )
    {
        if( 'charter' == $variable )
        {
            return $postname;
        }
        else
        {
            return $custom_redirect_to;
        }
    }
    
    add_filter( 'rul_replace_variable', 'customRULVariableCharter', 10, 3 );

    Would appreciate any help figuring this out.

    Cheers!

    http://wordpress.org/extend/plugins/peters-login-redirect/

  2. housebg
    Member
    Posted 2 years ago #

    I am so close, yet not there. New modified code below...
    I can get this to work if I hard code the user id in the where clause, like post_author=4. But I can't seem to grab the id of the currentuser and pass that to the where clause.
    Can anyone with fresh eyes see my problem?

    function customRULVariableCharter( $custom_redirect_to, $variable, $user ) {
    
    global $wpdb, $current_user;
    get_currentuserinfo();
    
    $pullcharter = $wpdb->get_row("SELECT post_name FROM $wpdb->posts WHERE post_author=$current_user->ID AND post_type='captain'", ARRAY_N);
    $postname = $pullcharter[0];
    
        if( 'charter' == $variable )
        {
            return $postname;
        }
        else
        {
            return $custom_redirect_to;
        }
    }
    
    add_filter( 'rul_replace_variable', 'customRULVariableCharter', 10, 3 );
  3. Peter
    Member
    Plugin Author

    Posted 2 years ago #

    Try using the $user variable that is passed to the function instead of trying to grab $current_user separately.

    $pullcharter = $wpdb->get_row("SELECT post_name FROM {$wpdb->posts} WHERE post_author={$user->ID} AND post_type='captain'", ARRAY_N);

  4. housebg
    Member
    Posted 2 years ago #

    THANK YOU!!! That was the secret, works perfect now. :-)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.