php5

How To Create a New Role in WordPress?

WordPress integrates six default roles: Administrator, Editor, Contributor, Author, subscriber and sixth multi-role: Super Admin which have all privileges under WordPress blog.

In this tutorial we will see how to create a new role for your website subscribers and how to display the content according to the new role.

Why would you need to create a new role in WordPress blog?
Apart from enhancing the WordPress security, we could give you a lot of reason to create a new role for subscribers. However in this tutorial we will give you one good example on how to keep the default subscriber role but also create a premium subscription to monetize your blog. So if you have a blog where you want to give access to certain articles or special offers only to people to have paid monthly subscription for example, they can have this custom-built new role in WordPress blog.

To create this new role (Premium Subscription) in WordPress you will need to add the following code in function.php file of your WordPress theme:

// Add a role
add_role(‘premium_user’, ‘Premium Subscription’, array(
‘read’ => true, // true : Allow reading of article page
‘edit_posts’ => false, // false : Forbidden to add articles or pages
‘delete_posts’ => false, // false : Forbidden to remove articles or pages
));

Add_role : premium_user identifier will be our new role and Premium Subscription is the name of the role that I attribute to my future subscribers who want this subscription.

How To Create a New Role in WordPress? Read More »

WP_Query

How To Create WordPress Custom Queries with WP_Query?

WordPress custom queries are very powerful tool. If you want to display on your index.php or single.php file other content different from what the WordPress shows by default, just create WordPress custom queries using WP_Query. For those that don’t know, WP_Query is a class that allows you to create WordPress custom queries and retrieve your posts according to criteria defined via the parameter to the constructor of this class.

To use this application you must store it in a variable and use that object in WordPress loop. In this tutorial I will show you some great examples on how to use WordPress custom queries or WP_Query in your own WordPress projects.

WordPress Custom Queries Examples
This class can be configured as desired, and there are many possibilities available to you, here are some of WordPress custom queries examples I find most useful:

Example 1: Select all posts

$query = new WP_Query(array(‘post_type’ => ‘post’));

Example 2: Select all posts and all pages

How To Create WordPress Custom Queries with WP_Query? Read More »