I have created the following wordpress function to save a form created in Gravity Forms to the usermeta database based on one I had working for CF7 but it isn't working, hopefully someone can see where I've made a mistake. It needs to update the current users fields.
add_action('gform_after_submission', 'input_fields', 10, 2);
function input_fields($entry, $form){
$name = $entry['1'];
$email = $entry['4'];
global $wpdb, $current_user;
$wpdb->insert(
'usermeta',
array(
'description' => $email,
'former_name' => $name
)
);
}
I've seen other examples which are pretty much identical so i'm a bit stuck.
This should do the trick:
add_action( 'gform_after_submission', 'input_fields', 10, 2 );
function input_fields( $entry, $form ) {
$name = $entry[1];
$email = $entry[4];
update_user_meta( get_current_user_id(), 'description', $email );
update_user_meta( get_current_user_id(), 'former_name', $name )
}
Alternately, I'd recommend upgrading to a Developer license and getting access to the User Registration add-on which would do this even more easily. :)
Semicolon sign ";" is missing at line 8 so this is right the answer. :)
add_action( 'gform_after_submission', 'input_fields', 10, 2 );
function input_fields( $entry, $form ) {
$name = $entry[1];
$email = $entry[4];
update_user_meta( get_current_user_id(), 'description', $email );
update_user_meta( get_current_user_id(), 'former_name', $name );
}
Related
I'm trying to add the _capabilities and _level meta data to existing users in a multisite setup so they also have access to the new section, our forum.
The forum is the 3rd site on the network and I want to grant everyone the basic access rights as Contributor. Some users I already added manually to the usermeta table so I check in the script if the user meta_key already exists or not. And to ensure the script doesn't mess with the admin accounts I only want to run the script for user IDs above 3.
When I try below code, I use it in a temp plugin which I will only run once, then I just get a whole bunch of WordPress error messages thrown at me which I somehow can't really decipher to get it working :(
The issue is somehow with the add_ / update_user_meta functions... What am I overlooking?
$prefix = 'tbl_prefix_3_';
$cap = [ "contributor" => true ];
$lvl = 1;
$users = get_users( array( 'fields' => array( 'ID' ) ) );
asort( $users );
foreach( $users as $user ) {
$user_id = intval( $user->ID );
if( $user_id > 3 ) {
$check = get_user_meta( $user_id, $prefix . 'capabilities', true );
if( empty( $check ) ) {
add_user_meta( $user_id, $prefix . 'capabilities', $cap );
} else {
update_user_meta( $user_id, $prefix . 'capabilities', $cap );
}
$check = get_user_meta( $user_id, $prefix . 'user_level', true );
if( empty( $check ) ) {
add_user_meta( $user_id, $prefix . 'user_level', $lvl );
} else {
update_user_meta( $user_id, $prefix . 'user_level', $lvl );
}
}
}
The errors:
Fatal error: Uncaught Error: Call to undefined function get_user_by()
in /home/.../wp-includes/meta.php:1652 Stack trace:
#0 /home/.../wp-includes/meta.php(176): get_object_subtype('user', 4)
#1 /home/.../wp-includes/user.php(1043): update_metadata('user', 4, 'tbl_prefix_capab...', Array, '')
#2 /home/.../wp-content/plugins/assign_rights_to_users.php(33): update_user_meta(4, 'tbl_prefix_capab...', Array)
#3 /home/.../wp-settings.php(391): include_once('/home/...') #4 /home/.../wp-config.php(253): require_once('/home/...')
#5 /home/.../wp-load.php(37): require_once('/home/...')
#6 /home/.../wp-admin/admin.php(34): require_once('/home/...') #7 /ho in /home/.../wp-includes/meta.php on line 1652
Notice: is_embed was called incorrectly. Conditional query tags do not
work before the query is run. Before then, they always return false.
Please see Debugging in WordPress for more information. (This message
was added in version 3.1.0.) in /home/.../wp-includes/functions.php on
line 5313
Notice: is_search was called incorrectly. Conditional query tags do
not work before the query is run. Before then, they always return
false. Please see Debugging in WordPress for more information. (This
message was added in version 3.1.0.) in
/home/.../wp-includes/functions.php on line 5313
There has been a critical error on this website. Please check your
site admin email inbox for instructions.
I figured it out, I had forgotten to include it in a hook so the WordPress stuff was not properly available in the plugin :|
I now placed it into an admin_init hook so I activate the plugin, let it do it's magic, and than deactivate and remove the plugin again. I also simplified the whole thing, so only using the update_user_meta now.
Anyway, this is the working result:
<?php
/**
* Plugin Name: 000 FIX MISSING USER RIGHTS
* Description: Activate, deactivate, check the results, remove the plugin!
* Version: 1.0
* Author: golabs
* #source: https://stackoverflow.com/a/66877257/3592696
*/
function fix_missing_user_rights() {
$prefix = 'wp_prefix_';
/**
* Use one of the 2 methods:
* - manual array with user ids
* OR
* - collect them from the database
* Just comment out the method you don't want to use.
*/
# $users = [ 1,2,3,4,5,6,7,8,9,10 ];
$users = get_users( array( 'fields' => 'ID' ) ); asort( $users );
foreach( $users as $user ){
update_user_meta( $user, $prefix.'capabilities', [ 'subscriber' => true ] );
update_user_meta( $user, $prefix.'user_level', 0 );
update_user_meta( $user, $prefix.'2_capabilities', [ 'customer' => true ] );
update_user_meta( $user, $prefix.'2_user_level', 8 );
update_user_meta( $user, $prefix.'3_capabilities', [ 'contributor' => true ] );
update_user_meta( $user, $prefix.'3_user_level', 1 );
}
}
add_action( 'admin_init', 'fix_missing_user_rights' );
When a user register in my site I have set the user_activation_key column value from wp_users table like that:
$code = sha1( $user_id . time() );
global $wpdb;
$wpdb->update(
$wpdb->prefix.'users', //table name
array( 'user_activation_key' => $code ),
array( 'ID' => $user_id ),
array( '%s' ),
array( '%d' )
);
It's because I want to make activation system by sending an email with clickable link:
$activation_link = add_query_arg(
array(
'key' => $code,
'user' => $user_id
), get_permalink( 44 )
);
$message = "<div style='padding : 20px; border : 1px solid #ddd; color : #000;'>Hello $surname, <br/><br/>Please confirm your email addresss . Click this link to confirm : <a href='$activation_link'>Confirm Now</a><br/><br/></div>";
$to = $email;
$subject = 'Confirm your registration process"';
$body = $message;
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $body, $headers );
Now, the column user_activation_key has the hash code and user_status column value is 0
Now the actual question:
When user go to www.site.com/wp-admin that means login page I want to show an error message if the user_status column value is 0.
I don't' have any idea which hook or how can I cehck this while user login?
Use the admin_notices hook to display messages in the backend, the code below is similar to what your after:
add_action('admin_notices', 'account_activation_check');
function account_activation_check() {
global $wpdb;
// setup vars //
$currentID = get_current_user_id();
$user = get_user_by( 'ID', $currentID );
$userStatus = $user->user_status;
// check if user status is 0 //
if($userStatus == 0) {
echo '<div class="error"><p>Your email has not been verified!</p></div>';
}
}
I have solved the issues by using the wp_authenticate_user hook. Here is the code:
add_filter( 'wp_authenticate_user', 'shibbir_authenticate_user', 10, 2 );
function shibbir_authenticate_user( $user ) {
if ( $user->data->user_status == 0 ) {
return new WP_Error( 'error', __( 'Your account is not activate, Please contact site admininstrator.' , 'shibbir' ) );
}
return $user;
}
It is possible set display name from entered string into nickname registration field? I trying do this with simple hook, but after all it is not work.
function set_default_display_name( $user_id ) {
$user = get_userdata( $user_id );
$name = $user->nickname;
$args = array(
'ID' => $user_id,
'display_name' => $name
);
wp_update_user( $args );
}
add_action( 'user_register', 'set_default_display_name' );
By default immediately after registration the display name was set from WP username (login) not nickname. Can sombody help me to set a display name from nickname?
// change default display name format
add_action('user_register', 'registration_save_displayname', 1000);
function registration_save_displayname($user_id) {
if ( isset( $_POST['first_name'])){
$pretty_name = $_POST['first_name'];
wp_update_user( array ('ID' => $user_id, 'display_name'=> $pretty_name) ) ;
}
}
I want to send out a e-mail to the site admin that a new Buddypress user has signed up. That is what the first function is for. It works great. The second function is to append a few xprofile custom fields into the e-mail. Sadly the filter in the 2e function is not working. How can I make it work? Or how can I combine them so that they work?
function my_pending( $user_id, $user_login, $user_password, $user_email, $usermeta ) {
// Send the email notification.
wp_mail( 'some#some.com', $user_login . ' bla bla', 'bla bla' );
}
add_action( 'bp_core_signup_user', 'my_pending', 10, 5 );
And the second function:
function custom_activation_email_body( $message, $user_id, $key ) {
$field1 = xprofile_get_field_data( '11', $user_id );
$field2 = xprofile_get_field_data( '12', $user_id );
$field3 = xprofile_get_field_data( '4', $user_id );
$message .= sprintf( __( "Username: %s Email: %s Membership Type: %s", 'lang' ), $field1, $field2, $field3 );
return $message;
}
add_filter('bp_core_signup_user', 'custom_activation_email_body', 10, 3);
If you use a field id, don't use it as a string, so
xprofile_get_field_data( '11', $user_id );
should be
xprofile_get_field_data( 11, $user_id );
And I think your filter is applied to the email sent to the new user.
So move your xprofile calls into the add_action function.
Review wp_mail to see how to setup the fields.
I use a frontend form to post, and what i want is to check if a post exists by title.If yes and metafield value is bigger than the old one,just replace the value.
Anyone that has implemented something like that in the past?
<?php
session_start();
$user_email = $_SESSION['user_email'];
$user_name = $_SESSION['user_name'];
$user_img_url = 'https://graph.facebook.com/'.$user_name.'/picture?width=200&height=200';
global $wpdb;
global $post;
$title = $user_name; // get the inputted title
$content = $_POST['content']; // get the inputted content
$categorie = $_POST['cat']; // get the category selected by user
$zombies = $_POST['zombies'];
$kliks = $_POST['klik'];
$timess = $_POST['times'];
$name = $_POST['namn'];
if( 'POST' == $_SERVER['REQUEST_METHOD'] ) { // if form has been submitted
$my_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
'post_author' => 2,
'post_category' => array(2),
);
$my_post = wp_insert_post($my_post);
add_post_meta($my_post, 'Zombies', $zombies);
add_post_meta($my_post, 'klik', $kliks);
add_post_meta($my_post, 'times', $timess);
add_post_meta($my_post, 'namn', $name);
add_post_meta($my_post, 'profile_photo', $user_img_url);
wp_redirect( home_url() );
# if $verifica is not empty, then we don't insert the post and we display a message
}
?>
Your question is not quite clear to me but if you want to query a post by it's title then you can use get_page_by_title() function as given below
$post = get_page_by_title( $_POST['post_title'], OBJECT, 'post' );
To get a custom meta field you can use get_post_meta() function as given below
$meta_value = get_post_meta($post->ID, 'field_name', true);
Then compare and update the meta value you can use, for example,
if( $_POST['custom_meta_field'] > $meta_value )
{
// Update the meta value
update_post_meta( $post->id, 'field_name', $meta_value );
}
The update_post_meta() function is being used to update the custom meta field.
Update: (Based on comment)
You can use following to get the post that is available by Facebook ID
$post = get_page_by_title( $_POST['facebook_id'], OBJECT, 'post' );
Also if the meta field is time string (12:10 am) then you have to convert it to timestamp/numeric value before you compare it, like,
$meta_value = strtotime(get_post_meta($post->ID, 'field_name', true));
So, it'll become something like 1363493400 and you can compare like
if( $_POST['custom_meta_field'] > $meta_value ){ ... }
In this case your custom_meta_field should be also a timestamp/numeric value or you have to convert it using strtotime() function just like $meta_value has been converted.