How to rename the users column in WordPress database - wordpress

I have this specific requirement using shared database which is shared by laravel. In the db table, all the registered username records gets stored in 'username' column (under users table), so I need to retrieve the username to login in another app that is built in WordPress. By default WordPress retrieve username from 'user_login' column, so what I am looking here is pick username from 'username' column instead of 'user_login'.. This is how I am trying to filter
add_filter( 'manage_users_columns', 'so_25737835' );
function so_25737835( $columns )
{
$columns['user_login'] = 'username';
return $columns;
}
If anyone has something to share then it will be nice.

Related

How to delete specific users in bulk in WordPress?

How can WordPress delete users in bulk when the user_id is already known?
Is there an easy way to do this? For example SQL CLI or PHP CLI or something like that. Has anyone already done this?
Use the WordPress CLI. There's a user delete command which can accept a list.
Examples from the docs:
# Delete user 123 and reassign posts to user 567
$ wp user delete 123 --reassign=567
Success: Removed user 123 from http://example.com
# Delete all contributors and reassign their posts to user 2
$ wp user delete $(wp user list --role=contributor --field=ID) --reassign=2
Documentation: https://developer.wordpress.org/cli/commands/user/delete/
Try the following snippet to delete specific users whose ids are known.
function delete_specific_users() {
//Include the user file with the user administration API
require_once( ABSPATH . 'wp-admin/includes/user.php' );
//Get a list of users that belongs to the specified role
$users = get_users( array( 'ID' => array( comma separated list of user_ids you want to delete ) ) );
//Delete all the user of the specified role
foreach ( $users as $user ) {
wp_delete_user( $user->ID );
}
}
// call the method like this.
delete_specific_users();

WooCommerce REST Client API - Programmatically get consumer key and secret

I am currently using the client-API to implement a simple user front-end to upload products. The function client->products->create() seems to work fine, how ever I can’t get around one issue.
Every time I upload a product, the vendor is set to the admin user instead of the user that is currently logged in. Is there a way to set the vendor through the API? Has anybody get done this?
This is the function I created that is called by AJaX when the form is submitted (I left key and website fields empty here on purpose):
function addProduct()
{
$options = array(
'debug' => false,
'return_as_array' => false,
'validate_url' => false,
'timeout' => 30,
'ssl_verify' => false,
);
try {
$client = new WC_API_Client('', '', '', $options);
$productName = $_POST["productname"];
$price = $_POST["price"];
$discountPrice = $_POST["discountPrice"];
$description = $_POST["description"];
$shortDescription = $_POST["shortDescription"];
$authorId = 5;
$client->products->create(array('title' => $productName, 'type' => 'simple', 'regular_price' => $price, 'description' => $description));
} catch (WC_API_Client_Exception $e) {
echo $e->getMessage() . PHP_EOL;
echo $e->getCode() . PHP_EOL;
if ($e instanceof WC_API_Client_HTTP_Exception) {
print_r($e->get_request());
print_r($e->get_response());
}
}
echo ("Publicado" . $authorId);
// Una función AJaX en WordPress debe siempre terminarse con die().
die();
}
The problem seems to be the consumer key and consumer secret, so, is there a way to programmatically provide the clients with API keys and get these dynamically?
UPDATE: The method to obtain the consumer key described below will not work; it is no longer possible to get hold of the consumer key from the database once it has been generated. The consumer key stored in this new table is not the same consumer key that is generated in the admin screens and passed out to the end user. It appears to be an SHA256 hashed a version of this key. This is more secure (previously the consumer key and secret stored in wp_usermeta was tantamount to storing clear-text passwords, as anyone with access to that data would be able to log into the API as any of those users), but is a little less convenient. Win some, lose some, but win on security.
Your new WC_API_Client() will take three parameters before the options: $store_url, $consumer_key and $consumer_secret.
Any user on the WC shop who is to be used to access the API will need a consumer key or consumer secret. The consumer key will identify which user the API will run as, and it is that user which will be linked to any entities created through the API.
Until recently, you could get these two pieces of information for a user like this:
$consumer_key = get_user_meta($user_id, 'woocommerce_api_consumer_key', true);
$consumer_secret = get_user_meta($user_id, 'woocommerce_api_consumer_secret', true);
Where $user_id is the ID for the user that will be creating items. If you want the current logged in user to be able to create items in their name then that user would need to be given a consumer key and secret, and would need to be in an appropriate WC/WP group to give them permission to do so.
Note, that if you do this, then the user will also have access to the admin pages for WC to create these items, and not just through the API.
In later versions of WC, the user meta items have been moved to a separate table: wp_woocommerce_api_keys so you need to look in there instead of in the user meta.
This will get you the consumer key and secret for a given user ID:
global $wpdb;
$key = $wpdb->get_row( $wpdb->prepare("
SELECT consumer_key, consumer_secret, permissions
FROM {$wpdb->prefix}woocommerce_api_keys
WHERE user_id = %d
", $user_id), ARRAY_A);
the results being something like this:
array(3) {
["consumer_key"]=>
string(64) "58043812eee6aa75c80407f8eb9cec025825f138eb7d60118af66cf4b38060fa"
["consumer_secret"]=>
string(43) "cs_1da716412bb9680d8b06b09160872b7e54416799"
["permissions"]=>
string(10) "read_write"
}
I am, of course, assuming you are using the API to "loop back" to the current site and not accessing a remote site. Using the WC API to create products even on the current site can be very much more convenient than going through the PHP object API.
I have not yet found any public WC methods to get these details; they are all private and assume only WC needs to know these details.
Yes there is a fine customization that you need to do in your code that is as follows:
Background information:
Each users Consumer Key,Consumer Secret Key and read/write permissions (if WooCommerce API Keys are generated for that users) are stored in wordpress's usermeta table with a meta_keys as 'woocommerce_api_consumer_key', 'woocommerce_api_consumer_secret' and 'woocommerce_api_key_permissions' respectively.
So you just need to get the current users id first then get that user's meta value as mention above assign to some variables and send them as a parameter.
I think the problem is generate programmatically the API keys for that customer for witch you want consume the woocommerce service, because the keys ar owned for each users and there aren't be useful for other users.
My advice is looking admin source code of woocommerce.

how should i get all posts for particular user role in wordpress through plug-in

i am using wordpress database as a backend for android application and i have 2 custom roles in the application one is management and second is user.and i am using members plugin for content permission in wordpress which allows to add permissions.now from my plugin i want to get all the posts for users which has management role.these request will be come as a REST based URL and return json data.
try this
create a function to alter the query's where clause:
function authors_where_filter( $where ) {
global $wpdb;
$ids = get_users(array('role' => 'author' ,'fields' => 'ID'));
$where .= " AND post_author IN ($ids)";
return $where;
}
and then before you query just hook it ex:
add_filter('posts_where','authors_where_filter');
$all_posts = new WP_Query(array('posts_per_page' => -1 .....
remove_filter('posts_where');
and you should get all posts of author users in a single query, (well two actually one to get the users and the other is to get the posts)
Source

WordPress: get all meta data when user registers

I have a user registration form in the front end (in the Users admin section as well) with three extra fields (apart from default ones): birthday, country, language. their values are stored in usermeta table.
I have this action hook to retireve all meta data for the registered user:
add_action('user_register', 'new_user_func');
// user registration callback function
function new_user_func($userID) {
$newUser = get_user_meta( $userID );
$userMeta = array();
foreach ($newUser as $key => $value) {
$userMeta[$key] = $value[0];
}
//do something with $userMeta...
}
var_dump($userMeta) after submit doesn't give me the extra fields value though.. only defaults (first name, last name etc)
Anyone know what might be the case?
Did you try getting the values with:
$meta = get_the_author_meta($meta_key, $user_id);
Perhaps the meta values you add yourself isn't supported by get_user_meta() .
If this don't work either, perhaps you need to look on how you went about creating the new meta fields. Theres a pretty decent tutorial on how to do it here:
http://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields
Read de Codex entry for user_register action, it says:
Not all user metadata has been stored in the database when this action is triggered.

How to connect to multiple databases within Drupal

I want to deploy openpublish on about 5 drupal multisite by high traffic on website and want to use multiple database server but some tables should be shared.
How to connect to multiple databases within Drupal by making share on 'users', 'sessions' and 'role' tables.
To allow multiple database connections, convert $db_url to an array.
<?php
$db_url['default'] = 'mysql://drupal:drupal#localhost/drupal';
$db_url['mydb'] = 'mysql://user:pwd#localhost/anotherdb';
$db_url['db3'] = 'mysql://user:pwd#localhost/yetanotherdb';
?>
To query a different database, simply set it as active by referencing the key name.
<?php
db_set_active('mydb');
db_query('SELECT * FROM table_in_anotherdb');
//Switch back to the default connection when finished.
db_set_active('default');
?>
But make sure all databases are of same kind.
The documentation on sharing tables says that you have to use a single database to share tables, but I've done it with multiple databases on the same server. I've done this by putting the database name before the prefix then adding a dot. So if your default database is called 'drupal', and your second database is called 'second_drupal', the prefixes would look like this in settings.php:
$db_prefix = array(
"default" => "slave1_", // the prefix for tables that are not shared.
"users" => "second_drupal.master_",
...
(Note that it doesn't matter what you call your default database, since it's the default database, you don't need to refer to it by name in the $db_prefix variable, as long as it's setup correctly in the settings.php file.)
http://thedrupalblog.com/setting-multi-site-drupal-6-installation-shared-databases-and-single-sign has some instructions, but I think that still assumes a single database server.
Sum times i wanted to retrieve data from other database in drupal then, i created a function in selected theme folder inside template.php file
connect_to_database($un, $pass, $db, $insert)
it takes four parameters of the database username, password, databasename, and last is query
function connect_to_database($username,$password,$database,$query){
$database_info = array(
'host' => 'hosname',
'database' => $database,
'username' => $username,
'password' => $password,
'driver' => 'mysql'
);
Database::addConnectionInfo('coreapp', 'default', $database_info);
db_set_active('newcon');
$q = db_query($query);
db_set_active('default');
return $q;
}

Resources