• Basically I need to add an extra login field to the WordPress login. But the data in the field I have to add to the form resides on a separate database. So the complicated part is that I have ‘Username’ and ‘Password’ that is authenticated against the WordPress database and I need to add ‘Customer Number’ that resides on another database. I’m not sure if there is a way to authenticate one login using multiple databases.

    The ‘Customer Number’ doesn’t need to match any of the other fields. It just needs to check whether or not it exists.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter on3advertising

    (@on3advertising)

    So I got this far. I first connect to the database, validate the customer ID that is entered into the form, then do something with it. The problem is that I am getting a SQL error when entering letters into the form, saying “mysql_num_rows() expects parameter 1 to be resource, boolean”.

    Also, I am embedding a shortcode into this function using the “do_shortcode(‘shortcode’)” function and it’s showing the name of the shortcode as text rather than actually populating the shortcode data.

    function restrictForum(){
    	$db_name = 'dbname';
    	$con = mysql_connect("url","username","password");
    	mysql_select_db("$db_name")or die("cannot select DB");
    	$cust_id = mysql_real_escape_string($_GET['cid']);
    	$sql = "SELECT * FROM customer_data WHERE customer_number = $cust_id";
    	$result= mysql_query($sql);
    if ( isset( $_GET['cid'] ) && !empty( $_GET['cid'] ) && mysql_num_rows($result) == 1) {
    // cid (customer ID) is present, show the bbPress login form
    echo do_shortcode('bbp-login');
    
    } elseif ( $_GET['error'] == true ) {
    
    // cid entered was not valid
    echo 'The customer ID you entered is not valid.';
    
    } else {
    echo ('<form name="cust-form" id="cust-form" method="get">
    <label for="cid">Enter Your Customer #</label><input name="cid" id="cid" type="text" maxlength="6" />
    <input type="submit" value="Validate"');
    // cid is absent so show the form to validate it
    
    // do your custom form here that asks for the customer ID. Then if the customer ID
    // is correct/valid reload this page like /login?cid=123456 which will show
    // the bbPress login form.
    }
    }
    add_shortcode('forum-login-restrict','restrictForum');
    Moderator bcworkz

    (@bcworkz)

    While I am interested in adding login fields, I don’t have an answer for you, in particular regarding other databases.

    I can help with do_shortcode() though. The function is expecting the square brackets as part of the tag parameter passed. This enables all the various forms with arguments and end tags to be properly interpreted as well. So just do echo do_shortcode('[bbp-login]');

    Thread Starter on3advertising

    (@on3advertising)

    That is really weird, I tried that before and it didn’t work. I think I had some code that was messing it up. It now works.

    I almost have this whole thing working, but when I enter letters as a customer ID (which is invalid), I get a SQL error rather than an expected error:

    function restrictForum(){
    	$db_name = 'dbname';
    	$con = mysql_connect("url","username","password");
    	mysql_select_db("$db_name")or die("cannot select DB");
    	$cust_id = mysql_real_escape_string($_GET['cid']);
    	$sql = "SELECT * FROM customer_data WHERE customer_number = $cust_id";
    	$result= mysql_query($sql);
    		if ( isset( $_GET['cid'] ) && !empty( $_GET['cid'] ) && mysql_num_rows($result) == 1) {
    			// cid (customer ID) is present, show the bbPress login form
    			echo ('Please enter your username and password to continue.</br>');
    			echo  do_shortcode('[bbp-login]');
    
    		} elseif ( $_GET['error'] == true ) {
    
    			// cid entered was not valid
    			echo 'The customer ID you entered is not valid.';
    
    		} else {
    			echo ('<form name="cust-form" id="cust-form" method="get">
    			<label for="cid">Enter Your Customer #</label><input name="cid" id="cid" type="text" maxlength="6" />
    			<input type="submit" value="Validate"');
    			// cid is absent so show the form to validate it
    
    			// do your custom form here that asks for the customer ID. Then if the customer ID
    			// is correct/valid reload this page like /login?cid=123456 which will show
    			// the bbPress login form.
    		}
    }
    add_shortcode('forum-login-restrict','restrictForum');
    Thread Starter on3advertising

    (@on3advertising)

    Just got the whole thing working, except I still get the sql error with varchars:

    function restrictForum(){
    	$db_name = 'dbname';
    	$con = mysql_connect("url","username","password");
    	mysql_select_db("$db_name")or die("cannot select DB");
    	$cust_id = mysql_real_escape_string($_GET['cid']);
    	$sql = "SELECT * FROM customer_data WHERE customer_number = $cust_id";
    	$result= mysql_query($sql);
    	$cust_id_form = ('<form name="cust-form" id="cust-form" method="get">
    			<label for="cid">Enter Your Customer #</label><input name="cid" id="cid" type="text" maxlength="6" />
    			<input type="submit" value="Validate" />');
    		if ( isset( $_GET['cid'] ) && !empty( $_GET['cid'] ) && mysql_num_rows($result) == 1) {
    			// cid (customer ID) is present, show the bbPress login form
    			echo ('Please enter your username and password to continue.</br>');
    			echo  do_shortcode('[bbp-login]');
    
    		} elseif ( $_GET['error'] == true ) {
    
    			// cid entered was not valid
    			echo 'The customer ID you entered is not valid.';
    
    		} elseif (!is_user_logged_in()){
    			echo $cust_id_form;
    		}
    }
    add_shortcode('forum-login-restrict','restrictForum');

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Add extra field to login’ is closed to new replies.