Category Archives: PHP

WordPress Check User Role Function

/**
 * Checks if a particular user has a role. 
 * Returns true if a match was found.
 *
 * @param string $role Role name.
 * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
 * @return bool
 */
function appthemes_check_user_role( $role, $user_id = null ) {

    if ( is_numeric( $user_id ) )
	$user = get_userdata( $user_id );
    else
        $user = wp_get_current_user();

    if ( empty( $user ) )
	return false;

    return in_array( $role, (array) $user->roles );
}

// example use for the current user
if ( appthemes_check_user_role( 'customer' )
    _e( "You've got access dude!", 'appthemes' );
else
    _e( "Sorry man, no luck.", 'appthemes' );

// example use for a specific user
$user_id = 23;

if ( appthemes_check_user_role( 'customer', $user_id )
    _e( "You've got access dude!", 'appthemes' );
else
    _e( "Sorry man, no luck.", 'appthemes' );

 

How to change contents of a dashboard help tab

source

//hook loading of new page and edit page screens
add_action('load-page-new.php','add_custom_help_page');
add_action('load-page.php','add_custom_help_page');

function add_custom_help_page() {
   //the contextual help filter
   add_filter('contextual_help','custom_page_help');
}

function custom_page_help($help) {
   //keep the existing help copy
   echo $help;
   //add some new copy
   echo "<h5>Custom Features</h5>";
   echo "<p>Content placed above the more divider will appear in column 1. Content placed below the divider will appear in column 2.</p>";
}

 

Potpiši podatke privatnim ključem

Potpiši podatke privatnim ključem:

<?php
/* Generiraj potpis iz privatnog ključa trgovca */
$fp = fopen( 'private-key.pem', 'r');
$priv_key = fread($fp, 8192);
fclose($fp);
$pkeyid = openssl_get_privatekey($priv_key);
// generiranje potpisa
openssl_sign($data, $signature, $pkeyid);
// brisanje kljuca iz memorije
openssl_free_key($pkeyid);
// kodiranje vrijednosti u BASE64
$b64sign = base64_encode($signature);
?>