This code is used to restrict access to specific content in WordPress to logged-in users who have the role of administrator or member.
You might use this code snippet when you want to restrict access to certain content or resources on your website to specific user roles, such as members or administrators.
This could be useful for creating membership-based websites where certain content or resources are only available to certain user roles. This code snippet could replace a similar feature in a popular membership plugin.
Check if user is logged in by using the function is_user_logged_in().
Check if the user has the role of administrator or member by using the function current_user_can().
If both of these conditions are true, execute the shortcode for the data table by using the function do_shortcode().
If the user is not logged in or does not have the correct role, display a message that the content is restricted to site members and provide a link to log in or register.
if ( is_user_logged_in() && (current_user_can('administrator') || current_user_can('member')) ) {
//Add other code here
} else {
echo'<div class="restricted-content">';
echo'<p>This content is restricted to site members. If you are an existing user, please log in. New users may register below.</p>';
echo'</div>';
}
By adding this code to your WordPress theme's functions.php file or creating a custom plugin, you can easily restrict access to certain content or resources on your website to specific user roles.
Restricting Access to the WordPress Dashboard for Non-Permitted Users
This code is used to prevent users who do not have certain permissions, from accessing the WordPress dashboard. It checks if the current user doesn't have the "manage_options" permission, and if they are not currently on the admin-ajax.php page.
You might use this code snippet when you want to restrict access to the WordPress dashboard for specific user roles. This could be useful for creating membership-based websites where certain content or resources are only available to certain user roles. This code snippet could replace a similar feature in a popular plugin such as Adminimize or Restrict User Access.
Check if the current user does not have the "manage_options" capability by using the function current_user_can().
Check if the current PHP file being accessed is not the admin-ajax.php file by comparing the value of $_SERVER['PHP_SELF'] to the string /wp-admin/admin-ajax.php.
If both of these conditions are true, redirect the user to the site's home page by using the function wp_redirect() and exit the script by using the exit; statement.
Creating a Custom Affiliate Search Page in the WordPress Dashboard
This code is used to create a custom affiliate search page in the WordPress dashboard using the WP DataTable plugin.
You might use this code snippet when you want to create a custom affiliate search page in the WordPress dashboard for your website. This could be useful for searching and managing affiliates on your website.
Create a function called wp_affiliate_search_page
Use the add_menu_page() function to create a new menu page in the WordPress dashboard. The function takes in several parameters, including the title of the page, the name that will appear in the menu, the capability required to access the page, the slug or URL of the page, the function that will be called to display the content, the icon that will be used, and the position of the menu item.
The callback function wp_affiliate_search_page_content will echo out the shortcode of the wpdatatable id
Use the add_action() function to execute the wp_affiliate_search_page function when the admin_menu action is called.
Customizing Admin Bar Access for Non-Administrators
This code snippet uses the !current_user_can() function to check if the current user has the capability of being an administrator. If the user is not an administrator, the admin bar is hidden using the show_admin_bar(false) function.
This can be useful if you want to restrict access to the admin bar for non-admin users on your website. This can help to keep your website's backend clean and organized, as well as prevent users from accidentally making changes they shouldn't be making.
if (!current_user_can('administrator')) {
show_admin_bar(false);
}
It's important to note that this snippet will only hide the admin bar on the frontend of the website. Users will still be able to access the backend of the website if they know the URL.
This snippet is an alternative for controlling access to the admin bar using the plugin like "Adminimize" and "Remove Admin Bar" etc.
Set Permalink to Postname
This snippet is used to set the permalink structure of your website to postname.
Why you would use this snippet:
The permalink structure is the format of the URLs on your website that are used to link to your posts and pages. By default, WordPress uses the date and title of the post as the permalink structure.
By using this snippet, you can change the permalink structure to postname which makes the URLs of your website more user-friendly and SEO-friendly.
The snippet uses the add_action function to execute the code when the init action is fired.
Inside the function wpse_125800_init, the global $wp_rewrite variable is used to set the permalink structure to /%postname%/ using the set_permalink_structure method.
Want to print your doc? This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (