Here's the situation:
I have an old site, with an old userdatabase (MySQL). I'd like to migrate these users into wordpress without losing the old data (recipe id's, custom user fields etc.).
Besides this i'd like to make a custom registration page with all the (extra) user data i allready had on the old site.
I tried to find some plugins (WP-members, Registration Widget, Register Plus Redux, etc.), but they all didn't fit my purpose.
I'm starting to think that I probably need to code this myself, but that will make Wordpress unable to update.
Does anyone have a solution for this problem?
Thx, Rick
Should be fairly easy although you may have to assign users a new password. That is usually ok with the users as long as they know it's coming.
Take your old database and to a simple sql query to extract the data, then use some code like the following to create a new user for each user in your old database:
$newuser = array(
'user_pass' => wp_generate_password( 12,0 ),
'user_login' => $email,
'user_nicename' => $name['first'].' '.$name['last'],
'user_email' => $email,
'display_name' => $name['first'].' '.$name['last'],
'nickname' => $name['first'].' '.$name['last'],
'first_name' => $name['first'],
'last_name' => $name['last'],
//''
);
$user_id = wp_insert_user($newuser);
wp_new_user_notification($user_id, $newuser['user_pass']);
This code works as of WordPress 3.1. In this example you'd simply want to replace the $name variables with data that you've provided from your old database.
The last two lines are important because that's where the real work happens. The wp_insert_user function will create the user (or throw an error if you're missing info) and the wp_new_user_notification function will send them an email with their password. I would highly recommend taking the time to rewrite the new user notification plugin (it's a pluggable function so you can just copy paste it to your functions.php and make changes there) so that the email users get makes sense. You could also write your own email function or if your passwords are stored in plain text simply pass that info on to WP.
Edit: I missed your need for custom fields, Gravity Forms does this quite well but you can add a custom field to any user with the update_user_meta function. So in the sample code above you'd just want add something after that like:
update_user_meta($user_id, "my_custom_data_key", "my_custom_data_value");
To retrieve that data for display, you'd just use get_user_meta:
print get_user_meta($user_id, "my_custom_data_key");
Related
I'm quite new to Wordpress, so please forgive me and correct any mistake I'm making, I'm willing to learn and improve :)
I set up multiple contact forms for applying to fitness courses. People need to fill them, and I get an email with their written data.
What I'm trying to do now is execute some PHP code that writes data into a MySQL database whenever the user correctly fills and sends a contact form.
I also need every contact form to have a unique code "attached" to it, because the PHP code needs this code to write the data inside the database. (simply put, every course has its unique code that i need to write in the database along with the user's data).
So far as I understand, I need to use add_action( 'wpcf7_before_send_mail', 'my_function' ); in a snippet inside functions.php. What I'm trying to achieve now is to attach this code to every contact form (but it mustn't be visible to users) so that my php snippet reads this code and correctly edits the database.
Any clue on where to look? I don't need the code written, just some ideas!
Thank you in advance, have a nice day everybody.
EDIT: I found out there are "hidden fields" in CF7. So, i added these to my test contact form:
[hidden idcorso "6"]
[hidden idgruppo "0"]
Then i'm using this snippet, but it doesn't work:
add_action( 'wpcf7_before_send_mail', 'process_contact_form_data' );
function process_contact_form_data( $contact_data ){
$idcorso = $contact_data->posted_data["idcorso"];
$idgruppo = $contact_data->posted_data["idgruppo"];
if (is_user_logged_in()) {
$idutente = get_current_user_id();
$data = current_time('d-m-Y - g:i');
$stato = 1;
$wpdb->insert("fis_iscrizioni_2018", array('id_utente' => $idutente, 'id_corso' => $idcorso, 'data' => $data, 'stato' => $stato, 'id_gruppo' => $idgruppo) );
}
}
Any clue?
Suppose you want to build a webpage with Facebook PHP SDK, where you want to allow the user to select the information Facebook will return to the server. I've came with the following code to allow an user to either choose from allowing Facebook to send only the basic profile or else to also send the pages managed by this user.
session_start();
// Load the Facebook PHP SDK
require_once __DIR__ . '/facebook-sdk-v5/autoload.php';
define('APP_ID', 'xxxxxxxxxxx');
define('APP_SECRET', 'xxxxxxxxxxxxxxxxxxxx');
$fbProfile = new Facebook\Facebook([
'app_id' => APP_ID,
'app_secret' => APP_SECRET,
'default_graph_version' => 'v2.7'
]);
$fbPages = new Facebook\Facebook([
'app_id' => APP_ID,
'app_secret' => APP_SECRET,
'default_graph_version' => 'v2.7'
]);
$helperProfile = $fbProfile->getRedirectLoginHelper();
$redirectUrlProfile = 'http://www.example.com/link1.php';
$loginUrlProfile = $helperProfile->getLoginUrl($redirectUrlProfile);
echo 'Get profile with Facebook!<br>';
$helperPages = $fbPages->getRedirectLoginHelper();
$permissions = ['pages_show_list']; // Optional permissions
$redirectUrlPages = "http://www.example.com/link2.php";
$loginUrlPages = $helperPages->getLoginUrl($redirectUrlPages, $permissions);
echo 'Get pages with Facebook!';
If I use the above code (commenting the non-relevant parts) with only one facebook object to either retrieve the profile or the pages managed by user (but not both), everything works fine. But if I use both objects concurrently to give a choice to the user, I get a FacebookSDKException. I guess this is due to CRSF cookies.
Is there any way to circunvent this problem?
I guess this is due to CRSF cookies.
Correct. Calling the getLoginUrl method creates a new random state value and writes it into the session, overwriting any previously stored one.
So if you call the method twice (or more times), login will only work if you call the login dialog via the last login URL created, because only that contains a state value that matches the one stored in the session.
If you want to keep using two different redirect URIs, then you need to implement an additional step to create the correct login URL, and only that one.
So you have two links in your page, both pointing to a script on your server, and passing what permissions to ask for via a GET parameter (whether you want to pass permission names directly, or just a flag like ?loginmode=1/?loginmode=2, is up to you.)
In that script, you decide which redirect URI and scope value to call the getLoginUrl method with - once. And then your script just redirects to that login URL.
(But keep in mind that the step that exchanges the code for an access token also requires the redirect URI parameter to be passed, again - and again with the exact same value that was used in the login dialog call.)
So doing it the way #luschn suggested in comments, using the JS SDK for login purposes, is probably much easier. FB.login can be called with different scopes from different points in your client-side JS code without any such problems.
First of all, I'm not a security expert and new to form validation, password storing and wp plugin development. Any wp plugin tutorial I've been looking at never had a chapter about API passwords. Googling for wordpress password issues didn't return any satisfying results. So that's why I'm asking here.
I want to create a custom Wordpress Plugin which works with a Soap API of another page. To use the Soap API a login is needed. So the Wordpress built in functions add_option(), update_option() and get_option() are all working with plain text.
But I know that in the wp_config file authentication keys can be saved. But how to use them in an option page form to encrypt the password? And would it be possible just to store them in the database, decrypt it and use it in the backend but not showing it on the options page if the user visits that page again. So that the password field just has some black spots in it (not the same amount of the chars of the pass) and the password option only is updated if something is written into that field.
Normally the code is like this:
register_setting( 'my_plugins_option', 'my_options', 'my_validation_function' );
$options = array(
'user' = > 'name',
'password' = > 'pass',
//... other options
)
update_option( 'my_plugins_option', $options );
But how could I make this more secure? I've seen many plugin examples but nothing was about storing passwords. I'm looking for something like this:
function my_validation_function($input){
if($input['password']=='•••••'){
//use the default value of the database if nothing was changed
$old_options=get_option('my_plugins_option');
$input['password']=some_decrypting_function($old_options['password']);
}
else{
//use the password sent from the form
$password=esc_sql(some_encrypting_function($input['password']));
}
// ... validate the other inputs
update_option( 'my_plugins_soap_api_pass', $password );
}
P.S.: This code is not tested yet of course because I don't know how to work with passwords in wordpress plugins and so I wanted to ask for the best practices first.
The other question is: If the modified version of the code from above would work and the password is saved once and never loaded into the Dashboard frontend again (just showing this: '•••••' once typed in) would it be save to work with the get_option() function, decrypt the password and use it in the backend?
Here are a couple recommendations. They aren't specific to WP, but can apply.
1) Save an encrypted password in the options table (use whatever encrypting function you want, just don't write your own)
2) In the options page, simply do NOT output the password. Leave that field blank, and don't require it to be entered if there is already a password stored in the database.
3) Only decrypt the password retrieved from the options table just prior to actually needing it in code.
Say we have a a mother site. Then we have a user registration form on a 3rd party site and a user register system which is processing the whole registration process and in the end will send the user login details in the mother site's database (mysql insertion, again no user_register function). Since there are 2 completely different browser sessions, no actions can be hooked on the mother site during or after registration.
So, let's say we will have stored the users in the database with logins like aaa#bbb.cc (weird, yes) and having a name and the user_nicename appearing like aaa#bbb.cc
Question:
What is the best aproach, wp action/function to be hooked, that once the user is stored in the mother site's database, to write a function to change the user nicename in something like aaa-bbb Automatically of course.
Is there a function/hook suggested for such cases?
The below code didn't helped me, since as I told above, I think the user_register action can't be triggered when a 3rd party site registration is processed:
add_action( 'user_register', 'myplugin_registration_save' );
function myplugin_registration_save( $user_id ) {
$info = get_userdata( $user_id );
$args = array(
'ID' => $user_id,
'user_nicename' => $info->first_name . '-' . $info->last_name
);
wp_update_user( $args );
}
The question as worded is really hard to understand. If I'm reading it correctly, you have two websites. Site One is where people are registering. When they complete registration on Site One something runs that creates a new user in the Site Two database by doing a direct sql insert, not by using any native WP functions.
If that's the case, why don't you simply manipulate the user login before you insert it into the Site Two db? You can't do it via a WordPress hook b/c WordPress is never being called. Hooks are just callback functions sprinkled through the WordPress code. When something happens, like a new user is created, there is a hook that you can assign a function to -- something "Send me an email." If WordPress doesn't handle the new user creation then the hook never gets called.
If you have to do the manipulation after the data has been inserted you'll probably need to look at using a cron job that runs every X amount of time looking for new records in the wp_users table.
I have something a client wants me to build, and I can with wp_mail, but I am wondering if how it should be built is fessable - no they dont want to use third party websites or software.
Essentially a widget will take in the clients email address, with this we can:
Have some kind of interface so we can say that send out 5, 10, 15 posts of category x, y, x on a daily, weekly or monthly basis
Thats not hard, but the question is: how would I store the emails that come in? a new column?
Use these emails and a custom post type to create email templates, newsletters and so on that could be sent to a set of emails (in this case all emails stored for now) at a specified time.
This one isn't hard either, its the custom post type part, how would I create a custom post type that when a post is published the post is not published the same way a post is, or a page. but instead its stored like one, but I can use its content in an email body instead of displaying it like a post or page.
essentially I shouldn't be able to go to:
site.come/email_templates/post_id
So the second one is a bit more complicated but I am wondering how you guys might approach this situation or idea.
Here are some thoughts when it comes to the e-mail subscription part. As for the custom post types - I don't have much experience with those, sorry :)
If you want a quick and easy solution for the e-mail subscriptions, create a wp option (see http://codex.wordpress.org/Function_Reference/add_option) that is essentially a hash table that maps categories to keys in the table.
For each category in the hash table, store an array of userIDs and/or e-mails of the users that are subscribed to that category.
Once you have this data structure in place, it's fairly easy to manipulate and use in with wp_mail. Here is some example code that I've written for one of my plugins:
$subscribers = get_option('subscribers');
$categories = get_the_category($post->ID);
if( !empty($categories) && !empty($subscribers)){
$emails = array();
//Go through each category and accumulate the necessary e-mail addresses
foreach($categories as $category){
$catID = $category->term_id;
if( !empty($subscribers[$catID]) ){
foreach($subscribers[$catID] as $userID => $trash){
$user = get_userdata($userID);
$userEmail = array( $userID => $user->user_email );
if( !in_array($userEmail, $emails) ){
$emails = $emails + $userEmail;
//you can use something like implode(", ", $emails)
//in the Bcc: part when you send out the e-mail.
}
}
}
}
}
Some things to note:
This is a quick and dirty solution. If the number of categories and number of subscribers grows big, you're better of creating a table in the database and maintaining it that way
Make sure to think of situations when categories are deleted (i.e. hook into actions when categories are deleted) and how that will affect your datastructure
The hash table approach works well assuming categories are NOT deleted/added frequently
Good luck!