In Wordpress, I added some fields in the users dashboard. I made them required but I'd like to display a message if they are empty.
Here is an example (just one field) of what I did :
function my_admin_notice() { ?>
<div class="error">
<p><?php _e( 'Error!', 'user_street' ); ?></p>
</div>
<?php
}
function save_extra_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) {
return false;
}
if (!empty($_POST['user_street'])) {
update_user_meta( $user_id, 'user_street', $_POST['user_street'] );
}
else {
add_action('admin_notices', 'my_admin_notice');
return false;
}
}
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
Unfortunately, nothing happens.
I also tried with add_settings_error but I had the same problem.
Can someone give me a hand or just explain to me what I am doing wrong? It would be much appreciated! Thank you very much!
Looks like it's too late to add the admin_notice. An alternative is to use the action hook load-$page (that runs at the very beginning of a page load) and check if the user meta is empty. If so, trigger the notice.
add_action( 'load-profile.php', 'notice_so_23373245' );
add_action( 'load-user-edit.php', 'notice_so_23373245' );
function notice_so_23373245()
{
// Check to see if page is user-edit or profile
$user = isset( $_GET['user_id'] ) ? $_GET['user_id'] : get_current_user_id();
if( empty( get_user_meta( $user, 'user_street' ) ) )
add_action('admin_notices', 'my_admin_notice');
}
Related
When I add these lines to my code
It seems Whenever a user put an order in WooCommerce it gets fired and overwrite user_pass2 with an empty string
I expect this line of code run only when a user update the profile
function my_profile_update( $user_id ) {
if ( ! is_admin() ) {
update_user_meta($user_id, 'user_pass2', password_hash($_POST['password_1'], PASSWORD_DEFAULT));
}
}
add_action( 'profile_update', 'my_profile_update' );
We need to just check for the $_POST argument that has to be available
function my_profile_update( $user_id ) {
if ( ! is_admin() && isset($_POST['password_1']) ) {
update_user_meta($user_id, 'user_pass2', password_hash($_POST['password_1'], PASSWORD_DEFAULT));
}
}
add_action( 'profile_update', 'my_profile_update' );
A customer can usually cancel a membership in his dashboard. How can I restrict this (hide the cancel button) for a specific membership?
I found this code for general hide the cancel button, work's so far:
function sv_edit_my_memberships_actions( $actions )
{
unset( $actions['cancel'] );
return $actions}
add_filter( 'wc_memberships_my_account_my_memberships_actions', 'sv_edit_my_memberships_actions' );
}
maybe with this function?
wc_memberships_is_user_active_member( $current_user_id, 'membership-name' )
You are almost there. You already have done some syntactical mistakes which #mujeeb specified. Try following code
function sv_edit_my_memberships_actions( $actions )
{
$user_id = get_current_user_id();
if(wc_memberships_is_user_active_member( $user_id, 'silver' )){// Instead of silver you can give your membership type
unset( $actions['cancel'] );
}
return $actions;
}
add_filter( 'wc_memberships_my_account_my_memberships_actions', 'sv_edit_my_memberships_actions' );
function sv_edit_my_memberships_actions( $actions ) {
unset( $actions['cancel'] );
return $actions;
}
add_filter( 'wc_memberships_members_area_my_memberships_actions', 'sv_edit_my_memberships_actions' );
On my wordpress blog I have the functionality that allows users who are logged in to be able to select categories of posts they would like to see as a stream on the blog page. So each user only sees posts from categories they have subscribed to and not all categories.
I used this code.
add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
function my_show_extra_profile_fields( $user ) {
?>
<h3>Extra profile information</h3>
<table class="form-table">
<tr>
<th>
<label for="post_category">Display Categories</label><br />
<span class="description">Which categories would you like to see posts from?</span>
</th>
<td>
<ul class="categorychecklist form-no-clear">
<?php wp_terms_checklist( 0, array( 'checked_ontop' => false, 'taxonomy' => 'category', 'selected_cats' => get_user_meta( $user->ID, 'post_category', true ) ) ) ?>
</ul>
</td>
</tr>
</table>
<?php
}
add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
function my_save_extra_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
if( !isset( $_POST['post_category'] ) )
return false;
if( !is_array( $_POST['post_category'] ) )
return false;
update_usermeta( $user_id, 'post_category', array_map( 'absint',
$_POST['post_category'] ) );
}
add_action( 'pre_get_posts', 'set_user_categories', 1000 );
function set_user_categories( $wp_obj ) {
if( !is_front_page() )
return;
if( 'nav_menu_item' == $wp_obj->query_vars['post_type'] )
return;
global $current_user;
$user_cats = get_user_meta( $current_user->ID, 'post_category', true );
if( !$user_cats )
return;
$wp_obj->set( 'category__in', $user_cats );
}
this is the result.
This code works fine, however, I want these options to appear on another page and not the user profile, so I created another page template specifically for handling this.
<?php
/*
Template Name: Subscribe
*/
?>
<?php get_header() ?>
<div class="page-container">
<div><?php the_field('my_show_extra_profile_fields'); ?></div>
<?php get_footer(); ?>
as you can see, I tried to display the field "my_show_extra_profile_fields" but it doesn't show up.
How can i make these options show on my page template subscribe.php and not the user profile??
Try this code. extra_field_name may be "post_category"
global $current_user;
echo get_user_meta( $current_user->ID, 'extra_field_name', true );
as like
echo get_user_meta( $current_user->ID, 'post_category', true );
I'm using the Current version of Wordpress (4.2.4) and woocommerce (2.3.6).
Problem: Don't let users share their login details to the site.
Old Solution: The Answer to this question has been working until now.
What i need: Can anyone see what's wrong with this code & why it wouldn't work with the new Wordpress update (4.2.4).
Or offer up another solution.
Code from answer by #manoj-dhiman:
how can I prevent multiple login from same user id from different browsers in wordpress?
if( !class_exists( 'WPSingleUserLoggin' ) ) {
class WPSingleUserLoggin
{
private $session_id;
function __construct()
{
if ( ! session_id() )
session_start();
$this->session_id = session_id();
add_action( 'init', array( $this, 'init' ) );
add_action( 'wp_login', array( $this, 'wp_login' ), 10, 2 );
}
function init()
{
if( ! is_user_logged_in() )
return;
$stored_sess_id = get_user_meta( get_current_user_id(), '_wp_single_user_hash', true );
if( $stored_sess_id != $this->session_id )
{
wp_logout();
wp_redirect( wp_login_url() );
exit;
}
}
function wp_login( $user_login, $user )
{
update_user_meta( $user->ID, '_wp_single_user_hash', $this->session_id );
return;
}
}
new WPSingleUserLoggin();
}
Using user meta data, I am running a function that is hidden in a user’s profile that allows them to have a checklist. I’m using Theme My Login and not including the checklist on the user’s profile page when they edit/view their profile.
I am trying to display the checklist on it’s own page. I can get it to display but can’t seem to get the Submit button to work. Can someone point me in the right direction?
Thanks!
Code example in functions.php:
function my_show_extra_profile_fields( $user ) { ?>
<div><input type="checkbox" name="checkbox1" id=" checkbox1 " value="yes" <?php if (esc_attr( get_the_author_meta( "checkbox1", $user->ID )) == "yes") echo "checked"; ?> />
Checkbox 1
</div>
<button type="submit">Save</button>
<?php }
?>
<?php
add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
function my_save_extra_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
update_usermeta( $user_id, checkbox1, $_POST['checkbox1'] );
}
?>
Code example to display the extra profile field on my page template:
<?php my_show_extra_profile_fields(); ?>
EDIT: Forgot my '', added them back in
You have $_POST[checkbox1] it should be $_POST['checkbox1']
Assuming you created the field already, you can use wp_update_user from the codex.
$user_id = wp_update_user( array( 'ID' => $user_id, 'checkbox1' => $_POST['checkbox1'] ) );
if ( is_wp_error( $user_id ) ) {
// There was an error, probably that user doesn't exist.
} else {
// Success!
}