WordPress disable dashboard for non-admin

By adding this function to your functions.php, non-admin users can’t approach the WordPress admin / dashboard.

// No WordPress admin for non-administrators
add_action('admin_init', 'no_mo_dashboard');
function no_mo_dashboard() {
 if (!current_user_can('manage_options') && $_SERVER['DOING_AJAX'] != '/wp-admin/admin-ajax.php') {
 wp_redirect(home_url()); exit;
 }
}

I have used this in a project where I created a complete dashboard in the frontend of the website, so where no non-admin user was allowed to visit the WordPress admin / dashboard.

 

WordPress show admin bar only for Admin

In some situations it is helpful to don’t show the WordPress admin bar to all the user types. By adding the following code to your function.php, the admin bar only shows up when your logged in as Administrator.

// show admin bar only for admins
if (!current_user_can('manage_options')) {
 add_filter('show_admin_bar', '__return_false');
}

Related posts by author

For displaying related posts by author somewhere in your WordPress website you can use the following function:

function get_related_author_posts() {
 global $authordata, $post;

 $authors_posts = get_posts( array( 'author' => $authordata->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 5 ) );

 if( $authors_posts && count( $authors_posts ) >= 1 ) :
 $output = '<ul>';
 foreach ( $authors_posts as $authors_post ) {
 $output .= '<li><a href="' . get_permalink( $authors_post->ID ) . '">' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '</a>' . apply_filters( 'the_excerpt', $authors_post->post_excerpt, $authors_post->ID ) . '</li>';
 }
 $output .= '</ul>';
 else :
 $output = '<div class="error"><p>'. __( 'Nog geen artikelen geschreven...' ) .'</p></div>';
 endif;
 return $output;

 } 

 /*add this hook where you need the related posts*/
 echo get_related_author_posts();
 ?>

Tip! If you want to use posts from a custom post type, you need to specify this in get_posts() by adding: ‘post_type’ => ‘post-type-name’.

Difference between wp_list_categories and get_categories

The WordPress functions get_categories() and wp_list_categories() are almost the same. They are using almost the same parameters but there is a difference between them:

  • The get_categories() function returns an array of category objects matching the query parameters.
  • The wp_list_categories() displays a list of categories as links.

WordPress developer documention:

get_categories(): http://codex.wordpress.org/Function_Reference/get_categories

wp_list_categories(): http://codex.wordpress.org/Function_Reference/wp_list_categories

Post_is_in_descendant_category

You can use the post_is_in_descendant_category() to look if a post is in a child category of the category you have specified in the function.

So you can use it for example in your <title> in an if statement to change your <title> if the post is in a parent category that you had specified in the function. Example:

<?php if(post_is_in_descendant_category('1') ) : ?>
// Do something
<?php endif; ?>

In this example (‘1’) is a parent category.

This function is not standard in WordPress so you need to place the following code in your functions.php.

<?php
/**
 * Tests if any of a post's assigned categories are descendants of target 
categories
 *
 * @param int|array $cats The target categories. Integer ID or array of integer 
IDs
 * @param int|object $_post The post. Omit to test the current post in the Loop or main query
 * @return bool True if at least 1 of the post's categories is a descendant of any of the target categories
 * @see get_term_by() You can get a category by name or slug, then pass ID to this function
 * @uses get_term_children() Passes $cats
 * @uses in_category() Passes $_post (can be empty)
 * @version 2.7
 * @link http://codex.wordpress.org/Function_Reference/in_category#Testing_if_a_post_is_in_a_descendant_category
 */
if ( ! function_exists( 'post_is_in_descendant_category' ) ) {
 function post_is_in_descendant_category( $cats, $_post = null ) {
 foreach ( (array) $cats as $cat ) {
 // get_term_children() accepts integer ID only
 $descendants = get_term_children( (int) $cat, 'category' );
 if ( $descendants && in_category( $descendants, $_post ) )
 return true;
 }
 return false;
 }
}
?>

Create widget area header and call in template

To show a widget in your WordPress template you need to do 2 things.

  • Create an area in your WordPress dashboard where you can put your widgets in.
  • Place code where you want to show your widget in your WordPress template.

Create widget area in WordPress dashboard

To create an area you need to place the following code in your functions.php file.

<?php
if ( function_exists('register_sidebar') ){
 register_sidebar(array(
 'name' => 'Widget name',
 'before_widget' => '<div>',
 'after_widget' => '</div>',
 'before_title' => '',
 'after_title' => '',
));}
?>

And for each widget you place this code in functions.php and change the information.

Place code where to show widget in WordPress template

Now you need to place some code where to tell WordPress where to show the widget.

<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Widget name') ) : ?><?php endif; ?>

you take the same name as a previously completed in your functions.php.