• Checking if a Table Already Exists
  • Plugin that displays the avatar by role
  • Plugin with User Metadata
  • Roles
  • Plugin limits the editing of admin posts
  • Plugin Forum Roles
//Checking if a Table Already Exists
$tablename = $wpdb- > prefix . “hits”;
if( $wpdb- > get_var(SHOW TABLES LIKE$tablename’”) != $tablename ) {
// table does not exist!
}

plugin that displays the avatar by role

<?php
/*
Plugin Name: User Avatars
Plugin URI: http://example.com
Description: Displays user avatars based on role.
*/
 
function boj_user_avatars( $role = 'subscriber' ) {
 
    /* Get the users based on role. */
    $users = get_users(
        array(
            'role' => $role
        )
    );
 
    /* Check if any users were returned. */
    if ( is_array( $users ) ) {
 
        /* Loop through each user. */
        foreach ( $users as $user ) {
 
            /* Display ther user's avatar. */
            echo get_avatar( $user );
        }
    }
}
 
?>

The user_contactmethods fi lter hook returns an array of meta keys and labels for these label keys. To add new meta keys, you need to add new values to the array, as shown

/* Add a filter to the hook. */
add_filter( 'user_contactmethods', 'ssd_user_contactmethods' );
 
/* Function for adding new contact methods. */
function ssd_user_contactmethods( $user_contactmethods ) {
 
    /* Add the Twitter contact method. */
    $user_contactmethods['twitter'] = 'Twitter Username';
 
    /* Add the phone number contact method. */
    $user_contactmethods['phone'] = 'Phone Number';
 
    /* Return the array with the new values added. */
    return $user_contactmethods;
}

Plugin with User Metadata

<?php
/*
Plugin Name: User Favorite Post
Plugin URI: http://example.com
Description: Allows users to select their favorite post from the site.
Version: 0.1
 
*/
 
/* Add the post form to the user/profile edit page in the admin. */
add_action( 'show_user_profile', 'ssd_user_favorite_post_form' );
add_action( 'edit_user_profile', 'ssd_user_favorite_post_form' );
 
/* Function for displaying an extra form on the user edit page. */
function ssd_user_favorite_post_form( $user ) {
 
    /* Get the current user's favorite post. */
    $favorite_post = get_user_meta( $user->ID, 'favorite_post', true );
 
    /* Get a list of all the posts. */
    $posts = get_posts( array( 'numberposts' => -1 ) );
    ?>
 
    <h3>Favorites</h3>
 
    <table class="form-table">
 
        <tr>
            <th><label for="favorite_post">Favorite Post</label></th>
 
            <td>
                <select name="favorite_post" id="favorite_post">
                    <option value=""></option>
 
                <?php foreach ( $posts as $post ) { ?>
                    <option value="<?php echo esc_attr( $post->ID ); ?>" 
                    <?php selected( $favorite_post, $post->ID ); ?>>
                        <?php echo esc_html( $post->post_title ); ?>
                    </option>
                <?php } ?>
 
                </select>
                <br />
                <span class="description">Select your favorite post.</span>
            </td>
        </tr>
 
    </table>
<?php }
 
/* Add the update function to the user update hooks. */
add_action( 'personal_options_update', 'ssd_user_favorite_post_update' );
add_action( 'edit_user_profile_update', 'ssd_user_favorite_post_update' );
 
/* Function for updating the user's favorite post. */
function ssd_user_favorite_post_update( $user_id ) {
 
    /* Check if the current user has permission to edit the user. */
    if ( !current_user_can( 'edit_user', $user_id ) )
        return false;
 
    /* Only accept numbers 0-9 since it's a post ID. */
    $favorite_post = preg_replace( "/[^0-9]/", '', $_POST['favorite_post'] );
 
    /* Update the user's favorite post. */
    update_user_meta( $user_id, 'favorite_post', $favorite_post );
}
 
?>

Therefore, as a plugin developer, you can never know exactly what roles exist or might exist for a site unless you have direct access to the install, such as when doing client work. Keep this in mind when developing your plugins. A common mistake many plugin authors make is to check a user ’ s role before executing code. There is rarely a good reason to do this. Your plugin should check for a capability because capabilities determine a user ’ s permission to do something on the site. More precisely, they grant users a set of permissions called capabilities. In general, most plugins won ’ t need to know what roles users have. Most plugins work directly with capabilities because they are what defi ne whether a user has permission to perform a task within the site.

Plugin limits the editing of admin posts

<?php
/*
Plugin Name: Restrict Admin Post Editing
Plugin URI: http://example.com
Description: Only admins can edit posts made by admins.
Version: 0.1
 
*/
 
/* Filter the 'map_meta_cap' hook. */
add_filter( 'map_meta_cap', 'ssd_restrict_admin_post_editing', 10, 4 );
 
/* Function for restricting users from editing admin posts. */
function ssd_restrict_admin_post_editing( $caps, $cap, $user_id, $args ) {
 
    /* If user is trying to edit or delete a post. */
    if ( 'edit_post' == $cap || 'delete_post' == $cap ) {
 
        /* Get the post object. */
        $post = get_post( $args[0] );
 
        /* If an admin is the post author. */
        if ( author_can( $post, 'delete_users' ) ) {
 
            /* Add a capability that only admins might have to the caps array. */
            $caps[] = 'delete_users';
        }
    }
 
    /* Return the array of capabilities. */
    return $caps;
}
 
?>

Allowing Custom Permissions

<?php
/*
Plugin Name: Private Content
Plugin URI: http://example.com
Description: Shortcode for hiding private content.
Version: 0.1
 
*/
 
/* Register shortcodes in 'init'. */
add_action( 'init', 'ssd_private_content_register_shortcodes' );
 
/* Function for registering the shortcode. */
function ssd_private_content_register_shortcodes() {
 
    /* Adds the [ssd-private] shortcode. */
    add_shortcode( 'ssd-private', 'ssd_private_content_shortcode' );
}
 
/* Function for handling shortcode output. */
function ssd_private_content_shortcode( $attr, $content = null ) {
 
    /* If there is no content, return. */
    if ( is_null( $content ) )
        return $content;
 
    /* Check if the current user has the 'read_private_content' capability. */
    if ( current_user_can( 'read_private_content' ) ) {
 
        /* Return the private content. */
        return $content;
    }
 
    /* If the user doesn't have the 'read_private_content' capability. */
    else {
 
        /* Return an alternate message. */
        return '<p>You do not have permission to read this content.</p>';
    }
 
    /* Return an empty string as a fallback. */
    return '';
}
 
?>

Plugin Forum Roles

<?php
/*
Plugin Name: Forum Roles
Plugin URI: http://example.com
Description: Creates custom roles and capabilities for a fictional forum plugin.
Version: 0.1
 
*/
 
/* Custom forum roles and capabilities class. */
class ssd_Forum_Roles {
 
    /* PHP4 Constructor method. */
    function ssd_Forum_Roles() {
 
        /* Register plugin activation hook. */
        register_activation_hook( __FILE__, array( &$this, 'activation' ) );
 
        /* Register plugin deactivation hook. */
        register_deactivation_hook( __FILE__, array( &$this, 'deactivation' ) );
    }
 
    /* Plugin activation method. */
    function activation() {
 
        /* Get the default administrator role. */
        $role =& get_role( 'administrator' );
 
        /* Add forum capabilities to the administrator role. */
        if ( !empty( $role ) ) {
            $role->add_cap( 'publish_forum_topics' );
            $role->add_cap( 'edit_others_forum_topics' );
            $role->add_cap( 'delete_forum_topics' );
            $role->add_cap( 'read_forum_topics' );
        }
 
        /* Create the forum administrator role. */
        add_role(
            'forum_administrator',
            'Forum Administrator',
            array(
                'publish_forum_topics',
                'edit_others_forum_topics',
                'delete_forum_topics',
                'read_forum_topics'
            )
        );
 
        /* Create the forum moderator role. */
        add_role(
            'forum_moderator',
            'Forum Moderator',
            array(
                'publish_forum_topics',
                'edit_others_forum_topics',
                'read_forum_topics'
            )
        );
 
        /* Create the forum member role. */
        add_role(
            'forum_member',
            'Forum Member',
            array(
                'publish_forum_topics',
                'read_forum_topics'
            )
        );
 
        /* Create the forum suspended role. */
        add_role(
            'forum_suspended',
            'Forum Suspended',
            array( 'read_forum_topics' )
        );
    }
 
    /* Plugin deactivation method. */
    function deactivation() {
 
        /* Get the default administrator role. */
        $role =& get_role( 'administrator' );
 
        /* Remove forum capabilities to the administrator role. */
        if ( !empty( $role ) ) {
            $role->remove_cap( 'publish_forum_topics' );
            $role->remove_cap( 'edit_others_forum_topics' );
            $role->remove_cap( 'delete_forum_topics' );
            $role->remove_cap( 'read_forum_topics' );
        }
 
        /* Set up an array of roles to delete. */
        $roles_to_delete = array(
            'forum_administrator',
            'forum_moderator',
            'forum_member',
            'forum_suspended'
        );
 
        /* Loop through each role, deleting the role if necessary. */
        foreach ( $roles_to_delete as $role ) {
 
            /* Get the users of the role. */
            $users = get_users( array( 'role' => $role ) );
 
            /* Check if there are no users for the role. */
            if ( count( $users ) <= 0 ) {
 
                /* Remove the role from the site. */
                remove_role( $role );
            }
        }
    }
}
 
$forum_roles = new ssd_Forum_Roles();
 
?>

Need to remember how the relationship of users, roles, and capabilities works. Capabilities control permissions. Roles are given capabilities. Users are assigned roles, and each role ’ s capabilities are extended to its users. Keeping this in mind when developing your plugins can make the development process much smoother.

Leave a Comment

Fields with * are required.

Please enter the letters as they are shown in the image above.
Letters are not case-sensitive.