Get author's avatar inside a MySQL query in WordPress - wordpress

I am fetching authors ID, points from wp_cp table using following code in my WordPress site and it works perfectly. Which I am trying now is to get users avatar beside users ID. How I can get users avatar inside the code?
<?php
global $wpdb;
/* wpdb class should not be called directly.global $wpdb variable is an instantiation of the class already set up to talk to the WordPress database */
$result = $wpdb->get_results( "SELECT uid,sum(points) as pt FROM wp_cp where timestamp between '2015-12-12' and '2015-12-31' group by uid "); /*mulitple row results can be pulled from the database with get_results function and outputs an object which is stored in $result */
//echo "<pre>"; print_r($result); echo "</pre>";
/* If you require you may print and view the contents of $result object */
echo "uid"." "."pt"."<br><br>";
foreach($result as $row)
{
echo $row->uid." ".$row->pt."<br>";
}
/* Print the contents of $result looping through each row returned in the result */
?>

If you use the uid field (which matches the user's ID in wp_users), pass it into the get_avatar function:
https://codex.wordpress.org/Function_Reference/get_avatar

Add the following code to get user avatar inside the foreach loop.
echo get_avatar( $row->uid );
Since you are asking for size, you can add a parameter to this function.
echo get_avatar( $row->uid, 64 ); // So the avatar size will be 64px X 64 px.
For further reference check this URL

Related

How to bulk change external column values defined in Orders table and stored in database?

I am working on return processes in Woocommerce. I am getting returned or not returned information from table named return_table. I added this information to a new column in the orders table. The table is simply as follows:
I need to make changes to the column I just added. For example, I want to update the value from no return to returned. For this, I should make a new definition in the default actions column or add a new bulk action. I decided to add bulk action as it is more functional:
However, I was unable to make any progress after that. I've done research on Stackoverflow or other platforms. I found an answer to add buttons (new actions) but still couldn't solve the problem:
Source-1
Others I've found are mostly geared towards changing the "Order Status":
Source-2, Source-3, Source-4...
So, i've been researching this for a while and haven't found a solution. I will be grateful if anyone can help.
My Code:
<?php
// Add new column
add_filter( 'manage_edit-shop_order_columns', 'return_column', 11);
function return_column( $columns ) {
$columns['return_column'] = 'Return Status';
return $columns;
}
// Add the data to the custom columns
add_action( 'manage_shop_order_posts_custom_column', 'return_column_content',11);
function return_column_content ($column){
if($column == 'return_column'){
// Create required global variables
global $post, $wpdb;
$order = wc_get_order( $post->ID );
$order_id = $order->get_id();
$result = $wpdb->get_results("SELECT return_status FROM {$wpdb->prefix}return_table WHERE order_id ='$order_id' LIMIT 1");
if(count($result)):
echo $result[0]->return_status;
else:
echo '-';
endif;
}
}
//Add new order status to bulk change
add_filter( 'bulk_actions-edit-shop_order', 'refund_register_bulk_action' );
function refund_register_bulk_action( $bulk_actions ) {
$bulk_actions['mark_returned'] = 'Returned';
$bulk_actions['mark_not_returned'] = 'Not Returned';
return $bulk_actions;
}

How to access Wordpress' global $wpdb from PHP?

I'm trying to execute a query in PHP against a MySQL database. My code is below:
<?php
require_once ("wp-includes/wp-db.php");
global $wpdb;
$myrows = $wpdb->get_results( "SELECT id, name FROM wp_db_posts" );
echo $myrows;
?>
After executing this code, I get a Fatal error: Call to a member function get_results() on null in C:\Apache24\htdocs\wordpress\me.php on line 4
I'm actually new to WordPress and want to know exactly how can I access the $wpdb global variable from PHP. My databses are pre-fixed with wp_db_.
What am I doing wrong? Can anyone help me here?
assuming you script placed in the root directory of WordPress you should load wordpress not access the wp-db.php directly so your code should be like this:
<?php
require_once "wp-load.php";
global $wpdb;
$myrows = $wpdb->get_results( "SELECT ID, post_name FROM {$wpdb->prefix}posts" );
and you can't echo myrows as you will get object not string you should use instead
var_dump() or print_r()

how do I insert a row into a custom table in Wordpress on s2member paid member activation?

I have a custom table my_users.
When a person signs up for membership to my site through s2member, I want to get the WordPress wp_user id and insert a new row into my_users.
Where do I do this?
Thanks in advance.
This will help you to save data in your custom table if you save table with wp prefix you can include that with table name.
<?php global $wpdb;
// for get user id
$user_id = get_current_user_id();
$arrayinsert = array('user_id'=>$user_id,'Type_User'=>get_current_user_role()); ?>
$insert_result = $wpdb->insert('my_users',$arrayinsert);
if ($insert_result) {
$varId = $wpdb->insert_id;
} else {
echo "Error:". $wpdb->last_query;
}
?>

Wordpress XML-RPC post success but don't work with long body

I use code below to post via XML-RPC, it does success. But when I send a long string $bodypost, my post doesn't have body content. I test it with html code, it's working, then I remove all space, my $bodypost just have 1 line with about 4000 words, it's not working.
How can I fix it?
<?php
function send_post($titlepost, $bodypost, $categorypost, $keywordspost )
{
require_once("IXR_Library.php.inc");
$encoding='UTF-8';
$client->debug = false; //Set it to false in Production Environment
$title= $titlepost; // $title variable will insert your blog title
$body= $bodypost; // $body will insert your blog content (article content)
$category=$categorypost; // Comma seperated pre existing categories. Ensure that these categories exists in your blog.
$keywords=$keywordspost;
$customfields=array('key'=>'Author-bio', 'value'=>'Autor Bio Here'); // Insert your custom values like this in Key, Value format
$title = htmlentities($title,ENT_NOQUOTES,$encoding);
$keywords = htmlentities($keywords,ENT_NOQUOTES,$encoding);
$content = array(
'title'=>$title,
'description'=>$body,
'mt_allow_comments'=>0, // 1 to allow comments
'mt_allow_pings'=>0, // 1 to allow trackbacks
'post_type'=>'post',
'mt_keywords'=>$keywords,
'categories'=>array($category),
'custom_fields' => array($customfields)
);
// Create the client object
$client = new IXR_Client('http://www.domain.com/xmlrpc.php');
$username = "abc";
$password = "abc";
$params = array(0,$username,$password,$content,true); // Last parameter is 'true' which means post immideately, to save as draft set it as 'false'
// Run a query for PHP
if (!$client->query('metaWeblog.newPost', $params)) {
die('Something went wrong - '.$client->getErrorCode().' : '.$client->getErrorMessage());
}
else
echo "Article Posted Successfully";
}
?>
I found how to fix it. This code does not work if my body code is give by source code of default Wordpress editor, but if I change it to CkEditor it's working!

Drupal: Access $profile from a block

I'm trying to get the avatar (profile picture) located in the $profile array to appear in a BLOCK. The variable $profile is not accessible from blocks. It's scope is only in that actual user-profile.tpl.php file. So... does anybody know how I can execute something like this:
print $profile[user_picture];
in a drupal BLOCK?
I figured i might as well post it here as well. See my second comment on the first thread in this discussion. Below is my code I used with INSERT VIEW to get what I wanted:
<?php
$profileUser = "";
if (arg(0) == "user") {
$profileUser = arg(1);
}
// removed some other checks i do to populate $profileUser
?>
[view:VIEWED_PROFILE_AVATAR=block=<?php print $profileUser; ?>]
I hope that helps someone.
You can try using the following code in a new block (admin/build/block/add):
<?php
global $user;
$output = theme_image($user->picture, $alt = 'user pic', $title = 'user pic');
print $output;
This gives you access to the global $user variable and then you can use the picture property to get the URL for the current users profile picture.

Resources