Ranford
Ranford

Reputation: 19

Modifying the role of a wordpress user using SQL

Im trying to change the role of users after 24 hours from when they logged in on a page. The user and password are in my database already and im just waiting for them to log in to change the role 24 hours later. Im just figuring out how to select only wp_capabilities from all the other tables but I dont know how to change it

This is the original code

<?php
    $servername = 'localhost';
    $username = 'root';
    $password = '';
    $dbname = 'wp';

    $conn = mysqli_connect( $servername, $username, $password, $dbname );

    if( $conn->connect_error ) {
      die ( "Conection Failed: " . $conn->connect_error );
    }

    $sql = "SELECT wp_users.ID, wp_users.user_nicename
            FROM wp_users INNER JOIN wp_usermeta
            ON wp_users.ID = wp_usermeta.user_id
            WHERE wp_usermeta.meta_key = 'wp_capabilities'
            AND wp_usermeta.meta_value LIKE '%subscriber%'
            SET wp_usermeta.meta_value='meh' ";

    if ( $conn->query($sql) === TRUE ) {
      echo "yay";
    } else {
      echo "oh no: " . $conn->error;
    }
   ?>

It ain't much, but its honest work

It shows me this error:

oh no: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SET wp_usermeta.meta_value='meh'' at line 6

I would appreciate any help, because this is my first SQL project

Upvotes: 0

Views: 1569

Answers (1)

Ranford
Ranford

Reputation: 19

As FluffyKitten said, built-in WP functions are amazing. In case someone wants the answer

$user_id = 2;
$new_value = array( 'subscriber' => 1 );
$updated = update_user_meta( $user_id, 'wp_capabilities', $new_value );

And you can change the arrary and write any role

Upvotes: 1

Related Questions