I am fairly new to coding and need a little guidance. I am using the following .php:
// [current_user_display_name]
function display_current_user_display_name () {
$user = wp_get_current_user();
$display_name = $user->display_name;
return $user->display_name;
}
add_shortcode('current_user_display_name', 'display_current_user_display_name');
and would like to style the text with CSS. I'm not sure how to do this, I tried this:
.display_current_user_display_name
{
color:#ffffff;
font-family:lato;
font-size:16px;
font-weight:regular;
}
but it did not work. Thanks!
This could be the PHP code for creating the WordPress shortcode.
Using this shortcode [current_user_display_name] will display an HTML span with the CSS class user-display-name.
We only show this if the user is logged in.
/**
* Current user display name shortcode.
*/
function get_current_user_display_name() {
// Check that the user is logged in.
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
return sprintf( '<span class="user-display-name">%s</span>', $user->display_name );
}
// If user is not logged in don't return anything.
return;
}
add_shortcode( 'current_user_display_name', 'get_current_user_display_name' );
Below is an example of how to style the CSS class .user-display-name:
.user-display-name {
/* CSS code here */
}
Related
I'm trying to customize a shortcode put inside a custom plugin, but I can't get the user id, it always returns me 0.
It might also be okay to understand the role with current_user_can, but any information is always empty.
Here the code:
add_action( 'plugins_loaded', 'check_current_user' );
function check_current_user() {
// Your CODE with user data
global $current_user;
$current_user = wp_get_current_user();
return $current_user->ID;
}
function appp_hide_content_shortcode( $atts, $content = '' ) {
if( class_exists('AppPresser') && AppPresser::is_app() )
return check_current_user();
else
return $content;
}
add_shortcode('appp_hide_content', 'appp_hide_content_shortcode');
Please try this
/**
* Hide content on app.
*
* Use this shortcode to hide content when viewed using the app.
*
* Use:
* [appp_hide_content]This content will not appear on the app.[/appp_hide_content]
*/
function appp_hide_content_shortcode( $atts, $content = '' ) {
ob_start();
//GET CURRENT USER ID
global $current_user;
$current_user = wp_get_current_user();
echo $current_user->ID;
if (current_user_can('free')){
if( class_exists('AppPresser') && AppPresser::is_app() )
echo '';
else
echo $content;
}
$sc_html = ob_get_contents();
ob_end_clean();
return $sc_html;
}
add_shortcode('appp_hide_content', 'appp_hide_content_shortcode');
You need to check the user after the user is loaded. I would hook to init. You can't call the pluggable functions directly in a plugin without delaying the execution. The simpler solution to your problem would be to include your shortcode in your theme's functions.php, rather than in a plugin. But below will execute.
add_action( 'init', 'check_current_user' , 999);
function check_current_user() {
// This returns current user id
return get_current_user_id();
}
function appp_hide_content_shortcode( $atts, $content = '' ) {
if( class_exists('AppPresser') && AppPresser::is_app() ){
// This is only returning an integer here.
return check_current_user();
} else {
return $content;
}
}
add_shortcode('appp_hide_content', 'appp_hide_content_shortcode');
How can I display the user role on a custom page with php? There's one code, but it's just works on the author.php page. How do I run this code on a custom page?
<?php $aid = get_the_author_meta('ID');
$role = get_user_role($aid);
if ('subscriber' === $role)
{
echo "Subscriber";
}
elseif ('editor' === $role)
{
echo "Editor";
}`?>`
Function.php
function get_user_role($id) {
$user = new WP_User($id);
return array_shift($user->roles);
}
In any custom page you can call below three lines to pull up WordPress stack and then use any WordPress functionality
<?
//Imp to include
include('wp-load.php');
define('WP_USE_THEMES', false);
require('wp-blog-header.php');
// check is user is logged - if yes then print its role
if(is_user_logged_in() ) {
$user = wp_get_current_user();
$role = ( array ) $user->roles;
echo "role is ".$role[0];
}
?>
How do I check if a user is admin before running a function in a Wordpress plugin. Something that seems like it should be trivial is a pain.
I've looked at dozens of posts online and can't find one thing that works. I tried the following for example (among half a dozen other things) which is a function in a plugin :
global $current_user;
if ( $current_user->role[0]=='administrator' ) {
function remove_post_metaboxes() {
remove_meta_box( 'formatdiv','album','normal' );
}
add_action('admin_menu','remove_post_metaboxes');
}
<?php if (current_user_can( 'manage_options' )) {
// do stuff
} ?>
$current_user = wp_get_current_user();
// print_r($current_user);
if ($current_user->has_cap('administrator')) {
// do something
echo 'is an admin';
}
So I was doing this wrong, the answer supplied by ReLeaf is partially correct but nobody pointed out that instead of trying to wrap the function like the example I gave in the original question and is why i was getting a blank admin screen :
global $current_user;
if ( $current_user->role[0]=='administrator' ) {
function remove_post_metaboxes() {
remove_meta_box( 'formatdiv','album','normal' );
}
add_action('admin_menu','remove_post_metaboxes');
}
I should have had the conditional inside the function instead :
function remove_post_metaboxes() {
global $current_user;
if ( $current_user->role[0]=='administrator' ) {
remove_meta_box( 'formatdiv','album','normal' );
}
}
add_action('admin_menu','remove_post_metaboxes');
So that's how it's done, thanks me for pointing it out to me ;)
I am developing a plugin for wordpress, I want to find if current user is administrator or not, unfortunately I could not use the current_user_can() as it gives me error, so am using the global $current_user. But I could not get inside the if part even for admin user.. How to fix this?
global $current_user;
if ($current_user->role[0]=='administrator'){
function hide_post_page_options() {
//global $post;
// Set the display css property to none for add category and add tag functions
$hide_post_options = "<style type=\"text/css\"> .jaxtag { display: none; } #category-adder { display: none; } </style>";
print($hide_post_options);
}
add_action( 'admin_head', 'hide_post_page_options' );
}
Try something like the following:
if ( current_user_can( 'manage_options' ) ) {
/* A user with admin privileges */
} else {
/* A user without admin privileges */
}
Read more about the current_user_can function here.
Get the user and check if it has the role adminstrator, like so:
function is_site_admin(){
return in_array('administrator', wp_get_current_user()->roles);
}
if (is_site_admin()) {
echo 'Woot Woot';
} else {
echo 'So sad, I have no rights';
}
This works for me:
global $current_user;
if( !empty($current_user->roles) ){
foreach ($current_user->roles as $key => $value) {
if( $value == 'administrator' ){
Do Something
}
}
}
If it's not a multi-site set-up, you can use this to detect an administrator. If it's multi-site, this will only return true for a super admin.
$user_ID = get_current_user_id();
if($user_ID && is_super_admin( $user_id )) {
Do Something
}
I know it is an old question but I would like to make this page more useful by addressing the actual issue. The actual issue here is that OP hasn't been able to use current_user_can( 'manage_options' ) in his plugin. Using the function raises the usual undefined function... PHP error. This happens because the plugin gets initialized before WP core completes loading. Fix is very simple. Loading the plugin at appropriate time is the key.
Assuming the admin plugin code resides inside a class MyPlugin, the class initialization should be hooked to init. Following is one way of doing it.
/**
* Plugin Class
*/
class MyPlugin{
public function __construct(){
/* all hooks and initialization stuff here */
/* only hook if user has appropriate permissions */
if(current_user_can('manage_options')){
add_action( 'admin_head', array($this, 'hide_post_page_options'));
}
}
function hide_post_page_options() {
// Set the display css property to none for add category and add tag functions
$hide_post_options = "
<style type=\"text/css\">
.jaxtag { display: none; }
#category-adder { display: none; }
</style>";
print($hide_post_options);
}
}
add_action('admin_init', function(){
$myplugin = new MyPlugin();
});
This is a way of making sure that wordpress core is available to the plugin function.
You can find admin_init documentation here.
P.S. You should look into using PHP HEREDOC. It is a very simple way of writing multi-line strings. Your style block can be re-written as follows
$hide_post_options = <<<HTML
<style type="text/css">
.jaxtag { display: none; }
#category-adder { display: none; }
</style>
HTML;
I hope it helps somebody.
Thanks.
Too late for an answer for this question, but I think it might be useful anyway if someone ends up here like me.
I needed a quick solution to this problem - check if the current user is admin or not.
From the WP codex I got a simple solution which is to use..
if(is_super_admin($user_id)) {
// do stuff for the admin user...
}
According to WP-Codex this function returns True if the currently logged in user is network (super) admin. This function returns True even if the network mode is disabled but the current user is admin.
<?php
if( current_user_can( 'administrator' ) ){} // only if administrator
if( current_user_can( 'editor' ) ){} // only if editor
if( current_user_can( 'author' ) ){} // only if author
if( current_user_can( 'contributor' ) ){} // only if contributor
if( current_user_can( 'subscriber' ) ){} // only if subscriber
?>
More info here: How To Check If User Is Administrator Or Editor In WordPress
use this code, I hope this solve your problem
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
echo trim($user_role);
$user=wp_get_current_user();
if(in_array("administrator", $user->roles)){
//user role is admin
}
I have made a public account in wordpress which I will send to 100 users.
So the login would be:
Username: public
Password: 123example
The only thing I want is to hide the profile page for this specific user account so they can't change password, emailadress, etc.
How to achieve this? Maybe change some php?
The last portion in #aSeptik's answer could be a little more WP friendly.
function force_profile_redirect() {
global $pagenow, $current_user;
get_currentuserinfo();
if ($pagenow == 'profile.php' && $current_user->user_login == 'public') {
wp_redirect(home_url());
}
}
add_action('admin_init', 'force_profile_redirect');
this script cover all the aspects of the question, read the code comments for further explanation.
<?php
/**
* this make sure the public user where redirected
* to home instead of profile page
*/
function redirect_user_to($redirect_to, $request, $user)
{
global $user;
if ($user->user_login == 'public') {
return home_url();
}
else {
return home_url("/wp-admin/");
}
}
add_filter('login_redirect', 'redirect_user_to', 10, 3);
/**
* this remove the profile links from
* the top nav menu
*/
function remove_edit_profile()
{
global $wp_admin_bar, $current_user;
get_currentuserinfo();
if ($current_user->user_login == 'public') {
$wp_admin_bar->remove_menu('edit-profile');
$wp_admin_bar->remove_menu('my-account-with-avatar');
$wp_admin_bar->remove_menu('my-account');
}
}
add_action('wp_before_admin_bar_render', 'remove_edit_profile', 0);
/**
* this remove the "Site Admin" link from
* the WP meta widget, usually placed in
* the side bar.
*/
function my_unregister_widgets()
{
unregister_widget('WP_Widget_Meta');
register_widget('MY_Widget_Meta');
}
add_action('widgets_init', 'my_unregister_widgets');
class MY_Widget_Meta extends WP_Widget
{
function MY_Widget_Meta()
{
$widget_ops = array(
'classname' => 'widget_meta',
'description' => __("Log in/out, admin, feed and WordPress links"),
);
$this->WP_Widget('meta', __('Meta'), $widget_ops);
}
function widget($args, $instance)
{
extract($args);
$title = apply_filters('widget_title', empty($instance['title']) ? __('Meta') : $instance['title']);
echo $before_widget;
if ($title) {
echo $before_title.$title.$after_title;
}
?>
<ul>
<?php
global $current_user;
get_currentuserinfo();
if ($current_user->user_login == 'public') {
}
else {
wp_register();
}
?>
<li>
<?php wp_loginout();?>
</li>
<li>
<a href="<?php bloginfo('rss2_url');?>" title="<?php echo esc_attr(__('Syndicate this site using RSS 2.0'));?>">
<?php _e('Entries <abbr title="Really Simple Syndication">RSS</abbr>');?></a>
</li>
<li>
<a href="<?php bloginfo('comments_rss2_url');?>" title="<?php echo esc_attr(__('The latest comments to all posts in RSS'));?>">
<?php _e('Comments <abbr title="Really Simple Syndication">RSS</abbr>');?></a>
</li>
<li>
WordPress.org
</li>
<?php wp_meta();?>
</ul>
<?php
echo $after_widget;
}
}
/**
* this prevent from non authorized user ( public )
* to pointing to the profile page by writing into
* the address bar.
*/
function force_profile_redirect()
{
global $pagenow, $current_user;
if (strtolower($current_user->user_login) == 'public') {
wp_redirect(home_url());
}
}
add_action('admin_init', 'force_profile_redirect');
?>
You'd need to modify your profile page code, to make it not show the editable areas, and not run the "update profile" action, if the user ID is [xyz].
For the page which actually does the updating of the profile, you can just put at the top something like
// Change this line to match however you identify your logged-in user
// And change the id number to the ID of the public user
global $current_user;
get_currentuserinfo();
if ($current_user->ID == 1)
{
// Stop them seeing this page
header('Location: index.php');
// And for good measure
die();
}
For the page on which they can change the profile fields before they submit the form, you can do something like this
// Change this line to match however you identify your logged-in user
// And change the id number to the ID of the public user
global $current_user;
get_currentuserinfo();
if ($current_user->ID == 1)
{
// Say no
echo '<p>You cannot edit your profile on this account.</p>';
// And for good measure
die();
}
Without seeing your code, it's hard to be more specific, but this should work at a push, even if it's not exactly how you want it to work.