Skip to content

Instantly share code, notes, and snippets.

View AlKov's full-sized avatar

Alexander AlKov

View GitHub Profile
@AlKov
AlKov / Plugin header
Created October 6, 2017 09:03
Tutorial on how to create simple plugin in Wordpress
<?php
/*
Plugin Name: Display user shortcode
Plugin URI: https://vitorials.net/
Description: Display name and avatar of the logged in users
Version: 1.0
Author: Alex Kovalenko
Author URI: https://vitorials.net/
Text Domain: Display user shortcode
Domain Path: /languages
@AlKov
AlKov / Preven acces to plugin
Created October 6, 2017 09:05
Prevent direct acces to plugin file
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
@AlKov
AlKov / Plugin function
Created October 6, 2017 09:06
Create funcion of the plugin
// display name and avatar
function alex_vitorials_get_user_unique_name(){ }
@AlKov
AlKov / If else statement
Created October 6, 2017 09:08
Check whether our user is logged in
// display name and avatar
function alex_vitorials_get_user_unique_name(){
// check whether our user is logged in or not
if ( ! is_user_logged_in() ) { return; } // if not, return nothing
else { //our code would go here } // return something
}
@AlKov
AlKov / Return user's name
Last active October 6, 2017 09:41
Return nickname of the logged in user
function alex_vitorials_get_user_unique_name(){
if ( ! is_user_logged_in() ) { // check whether our user is logged in or not return; }
// if not, return nothing
else { $user = wp_get_current_user();
$user_nicename = get_userdata( $user-&gt;ID )->user_nicename;
return $user_nicename; } // return users nickname
@AlKov
AlKov / Create shortcode
Last active October 6, 2017 09:42
Create shortcode
add_shortcode( 'user-ID', 'alex_vitorials_get_user_unique_name' );
@AlKov
AlKov / get avatar
Created October 6, 2017 10:22
retreive user avatar
$avatar = get_avatar( $user->ID, 40 );
function alex_vitorials_get_user_unique_name(){
if ( ! is_user_logged_in() ) : ?>
<?php return; ?>
<?php else: ?>
<?php $user = wp_get_current_user();
$avatar = get_avatar( $user->ID, 40 );
$user_nicename = '<span>';
$user_nicename .= get_userdata( $user->ID )->user_nicename;
$user_nicename .= '</span>';
?>
// check for function with the same name
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! function_exists( 'alex_vitorials_get_user_unique_name' ) ) {
function alex_vitorials_get_user_unique_name(){
if ( ! is_user_logged_in() ) {
return;
}
else {
$user = wp_get_current_user();
$avatar = get_avatar( $user->ID, 40 );
@AlKov
AlKov / Plugin to display logged in user's name and avatar
Created October 6, 2017 11:03
This a small bit of code, that displays user's data within [user-ID] shortcode
<?php
/*
Plugin Name: Display user shortcode
Plugin URI: https://vitorials.net/
Description: Display name and avatar of the logged in users
Version: 1.0 Author: Alex Kovalenko
Author URI: https://vitorials.net/
Text Domain: Display user shortcode
Domain Path: /languages
*/