WordPress Rewrite Rules Roles and Capabilities and WP Cron

Summary: Learning about the WordPress Rewrite Rules API, Roles and Capabilities of users and WP Cron to schedule events.

Rewrite API

The WordPress Rewrite API allows us to create custom URLs also known as permalinks for their content. This API is useful for creating SEO-friendly URLs, custom structures, or routing to specific functionality. The Rewrite API handles URL mapping and works in conjunction with the WordPress query to fetch and display the correct content.

  • Rewrite Rules: These are patterns that match URLs and route them to specific parts of our site, such as custom templates or endpoints.
  • Rewrite Tags: Custom placeholders for variables in URLs, like %postname% or %category%.
  • Query Vars: Variables that WordPress recognizes from URLs and passes to WP_Query for fetching content.
  • Flush Rewrite Rules: Necessary when adding or updating rewrite rules, to ensure WordPress recognizes new routing patterns.
function register_movie_post_type() {
    $args = array(
        'label'       => 'Movies',
        'public'      => true,
        'rewrite'     => array(
            'slug'       => 'movies',
            'with_front' => false,
        ),
        'supports'    => array('title', 'editor', 'thumbnail'),
    );
    register_post_type('movie', $args);
}
add_action('init', 'register_movie_post_type');

function custom_movie_rewrite_rules() {
    add_rewrite_rule(
        'movies/([0-9]{4})/([^/]+)/?$',
        'index.php?post_type=movie&year=$matches[1]&name=$matches[2]',
        'top'
    );
}
add_action('init', 'custom_movie_rewrite_rules');

function flush_movie_rewrite_rules() {
    flush_rewrite_rules();
}
register_activation_hook(__FILE__, 'flush_movie_rewrite_rules');
register_deactivation_hook(__FILE__, 'flush_rewrite_rules');

Roles and Capabilities

A Role defines a set of tasks a user assigned the role is allowed to perform. For instance, the Super Admin role encompasses every possible task that can be performed within a Network of virtual WordPress sites. The Administrator role limits the allowed tasks only to those which affect a single site. On the other hand, the Author role allows the execution of just a small subset of tasks.

  • Super Admin – somebody with access to the site network administration features and all other features. See the Create a Network article.
  • Administrator (slug: ‘administrator’) – somebody who has access to all the administration features within a single site.
  • Editor (slug: ‘editor’) – somebody who can publish and manage posts including the posts of other users.
  • Author  (slug: ‘author’)  – somebody who can publish and manage their own posts.
  • Contributor (slug: ‘contributor’) – somebody who can write and manage their own posts but cannot publish them.
  • Subscriber (slug: ‘subscriber’) – somebody who can only manage their profile.

Adding Roles

function simple_role() {
	add_role(
		'simple_role',
		'Simple Role',
		array(
			'read'         => true,
			'edit_posts'   => true,
			'upload_files' => true,
		),
	);
}

add_action( 'init', 'simple_role' );

Removing Roles

function simple_role_remove() {
	remove_role( 'simple_role' );
}

add_action( 'init', 'simple_role_remove' );

Adding Capabilities

function simple_role_caps() {
	$role = get_role( 'simple_role' );
	$role->add_cap( 'edit_others_posts', true );
}

add_action( 'init', 'simple_role_caps', 11 );

WP Cron

WP-Cron is how WordPress handles scheduling time-based tasks in WordPress. Several WordPress core features, such as checking for updates and publishing scheduled post, utilize WP-Cron. The “Cron” part of the name comes from the cron time-based task scheduling system that is available on UNIX systems.

Scheduling WP Cron Events

Adding the Hook

In order to get our task to run we must create our own custom hook and give that hook the name of a function to execute. This is a very important step. Forget it and our task will never run.

add_action( 'my_cron_hook', 'my_cron_exec' );

Scheduling the Task

An important note is that WP-Cron is a simple task scheduler. As we know, tasks are added by the hook created to call the function that runs the desired task. However if we call wp_schedule_event() multiple times, even with the same hook name, the event will be scheduled multiple times. WordPress provides a convenient function called wp_next_scheduled() to check if a particular hook is already scheduled. wp_next_scheduled() takes one parameter, the hook name.

if ( ! wp_next_scheduled( 'my_cron_hook' ) ) {
    wp_schedule_event( time(), 'five_seconds', 'my_cron_hook' );
}

Unscheduling tasks

register_deactivation_hook( __FILE__, 'my_deactivate' ); 

function my_deactivate() {
    $timestamp = wp_next_scheduled( 'my_cron_hook' );
    wp_unschedule_event( $timestamp, 'my_cron_hook' );
}
«
»

Leave a Reply

Your email address will not be published. Required fields are marked *