Handy Code Snippets / How To / No Plugins Necessary / PHP

Handy PHP Snippets for WordPress Sites

php-code-snippets

Here are some very handy PHP snippets I have collected over the years…

Use a shortcode inside PHP

1
echo do_shortcode('[your_shortcode]');

Only show content to users with a specific role

1
2
3
4
5
6
7
8
/* Only show content to users with a specific role */
global $user_login, $current_user;

if (is_user_logged_in()) {
get_currentuserinfo();
$user_info = get_userdata($current_user->ID);

if (in_array('administrator', $user_info->roles)) {

Send email to user when their role is changed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* Send email to user when their role is changed */
function user_role_update(
$user_id,
$new_role
) {
$site_url = get_bloginfo('wpurl');
$user_info = get_userdata( $user_id );

if (user_can( $user_id, 'new-role1' ) ) {

$to = $user_info->user_email;

$subject =
"Role changed: "
.$site_url."";

$message =
"Hello "
.$user_info->display_name .
" your role has changed on "
.$site_url.
", congratulations you are now an "
.$new_role;
wp_mail($to, $subject, $message);

} elseif (user_can( $user_id, 'new-role2' ) ) {

$to = $user_info->user_email;

$subject =
"Role changed: "
.$site_url."";

$message =
"Hello "
.$user_info->display_name .
" your role has changed on "
.$site_url.
", congratulations you are now an "
.$new_role;
wp_mail($to, $subject, $message);

} elseif (user_can( $user_id, 'new-role3' ) ) {

// etc...

}

}
add_action(
'set_user_role',
'user_role_update',
10,
2
);

Adding Custom Field’s Content to Your Theme’s Template Files

1
2
3
/* Add Custom Field's Content to Your Theme's Template Files */
global $wc_cpdf;
echo $wc_cpdf->get_value(get_the_ID(), '_customfield');

Replace “_customfield” with the id of the custom field you want to display.


Displaying User’s Name, Role, & ID

1
2
3
4
5
/* Displays User's Name, Role, and ID */
$user_info = get_userdata(1);
echo 'Username: ' . $user_info->user_login . "<br>";
echo 'User roles: ' . implode(', ', $user_info->roles) . "<br>";
echo 'User ID: ' . $user_info->ID . "<br>";

Add Login/Logout links to Menu

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* Add Login/Logout links to Menu */
add_filter(
'wp_nav_menu_items',
'add_login_logout_link',
10,
2
);
function add_login_logout_link($items, $args) {
ob_start();
wp_loginout('index.php');
$loginoutlink = ob_get_contents();
ob_end_clean();
$items .= '<li>'. $loginoutlink .'</li>';
return $items;
}

Shorten WordPress Generated Passwords

1
2
3
4
5
6
add_filter('random_password', 'modify_the_pass');

function modify_the_pass($pass) {
$pass = substr($pass, 0, 6); // make $pass six characters
return $pass; // return our new $pass
}

Only Show Content to Specific Role

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
global $user_login, $current_user;

if (is_user_logged_in()) {
get_currentuserinfo();
$user_info = get_userdata($current_user->ID);

if (in_array('role-goes-here', $user_info->roles)) {
?>

<p>Content goes here.</p>

<?php
}}
?>

 

Related Posts

Handy Code Snippets / How To / No Plugins Necessary / PHP / WooCommerce

Handy WooCommerce Store PHP Snippets

woocommerce-codesnippets

Here are some Handy WooCommerce Store PHP Snippets that you might find useful... Auto-Complete All WooCommerce Orders [cc lang="php"]...

Handy Code Snippets / How To / PHP / Plugins

List More Custom Field Names

ListMoreCustomFieldNames

First install & activate the List More Custom Field Names plugin. Then add the following code to your functions.php or your cust...

No comments

Leave a Reply