Getting an author's role in Wordpress - wordpress

i'm working on my first WP site and need to display an author's role next to their post. Something like "Jimmy | Administrator". Looking at the author metadata available: http://codex.wordpress.org/Function_Reference/the_author_meta doesn't give me a way to access that. I'm sure there's a quick easy way to do this and i just don't know it!! Thanks!

UPDATE: Place this in your functions.php file:
function get_author_role()
{
global $authordata;
$author_roles = $authordata->roles;
$author_role = array_shift($author_roles);
return $author_role;
}
Then call this within your Wordpress Loop. So:
<?php
if(have_posts()) : while(have_posts()) : the_post();
echo get_the_author().' | '.get_author_role();
endwhile;endif;
?>
...will print: 'Jimmy | Administrator'
COMPLETE ANSWER: The User Object itself actually stores roles, and other kinds of useful information. If you want more of a general function to retrieve the role of any given user, simply pass in the ID of the user you want to target with this function:
function get_user_role($id)
{
$user = new WP_User($id);
return array_shift($user->roles);
}
And if you want to grab the author of a given post, call it like so:
<?php
if(have_posts()) : while(have_posts()) : the_post();
$aid = get_the_author_meta('ID');
echo get_the_author().' | '.get_user_role($aid);
endwhile;endif;
?>
RESPONSE TO LAST COMMENT:
If you need to grab data outside of the Wordpress Loop (which I imagine you're trying to do on an Archive and Author page), you can use the function from my Complete answer like so:
global $post;
$aid = $post->post_author;
echo get_the_author_meta('user_nicename', $aid).' | '.get_user_role($aid);
That will output the information you want in your "user | role" format.

Related

ACF User fileld for subscriber not return value

I'm using WordPress ACF plugin to store users' data.
The values I'm calling in template are as below:
echo $uid=get_current_user_id();
echo $lesson_order = get_field('lesson_order', 'user_'.$uid);
echo $last_lesson_time = get_field('last_lesson_time', "user_".$uid);
The values returned for admins are correct, but when logged in as a subscriber, the code returns empty values. I also checked user ids, which appear to be correct.
Can someone give me a solution to this. Thanks a lot.
I've tested both option on a local project and both were working but have you tried retrieving the user ID using this instead?
<?php
$current_user = wp_get_current_user();
echo "Current User ID " . $current_user->ID;
?>
If it still doesn't work, can you show a little more of your code?
You can also check that answer for more information : https://wordpress.stackexchange.com/questions/163407/get-current-user-id-returns-0

How to get the default user data to a bitrix form

I am working on a bitrix site. A form was created linking into an information block that has a sorting of 100.
<? echo print_input($arParams, $arResult, 100) ;?>
I wanted to use the default user data like name, last name, email etc. of the current log user. How do I approach that?
Any help would much be appreciated.
On every page - $USER object is available - it contains information about current logged in user.
Contains this methods to retrieve info about current user:
GetID
GetLogin
GetEmail
GetFullName
GetFirstName
GetLastName
GetParam
GetUserGroupArray
GetUserGroupList
GetUserGroupString
Or you can use it like this
<?php
global $USER;
$rsUser = CUser::GetByID($USER->GetID());
$arUser = $rsUser->Fetch();
// print current user info
echo "<pre>"; print_r($arUser); echo "</pre>";
?>
here is more info about CUser class in official documentation (Russian)

Wordpress get_categories can not find out children directory

I had a wordpress problem which troubled me a lot. I writed a php script that can insert my specific data into the database of wordpress.
Actually, I deleted the initial data of wordpress and used the script to scan my directories to initialize the tables in the database.
However, I encounterd a problem that I used the get_categories() function to find out children directories but failed. Following is my code:
$args = Array('parent'=>'1', 'hide_empty'=>0);
echo count(get_categories($args));
The fact is that there are some directories in the category with number 1, but it prints 0.
So I used cat_is_ancestor_of function to test:
echo cat_is_ancestor_of(1, 2);
It prints 1 which shows that category 2 is children of category 1. I watched the mysql database, category 2 is indeed the son directory of category 1.
But why get_categories returns the wrong answer? It puzzled me a lot! Could anyone help me solve it?
you can get the child category of a parent category by the following way
<?php
$subcategories = get_categories('&child_of=1&hide_empty'); // List subcategories of category '4' (even the ones with no posts in them)
echo count($subcategories);
echo '<ul>';
foreach ($subcategories as $subcategory) {
echo sprintf('<li>%s</li>', get_category_link($subcategory->term_id), apply_filters('get_term', $subcategory->name));
}
echo '</ul>';
?>

wordpress page id in friendly url

How to get the page id in wordpress in the case of friendly url?
Inside the Loop: http://codex.wordpress.org/Function_Reference/the_ID
Outside the Loop: global $wp_query; $id = $wp_query->post->ID;
I'm guessing you're talking about doing it via the Admin interface. Can't you just add it into the custom format textbox using the %post_id% string:
/archives/%post_id%
If you are within the Loop (most of the time meaning within the while(have_posts()) you should be able to use: global $post; $id = $post->ID; to get the ID

Add Meta Post function not working

I am using add post meta function to save some data and its not working
<?php
//include '../../../wp-blog-header.php';
$unique = "true";
$pageID = $_GET['postID'];
echo "pageID:";
echo $pageID;
echo "</br>";
$num_posts = $_GET['num_posts'];
echo "num_posts: ";
echo $num_posts;
echo "</br>";
$num_posts_meta_key = "num_posts";
add_post_meta($pageID, $num_posts_meta_key, $num_posts , $unique) or update_post_meta($pageID, "num_posts" , $num_posts);
?>
Can someone help me out?
In first page I am getting all values from textboxes or checkboxes in javascript and then i am passing it in URL to next page where add_post_meta function is there.
I tried using method POST ...but then it doesnt work for me. It just submit the page and come back w/o doing anything on 1st page. I tried with GET method..but nothing works.
Hence I decided to take all values like num of post, post id in javascript and then from there pass it with url by using window.location.
I am very new to wordpress plugin coding. I thought POST method in my plugin is conflicting with some other post method in post.php..not sure though..
I am writing plugin for admin panel.
not sure what your problem is.. are you sure you're passing the right postID parameter? does the post exist in the database?
You don't really need to do add_post_meta() or update_post_meta.
From the manual:
The first thing this function will do
is make sure that $meta_key already
exists on $post_id. If it does not,
add_post_meta($post_id, $meta_key,
$meta_value) is called instead and its
result is returned.
<?php
// This minimum code should work, though you should really check that a post
// with this id does exist.
update_post_meta($_GET['postID'], "num_posts" , $_GET['num_posts']);
?>

Resources