PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label meta-key. Show all posts
Showing posts with label meta-key. Show all posts

Monday, October 24, 2022

[FIXED] How to update user meta for multiple meta_key in wordpress

 October 24, 2022     meta-key, php, wordpress, wordpress-theming     No comments   

Issue

I'm trying to update multiple meta_key for user in WordPress

update_user_meta( $user_id, array( 'nickname' => $userFirstName, 'first_name' => $userFirstName, 'last_name' => $userLastName , 'city' => $userCityID , 'gender' => $userGenderID) );

but it is not working. How can we update multiple meta_key for user?


Solution

Try:

<?php
$user_id = 1234;

$metas = array( 
    'nickname'   => $userFirstName,
    'first_name' => $userFirstName, 
    'last_name'  => $userLastName ,
    'city'       => $userCityID ,
    'gender'     => $userGenderID
);

foreach($metas as $key => $value) {
    update_user_meta( $user_id, $key, $value );
}

So instead of passing your array to update_user_meta which only accepts string arguments for $meta_key, loop over the array and call update_user_meta for each key/value pair in the array.

EDIT:

WordPress doesn't give a built in way to update multiple metas at once. Part of the reason for using their built in function is because filters and hooks can be registered to operate on the meta information. These won't be called if you update them directly.

That said, you can try something like this (code untested):

$columns  = implode(" = '%s', ", array_keys($metas)) . " = '%s'";
$values   = array_values($metas);
$values[] = $user_id;
$table    = _get_meta_table('user');
$sql      = "UPDATE $table SET $columns WHERE user_id = %d";
$wpdb->query(
    $wpdb->prepare($sql, $values)
);


Answered By - drew010
Answer Checked By - Pedro (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Friday, August 26, 2022

[FIXED] how to access meta value under image link in wordpres

 August 26, 2022     meta-key, metadata, php, wordpress     No comments   

Issue

**meta_value**:a:1:{i:0;s:105:"http://localhost/wordpress/wp-content/uploads/event-manager-uploads/event_banner/2020/07/diabetic_1-3.jpg";}

meta_key:_event_banner

while($res = mysqli_fetch_array($query)) 
                {
                    $i++;
                    // $img_src=wp_get_attachment_image_src(get_post_thumbnail_id($res['image']));
                    // $img_src_url=$img_src[0];
                    $id=$res['post_id'];
                    $post = get_post($res['post_id'] );
             
                  ?>
                    <div class="maindiv">
                  <div class="notification">
                      <img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id($id) );?>"></td>
                  <img src="<?php echo $post->_event_banner;?>"></td> 
                  </div>
                  <div class="notification1">
                  <h2><a href="<?php echo $res['permalink'] ?>"><?php echo $res['post_title']?></a></h2>
                  <h6><?php echo $res['date']?></h6>
              </div>
            </div>
                <?php
            }

how to display image give me some reference I need help on how to access the image in meta key use and filter I have not idea in meta key use give me idea... it's very important


Solution

So not sure how you're saving your meta_key in the first place? It looks like what you have is a serialized array with just a single, numerically indexed entry. So what you probably want to do is change wherever that's saved to just store the URL?

That said, you could access that meta value like this:

$banner_src = maybe_unserialize( get_post_meta( $post->ID, '_event_banner', true ) );
$banner_src = is_array( $banner_src ) ? $banner_src[0] : $banner_src;

And then display the banner like:

<img src="<?php echo $banner_src; ?>" />


Answered By - JBoss
Answer Checked By - Marie Seifert (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Wednesday, February 2, 2022

[FIXED] Undo the effects of changing meta_key from Varchar to int

 February 02, 2022     database, meta-key, phpmyadmin, wordpress     No comments   

Issue

I was trying to edit the email form field so that it accepts int values like of a phone number. I knew very less about how database values work and this is what i did: 1. Figured out that email field was "hrb_email" 2. Found that in wp_metauser hrb_email is a meta_key 3. Went to structure> and changed the meta_key from varchar to int.

Result: I cant find hrb_email in wp_metauser any more. Wordpress dashboard shows no users. Although wp_users has my user details. Also, on my website, I can see user if I manually put the URL for those users but I cant find them on the list on my website.

Please advice. Note: changing the meta_key value back to varchar does not help.

This is what meta_key looks likeChanged back to varcharI cant find hrb)email under meta_key anymore


Solution

Updated the meta_key fields manually for each user. To achieve this, I created a couple of new users and understood what values belonged to the key fields. From that, i started updating the meta_key field manually. It worked for all the users, except that i had to update password on Wordpress>dashboard>users>edit.



Answered By - shrbisht
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home
View mobile version

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing