I have the following function to create a member_id:
add_action('user_register', 'generate_member_id');
function generate_member_id($user_id){
$unique_id = 1000 + get_current_user_id();
$chapter_id = fetch_chapter();
$member_id = "FAL-" . $chapter_id . "-" . $unique_id;
return $member_id;
}
It appears to work fine and I can call the function from a shortcode and get the correct value.
Now I want to add the returned value to a database and have tried using
update_user_meta()
$wpdb->update
$wpdb->insert
$wpdb->query
all with necessary arguments but nothing seems to work.
I expected this to do it, but it just generates a generic error that I am not able to debug (I have a problem with error logging):
global $wpdb;
$users = $wpdb->get_results( "SELECT ID FROM $wpdb->users" );
$generated_id = generate_member_id()
if( $users ) {
foreach ( $users as $user ) {
update_user_meta( $user->ID, 'member_id', generate_member_id() );
}
}
However if I change generate_member_id() to a string like 'memberId', it works and the database is updated. So I don't know what I'm doing wrong. Why can't I add' the result of the generate_member_id() function and to a WP database?
EDIT
add_action('user_register', 'fetch_chapter');
function fetch_chapter(){
$current_user = wp_get_current_user();
$current_user_id = $current_user->ID;
global $wpdb;
$result = $wpdb->get_results('SELECT meta_value FROM usermeta WHERE meta_key = \'chapter_name\' AND user_id = '. $current_user->ID .' LIMIT 1');
if ($result[0]->meta_value == 'Peregrine') {
$result[0]->meta_value = 'PG';
}elseif ($result[0]->meta_value == 'Barbary') {
$result[0]->meta_value = 'BB';
}
return $result[0]->meta_value;
}
There are a few things I notice that could cause problems:
You have this line that (1) has no ; at the end and (2) it doesn't look like you need it anyway - you don't use $generated_id anywhere:
$generated_id = generate_member_id()
You are not passing the user_id into generate_member_id for your users in the loop calling update_user_meta...
... but even if you did pass in the user_id, generate_member_id isn't using it anyway - it is using the current user id. That is the wrong value for the users in your loop.
You need to fix your generate_member_id function to work with the user_id parameter. I assume you want this function to work without one too, so it still gets the get_current_user_id is no user id is passed in:
add_action('user_register', 'generate_member_id');
function generate_member_id($user_id){
// If no user_id is passed in, use the current user id
if (!$user_id) $user_id = get_current_user_id();
$unique_id = 1000 + $user_id;
$chapter_id = fetch_chapter_id();
$member_id = "FAL-" . $chapter_id . "-" . $unique_id;
return $member_id;
}
And then remove the erroneous $generated_id... line and pass the user_id to the generate_member_id in your update_user_meta:
global $wpdb;
$users = $wpdb->get_results( "SELECT ID FROM $wpdb->users" );
if( $users ) {
foreach ( $users as $user ) {
update_user_meta( $user->ID, 'member_id', generate_member_id($user->ID) );
}
}
Related
See image of the table here:
[1]: https://i.stack.imgur.com/KjWIQ.png
How do I show data from 'designs_save_user_list'
of all user_id that contain 'account_parent' and with the same number in the meta_value?
I'm only able to see the data of a user, but I want to see everyone who has the same number in the account_parent
the current code is:
if(is_user_logged_in()){
$user_ID = get_current_user_id();
$get_designs_list = get_user_meta($user_ID, 'designs_save_user_list', true);
if(is_user_logged_in()){
$user_ID = get_current_user_id();
$account_parent = get_user_meta($user_ID, 'account_parent', true);
if (isset($account_parent) && $account_parent == 2 ){
$get_designs_list = get_user_meta($user_ID, 'designs_save_user_list',
true);
}
}
check this code if its work for you
I have this function in order to set a link for my user to go to their custom profile page :
add_shortcode( 'current_user_link', 'wppbc_current_user_link' );
function wppbc_current_user_link( $atts, $content ) {
if ( is_user_logged_in() )
{
$current_user = wp_get_current_user();
$id = $current_user->user_login;
return "<a class='lienprofils' href='https://mywebsite/author/{$id}'><i class='fas fa-user' aria-hidden='true'></i> MON PROFIL</a>"; }
return ;
}
The issue is that if the user ID is his email like john#example.com the link https://mywebsite/author/john#example.com will have an 403 error. But if the is the link is https://mywebsite/author/johnexample-com it works.
So is there a way to clean the user ID into my function in order to remove # and transform "." into "-" ?
Best regards,
Clément
Try this
$id = $current_user->user_login;
//Remove #
$id = str_replace('#', '', $id);
// Replace . with -
$id = str_replace('.', '-', $id);
I have used browserstack many times for idea's and bugs. I'm at an ok level now but I like to hone my skills.
The following code is a custom piece of coding I have done to a template. It works perfectly. How can I improve this code? It seems way to long for what I'm trying to achieve. How can I shorten this code?
// Display treatments specialism icons that a location is practising
// Fetch all the entries from WP database
function get_specs($location_id) {
//Register globals
global $wpdb;
// The query
$sql = "SELECT * FROM $wpdb->postmeta";
// Get the results array from the query
$results = $wpdb->get_results($sql) or die(mysql_error());
// Assign the results array and iterate 1 by one
foreach( $results as $results ) {
$meta_key = $results->meta_key;
// Every time the key value is a 'naam' (name) check wether this is the name of a specialism.
// The names are not gotten by certain id's because or flexibility (Client add more/change spacialisms later without programming intervention)
if ($meta_key == "naam") {
// The name value to be compared
$specialismen_naam = $results->meta_value;
// Store the ID connected to the found naam for later use
$specialismen_id = $results->post_id;
// Fetch the specialism meta_value array of all the locations, all specialisms practised are in 1 array
$specialismen = get_post_meta($location_id, 'specialiteiten', true);
// Search location specialism array and compare it with the name value. if it has a match continue
if (in_array($specialismen_naam, $specialismen)) {
// Get the specialism image from the earlyer stored id
$thumbnail_image = get_post_meta( $specialismen_id, 'thumbnail_image', true );
// Get the link to the specialism page for if clicked
$link = get_page_link(get_post_meta( $specialismen_id, 'link', true ));
//display the specialism icon with link to specialism page
echo '<li>'. wp_get_attachment_image( $thumbnail_image, array(30,30) ) .'</li>';
}
}
}
}
// Get the Locations
// Fetch the data
function get_location_fp($object_ids) {
// register globals
global $wpdb;
// Query
$sql = "SELECT * FROM $wpdb->postmeta WHERE post_id = $object_ids";
// Results
$results = $wpdb->get_results($sql) or die(mysql_error());
foreach( $results as $results ) {
$meta_key = $results->meta_key;
$meta_value = $results->meta_value;
// If there is a location thumbnail assigned (has to be), process
if ($meta_key == "lokatie_thumbnail") {
// fetch displayed data from event in variable. Telephone, address, name, link to location page
$telefoonnummer = get_post_meta( $object_ids, 'telefoonnummer', true );
$adres = get_post_meta( $object_ids, 'adres', true );
$naam = get_post_meta( $object_ids, 'naam', true );
$link = get_page_link(get_post_meta( $object_ids, 'link', true ));
// Fetch thumbnail image of each location
$thumbnail_image = get_post_meta( $object_ids, 'lokatie_thumbnail', true );
// Display the location
echo '<div id="lokatie-fp"><div class="lokatie-image">'. wp_get_attachment_image( $thumbnail_image ) .'</div><div class="lokatie-gegevens"><h3>'. $naam .'</h3><p>'. $telefoonnummer .'</p><p>'. $adres .'</p>';
// Add all specialisms that are practised at this location
echo '<ul>'. get_specs($object_ids) .'</ul></div></div></a>';
}
}
}
// Called somewhere in template:
<?php get_location_fp(62); ?>
<?php get_location_fp(67); ?>
<?php get_location_fp(105); ?>
<?php get_location_fp(104); ?>
Indenting your code consistently will make it much easier to read/debug
// Display treatments specialism icons that a location is practising
// Fetch all the entries from WP database
function get_specs($location_id) {
//Register globals
global $wpdb;
// The query
$sql = "SELECT * FROM $wpdb->postmeta";
// Get the results array from the query
$results = $wpdb->get_results($sql) or die(mysql_error());
// Assign the results array and iterate 1 by one
foreach( $results as $results ) {
$meta_key = $results->meta_key;
// Every time the key value is a 'naam' (name) check wether this is the name of a specialism.
// The names are not gotten by certain id's because or flexibility (Client add more/change spacialisms later without programming intervention)
if ($meta_key == "naam") {
// The name value to be compared
$specialismen_naam = $results->meta_value;
// Store the ID connected to the found naam for later use
$specialismen_id = $results->post_id;
// Fetch the specialism meta_value array of all the locations, all specialisms practised are in 1 array
$specialismen = get_post_meta($location_id, 'specialiteiten', true);
// Search location specialism array and compare it with the name value. if it has a match continue
if (in_array($specialismen_naam, $specialismen)) {
// Get the specialism image from the earlyer stored id
$thumbnail_image = get_post_meta( $specialismen_id, 'thumbnail_image', true );
// Get the link to the specialism page for if clicked
$link = get_page_link(get_post_meta( $specialismen_id, 'link', true ));
//display the specialism icon with link to specialism page
echo '<li>'. wp_get_attachment_image( $thumbnail_image, array(30,30) ) .'</li>';
}
}
}
}
// Get the Locations
// Fetch the data
function get_location_fp($object_ids) {
// register globals
global $wpdb;
// Query
$sql = "SELECT * FROM $wpdb->postmeta WHERE post_id = $object_ids";
// Results
$results = $wpdb->get_results($sql) or die(mysql_error());
foreach( $results as $results ) {
$meta_key = $results->meta_key;
$meta_value = $results->meta_value;
// If there is a location thumbnail assigned (has to be), process
if ($meta_key == "lokatie_thumbnail") {
// fetch displayed data from event in variable. Telephone, address, name, link to location page
$telefoonnummer = get_post_meta( $object_ids, 'telefoonnummer', true );
$adres = get_post_meta( $object_ids, 'adres', true );
$naam = get_post_meta( $object_ids, 'naam', true );
$link = get_page_link(get_post_meta( $object_ids, 'link', true ));
// Fetch thumbnail image of each location
$thumbnail_image = get_post_meta( $object_ids, 'lokatie_thumbnail', true );
// Display the location
echo '<div id="lokatie-fp"><div class="lokatie-image">'. wp_get_attachment_image( $thumbnail_image ) .'</div><div class="lokatie-gegevens"><h3>'. $naam .'</h3><p>'. $telefoonnummer .'</p><p>'. $adres .'</p>';
// Add all specialisms that are practised at this location
echo '<ul>'. get_specs($object_ids) .'</ul></div></div></a>';
}
}
// Called somewhere in template:
<?php get_location_fp(62); ?>
<?php get_location_fp(67); ?>
<?php get_location_fp(105); ?>
<?php get_location_fp(104); ?>
Sometimes it can be easier to read if you can avoid doing nested if statements, for example:
if ($meta_key != "naam") {
continue;
}
directly echoing out can sometimes be a bit confusing and can lead to output being sent when not desired (say if an error occurs). I quite like to build up an array of the output before rendering that:
$output = array();
$output[] = '<li>'. wp_get_attachment_image( $thumbnail_image, array(30,30) ) .'</li>';
...
...
echo implode(PHP_EOL,$output);
Other than those minor things I'd say its fine. It's really nice and verbose, easy to read, well documented.
As for Jalpa's comments I'd say that its good practice to have the SQL query and the execution of it on different lines, it improves readability. And there's no need for an if condition before the foreach, that takes care of itself.
Edit
You mentioned your li's are being rendered early. From your function get_specs() you certainly don't want to render anything but here you can return your output to the get_location_fp() calling function.
function get_specs($loctation_id) {
...
$output = array();
...
$output[] = '<li>'. wp_get_attachment_image( $thumbnail_image, array(30,30) ) .'</li>';
...
return implode(PHP_EOL,$output);
}
function get_location_fp($object_ids) {
...
$output[] = '<ul>'. get_specs($object_ids) .'</ul></div></div></a>';
...
}
By returning the output from get_specs() rather than rendering it, it will be inserted between the uls and not before it.
I think you need to add below changes :
1) Please write query like this $wpdb->get_results("SELECT * FROM $wpdb->postmeta") no need to write in two lines.
2) please put if condition before foreach is not empty or count() array > 0.
I am trying to save what will amount to a small list of ids in user meta, but for some reason I am only able to save the most recent visit. Is there something obviously wrong with my approach?
function check_visit() {
$user = get_current_user_id();
$post_visits = get_user_meta($user, 'post_visits', true);
$visited = explode(",",$post_visits);
$id = (string)the_ID();
if($id && !in_array($id, $visited)) {
$visited[] = $id;
update_user_meta($user, 'post_visits', implode(",", $visited));
}
print_r(implode(",",$visited));
}
Actually
$visited[] = $id
is correct!
The issue is that you are using the_ID() function, this function print the ID, do not return any value.
The correct function should be get_the_ID()
Your code should look like this:
function aw_check_visit(){
$user = get_current_user_id();
$post_visits = get_user_meta($user, 'post_visits', true);
$visited = explode(",",$post_visits);
$id = (string)get_the_ID();
if($id && !in_array($id, $visited)) {
$visited[] = $id;
update_user_meta($user, 'post_visits', implode(",", $visited));
}
}
You need to be using array_push instead of adding it to the array like you have.
Change this
$visited[] = $id;
to
array_push($visited, $id);
I have the following code:
function process_bulk_action() {
if (isset($_GET['locations'])) {
$location_ids = ( is_array( $_GET['locations'] ) ) ? $_GET['locations'] : array( $_GET['locations'] );
global $wpdb;
switch ( $this->current_action() ) {
case 'edit':
bulk_edit($location_ids);
break;
case 'delete':
bulk_delete($locations_ids);
break;
default:break;
}
}
}
function bulk_delete($ids) {
foreach ( $ids as $id ) {
$id = absint( $id );
$sql = "DELETE FROM wp_nc_location WHERE location_id = $id";
$delete = $wpdb->query( $sql );
}
}
function bulk_edit($ids) {
foreach ( $ids as $id ) {
$id = absint( $id );
$sql = "SELECT name FROM wp_nc_location WHERE location_id = $id";
$select = $wpdb->query( $sql );
echo 'select: '. $select. ',';
print_r($select);
}
}
However I am getting the following error message when I try to call either bulk_edit or bulk_delete from inside that switch statement above:
Fatal error: Call to undefined function bulk_delete
I realize I am getting something wrong with the scope but I'm not sure where to put the functions bulk_edit or bulk_delete...
I'm guessing from your use of $this-> in various places that those functions belong to a class? In this case, you have to call the function like... $this->bulk_delete(..arguments..);