Building online school based on WP. I do understand how to go on with frameworks, but WP??? Analyzing articles and writing notes.

  • WordPress vs MVC Frameworks
  • Model of Site
  • Users, Roles, and Capabilities
  • Extending the WP_User Class

WordPress Versus MVC Frameworks Exist MVC plugins for WordPress: WP MVC, Churro, ... I would argue), but They say Models = plugins, Views = themes, Controllers = template loader. In WordPress, plugins are the proper place to store new data structures, complex business logic, and custom post type definitions; themes are the proper place to store templating code and logic; all page requests (unless they are accessing a cached .html file) are processed through the index.php file and processed by WordPress according to the Template Hierarchy. The template loader figures out which file in the template should be used to display the page to the end user. For example, use search.php to show search results, single.php to show a single post.

Model of Site

  • Schools can purchase a unique subdomain (specify a school name and slug admin)
  • The school admin then invites teachers. Teachers can invite students to the classes they create
  • Roles. Teacher gives access to create and edit classes, moderate discussion in their class forums, and create and manage assignments 4 classes through frontend forms created for this purpose. Students - the default Subscriber role in WordPress, only have access to view and participate in classes they are invited to by their teachers.
  • Classes Are BuddyPress Groups, forums are powered by the bbPress plugin, new forum is generated for each class
  • Assignments are a custom post type (CPT). Submissions are also CPTs linked to assignments by setting the submission’s post_parent field to the ID of the assignment it was submitted to. Students can post text content and also add one or more attachments to a submission.
  • Semesters Are a Taxonomy on the Class CPT. A custom taxonomy called “Department” is also set up for the group/class CPT.
  • Proposals Free Plugins: BadgeOS, Custom Post Type UI, Posts 2 Posts (allows you to create many-to-many relationships between posts, pages, and custom post types as well as users. For an example, you could use P2P to make connections between custom post types for schools, teachers, and subjects), Members, W3 Total Cache, WP All Import, BuddyPress (plus BuddyPress Toolbar, BuddyPress FollowMe, BuddyPress Media, BuddyPress Registration Options,BuddyMobile, BadgeOS Community Add-on, bbPress

The** functions.php** file of active theme (and parent theme if applicable) is loaded every time WordPress loads. For this reason, the functions.php file is a popular place to add little hacks and other random bits of code, the file can quickly become a mess. Developing a well-planned WordPress app, and function.php we break up the core functions of our main app plugin into smaller includes: functions.php, settings.php, sidebars.php

Users, Roles, and Capabilities The workhorse for managing WordPress users in code is the WP_User class.

// get the WP_User object WordPress creates for the currently logged-in user
global $current_user;
// get the currently logged-in user with the wp_get_current_user() function
$user = wp_get_current_user();
// set some variables
$user_id = 1;
$username = 'jason';
$email = '';
// get a user by ID
$user = wp_getuserdata( $user_id );
// get a user by another field
$user1 = wp_get_user_by( 'login', $username );
$user2 = wp_get_user_by( 'email', $email );
// use the WP_User constructor directly
$user = new WP_User( $user_id );
//use the WP_User constructor with a username
$user = new WP_User( $username );
// get the currently logged-in user
$user = wp_get_current_user();
// echo the user's display_name
echo $user->display_name;
// use user's email address to send an email
wp_mail( $user->user_email, 'Email Subject', 'Email Body' );
// get any user meta value
echo 'Department: ' . $user->department;
$full_name = trim( $user->first_name . ' ' . $user->last_name );
$full_name = trim( get_user_meta( $user->ID, 'first_name' ) . 
    ' ' . get_user_meta( $user->ID, 'last_name' ) );
function __get( $key ) {    
if ( isset( $this->data->$key ) ) {
        $value = $this->data->$key;
    } else {
        $value = get_user_meta( $this->ID, $key, true );
    return $value;
// dump all meta data for a user
$user_meta = get_user_meta( $user_id );
foreach( $user_meta as $key => $value )
    echo $key . ': ' . $value . '<br />';
// Split assignments into multiple lines when using magic methods.
$user->graduation_year = '2012';
$year = '2012';
//To test if a meta value is empty, set a local variable first.
$year = $user->graduation_year;
if ( empty( $year ) )
    $year = '2012';
//---------------Add, Update, and Delete Users
// insert user from values we've gathered
$user_id = wp_insert_user( array(
    'user_login' => $username,                          
    'user_pass' => $password,
    'user_email' => $email,
    'first_name' => $firstname,
    'last_name' => $lastname
// check if username or email has already been used
if ( is_wp_error( $user_id ) ){
    echo $return->get_error_message();
} else {
    // continue on with whatever you want to do with the new $user_id
// okay, log them in to WP                          
$creds = array();
$creds['user_login'] = $username;
$creds['user_password'] = $password;
$creds['remember'] = true;
$user = wp_signon( $creds, false ); 
// this will update a user's email and leave other values alone
$userdata = array( 'ID' => 1, 'user_email' => '' );
wp_update_user( $userdata );
// this function is also perfect for updating multiple user meta fields at once
wp_update_user( array(
    'ID' => 1,
    'company' => 'Stranger Studios',
    'title' => 'CEO',
    'personality' => 'crescent fresh'

Updating users is as easy as adding them with the wp_update_user() function. You pass in an array of user data and metadata. As long as there is an ID key in the array with a valid user ID as the value, WordPress will set any specified user values. You can also update one user meta value at a time using the up date_user_meta($user_id, $meta_key, $meta_value, $prev_value) function.

// arrays will get serialized
$children = array( 'Isaac', 'Marin');
update_user_meta( $user_id, 'children', $children );
// you can also store array by storing multiple values with the same key
update_user_meta( $user_id, 'children', 'Isaac' );
update_user_meta( $user_id, 'children', 'Marin' );
// when storing multiple values, specify the $prev_value parameter 
// to select which meta value is updated
update_user_meta( $user_id, 'children', 'Isaac Ford', 'Isaac' );
update_user_meta( $user_id, 'children', 'Marin Josephine', 'Marin' );
//delete all user meta by key
delete_user_meta( $user_id, 'children' );
//delete just one row when there are multiple values for one key
delete_user_meta( $user_id, 'children', 'Isaac Ford' );
// get the IDs of all users with children named Isaac
$parents_of_isaac = $wpdb->get_col( "SELECT user_id 
    FROM $wpdb->usermeta 
    WHERE meta_key = 'children' 
    AND meta_value = 'Isaac'" );
//this file contains wp_delete_user and is not always loaded, so let's make sure
require_once( ABSPATH . '/wp-admin/includes/user.php' );
//delete the user
wp_delete_user( $user_id );
//or delete a user and reassign their posts to user with ID #1
wp_delete_user( $user_id, 1 );

Hooks and Filters

//create a new "course" CPT when a teacher registers
function sp_user_register( $user_id ){
    // check if the new user is a teacher (see Chapter 15 for details)
    if ( pmpro_hasMembershipLevel( 'teacher', $user_id ) ) {
        // add a new "course" CPT with this user as author
        wp_insert_post( array(
            'post_title' => 'My First Course',
            'post_content' => 'This is a sample course...',
            'post_author' => $user_id,
            'post_status' => 'draft',
            'post_type' => 'course'
        ) );
add_action( 'user_register', 'sp_user_register' );
// send an email when a user is being deleted
function sp_delete_user( $user_id ){
    $user = get_userdata( $user_id );
    wp_mail( $user->user_email, 
        "You've been deleted.", 
        'Your account at SchoolPress has been deleted.'
// want to be able to get user_email so hook in early
add_action( 'delete_user', 'sp_delete_user' );

Checking a User’s Role and Capabilities

if ( current_user_can( 'manage_options' ) ) { 
    // has the manage options capability, typically an admin
if ( current_user_can( 'edit_user', $user_id ) ) {
    // can edit the user with ID = $user_id. 
    // typically either the user himself or an admin 
if ( current_user_can( 'edit_post', $post_id ) ) {
    // can edit the post with ID = $post_id. 
    // typically the author of the post or an admin or editor
if ( current_user_can( 'subscriber' ) ) {
    // one way to check if the current user is a subscriber
    Output comments for the current post,
    highlighting anyone who has capabilities to edit it.
global $post;   // current post we are looking at
$comments = get_comments( 'post_id=' . $post->ID );
foreach( $comments as $comment ){
    // default CSS classes for all comments
    $classes = 'comment';
    // add can-edit CSS class to authors
    if ( user_can( $comment->user_id, 'edit_post', $post->ID ) )
        $classes .= ' can-edit';
<div id="comment-<?php echo $comment->comment_ID;?>" 
    class="<?php echo $classes;?>">
    Comment by <?php echo $comment->comment_author;?>:
    <?php echo wpautop( $comment->comment_content );?>

Extending the WP_User Class

class Student extends WP_User {
    // no constructor so WP_User's constructor is used
    // method to get assignments for this Student
    function getAssignments() {
        // get assignments via get_posts if we haven't yet
        if ( ! isset( $this->data->assignments ) )
            $this->data->assignments = get_posts( array(
                'post_type' => 'assignment',    // assignments
                'numberposts' => -1,            // all posts
                'author' => $this->ID           // user ID for this Student
        return $this->data->assignments;
    // magic method to detect $student->assignments
    function __get( $key ) {
        if ( $key == 'assignments' )
            return $this->getAssignments();
            // fallback to default WP_User magic method
            return parent::__get( $key );

The getAssignments() method uses the get_posts() function to get all posts of type assignment that are authored by the user associated with this Student store the array of assignment posts in the $data property, which is defined in the WP_User class and stores all of the base user data and metadata. This allows us to use code like $student->assignments to get the assignments ,normally if $student->assignments is a defined property of $student, the value of that property will be returned. But if there is no property, PHP will send assignments as the $key parameter to __get method. Here we check that $key == "assignments" and then return the value of the getAssignments() method defined later. If $key is something other than assignments pass it to the __get() method of the parent WP_User class, which checks for the value in the $data property of the class instance or failing that sends the key to the get_user_meta() function.


Monday, September 29, 2014 2:30 PM
acquired said a 6% go up impressive september 22. basically second raise we reported. high quality one we feel am at whatever we contact a neo real estate market rotate where exactly we tried to increase ticket prices 4%. I absolutely enjoyed reading this flick, considering that the Yamada family and friends communications you should obtained a chuckle apart from anyone. usually the hubby coming back home right from the length of his job to order a little strawberry for lunch. the mother without having plenty of time to fix long before a traveler comes down all over in order that your girlfriend types every little thing a good deal more messy and / or getting it to be planting season maintenance time.
asd says:
Wednesday, September 10, 2014 5:06 AM

Leave a Comment

Fields with * are required.

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