How To Restrict WP Admin Access To Certain Users
Thanks to snippet below, with adding it to your theme’s functions.php file of your theme, you will be able to restrict WP admin access to certain users.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
function restrict_access_admin_panel(){ global $current_user; get_currentuserinfo(); if ( // Look for the presence of /wp-admin/ in the url stripos($_SERVER['REQUEST_URI'],'/wp-admin/') !== false && // Allow calls to async-upload.php stripos($_SERVER['REQUEST_URI'],'async-upload.php') == false && // Allow calls to admin-ajax.php stripos($_SERVER['REQUEST_URI'],'admin-ajax.php') == false ) { // Does the current user fail the required capability level? if (!current_user_can('activate_plugins')) { wp_redirect( get_bloginfo('url') ); exit; } } } add_action('admin_init', 'restrict_access_admin_panel', 1); |
Snippet Source/Credit: WordPress Codex
How To Store Dynamic Sidebar In A Variable
The use of the following snippet will let you store dynamic sidebar in a variable.
1 2 3 4 5 6 7 |
ob_start(); dynamic_sidebar('sidebar-id'); $sidebar = ob_get_contents(); ob_end_clean(); |
Snippet Source/Credit: Stack Exchange
How To Use Gravatars In Your Script
The use of Gravatars in recent times is on full rise and result of which more and more people want to use it in their script. This is where, the snippet below will help you achieve this and let you add Gravatars in your script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php function show_my_gravatar($email, $size, $default, $rating) { $params = '?gravatar_id='.md5($email).'&default='.$default.'&size='.$size.'&rating='.$rating; $output = '<img src="http://www.gravatar.com/avatar.php'.$params.'" width="'.$size.'px" height="'.$size.'px" />'; echo $output; } ?> |
Snippet Source/Credit: AppHP
How To Add Custom Styles To Visual Editor In WordPress
Using the following snippet will let you add custom styles to visual editor in WordPress.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// Add custom CTA styles to TinyMCE editor if ( ! function_exists('tdav_css') ) { function tdav_css($wp) { $wp .= ',' . get_bloginfo('stylesheet_directory') . '/css/tinymce.css'; return $wp; } } add_filter( 'mce_css', 'tdav_css' ); |
Snippet Source/Credit: WordPress.org
How To Display Custom Posts With Pagination
By default, in WordPress, a page name and custom post type can’t have the same name. Well, using the snippet below, you will be able to display custom posts with pagination.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
<h1><?php the_title(); ?></h1> <?php the_content(); ?><br /><br /> <?php $i = 0; ?> <?php $loop = new WP_Query( array( 'post_type' => 'testimonial', 'orderby'=> date, 'paged' => get_query_var( 'paged' ), 'posts_per_page' => 5 ) ); ?> <?php if ( function_exists( 'wp_pagenavi' ) ) { wp_pagenavi( array( 'query' => $loop ) ); wp_reset_postdata(); } ?> <?php while ( $loop->have_posts() ) : $loop->the_post(); ?> <div class="testimonial-block"> <?php $authorpic = get('testimonial_info_image'); $authortitle = get('testimonial_info_title_company'); ?> <?php if ($authorpic != NULL) { ?><img src="<?php echo $authorpic; ?>" alt="person" class="testimonial-pic" /><?php } ?> <?php the_content(); ?> <strong><?php the_title(); ?><br /> <?php if ($authortitle != NULL) { ?><br /><?php echo $authortitle; ?><?php } ?></strong> <small><?php echo date("m\/d\/y"); ?></small> </div> <?php $i++; endwhile; ?> <div class="clear"><br /><br /></div> <?php if ( function_exists( 'wp_pagenavi' ) ) { wp_pagenavi( array( 'query' => $loop ) ); wp_reset_postdata(); } ?> |
Snippet Source/Credit: WordPress.org
How To Add Custom Images Sizes To WordPress
The following snippet below will let you add custom images sizes to WordPress.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// Add Feature Image size, hard crop if ( function_exists( 'add_image_size' ) ) { add_image_size( 'feature-image', 570, 285, true ); add_image_size( 'feature-image-small', 285, 285, true); add_image_size( 'feature-image-home', 285, 303, true); } // To use the image, if feature image exist <?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'feature-image' ); } ?> // Add a class to the image <?php echo the_post_thumbnail( 'feature-image-large', array('class' => 'feature-image') ); ?> |
Snippet Source/Credit: Snipplr