Loading...

Follow Code And Me - WordPress Code Snippet Repository on Feedspot

Continue with Google
Continue with Facebook
or

Valid

Security wise, you do wish to restrict the access of WP Admin area to certain users only. While, you can achieve this by going onto your site’s cPanel area but thanks to snippet below, with adding it to your theme’s functions.php file of your theme, you will be able to achieve this.

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

Read Full Article
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

Using the following snippet will let you add sortable column in WordPress admin area.

add_filter( 'manage_posts_columns', 'namespace_AddXXXColumn' );
add_action( 'manage_posts_custom_column', 'namespace_AddXXXValue', 10, 2 );
function namespace_AddXXXColumn($cols) {
        $cols['xxx'] = __('XXX');
        return $cols;
}
function namespace_AddXXXValue($column_name, $post_id) {
    if ( 'xxx' == $column_name ) {
        echo get_permalink($post_id); // for example
    }
}
// Register the column as sortable
function xxx_column_register_sortable( $columns ) {
	$columns['xxx'] = 'xxx';
	return $columns;
}
add_filter( 'manage_edit-post_sortable_columns', 'xxx_column_register_sortable' );

Snippet Source/Credit: Snipplr

Read Full Article
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

Adding the following snippet to your theme’s functions.php file, you will be able to render your Twitter followers count. Also, remember to replace “planetabhi” with your own Twitter screen name.

function my_followers_count($screen_name = 'planetabhi'){
	$key = 'my_followers_count_' . $screen_name;
	// Let's see if we have a cached version
	$followers_count = get_transient($key);
	if ($followers_count !== false)
		return $followers_count;
	else
	{
		// If there's no cached version we ask Twitter
		$response = wp_remote_get("http://api.twitter.com/1/users/show.json?screen_name={$screen_name}");
		if (is_wp_error($response))
		{
			// In case Twitter is down we return the last successful count
			return get_option($key);
		}
		else
		{
			// If everything's okay, parse the body and json_decode it
			$json = json_decode(wp_remote_retrieve_body($response));
			$count = $json->followers_count;
			// Store the result in a transient, expires after 1 day
			// Also store it as the last successful using update_option
			set_transient($key, $count, 60*60*24);
			update_option($key, $count);
			return $count;
		}
	}
}
echo "I have " . my_followers_count('planetabhi') . " followers";

Snippet Source/Credit: Snipplr

Read Full Article
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

The following snippet will let you load content externally in WordPress.

require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php' );

Snippet Source/Credit: Snipplr

Read Full Article
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

We often are in the condition in which we are looking out for the ways by which we can prevent WordPress stylesheet from being cached. To help you out with the same, the following snippet below will let you achieve the same.

<link  href="<?php $theme_data = get_theme_data(TEMPLATEPATH . '/style.css'); bloginfo('stylesheet_url'); echo '?' . $theme_data['Version']; ?>" type="text/css" media="screen, projection" />

Snippet Source/Credit: WPRecipes.com

Read Full Article
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

It is pretty much common that you want to customize the label of certain things, say “post” label to “article” in your WordPress blog. It can be done with just few lines of code, just simply copy and paste the following snippet in your theme’s functions.php file and you are done.

add_filter('gettext',  'change_post_to_article');
add_filter('ngettext',  'change_post_to_article');
function change_post_to_article($translated) {
     $translated = str_ireplace('Post',  'Article',  $translated);
     return $translated;

Snippet Source/Credit: Smashing Magazine

Read Full Article
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

Using the whole timthumb script, which being saved as sized.php file in the theme directory, you will be able to resize image.

function get_thumb_src($src, $width="100", $height="100", $quality="85", $alignment="", $zoomcrop="", $filters="", $sharpen="", $canvascolor="", $transparency=""){
	if($src=="") return FALSE;
	$src 			= "src=".$src;
	$width			= "&w=".$width;
	$height			= "&h=".$height;
	$quality		= "&q=".$quality;
	$alignment		= (!empty($alignment)) ? "&a=".$alignment : "";
	$zoomcrop		= (!empty($zoomcrop)) ? "&zc=".$zoomcrop : "";
	$filters		= (!empty($filters)) ? "&f=".$filters : "";
	$sharpen		= (!empty($sharpen)) ? "&s=".$sharpen : "";
	$canvascolor	= (!empty($canvascolor)) ? "&cc=".$canvascolor : "";
	$transparency	= (!empty($transparency)) ? "&ct=".$transparency : "";
	return get_template_directory_uri()."/sized.php?".$src.$width.$height.$quality.$alignment.$zoomcrop.$filters.$sharpen.$canvascolor.$transparency;
}
function the_thumb_src($src, $width="100", $height="100", $quality="85", $alignment="", $zoomcrop="", $filters="", $sharpen="", $canvascolor="", $transparency=""){
	if($thumb = get_thumb_src($src, $width, $height, $quality, $alignment, $zoomcrop, $filters, $sharpen, $canvascolo, $transparency)){
		echo $thumb;
	}
}
function get_thumb($src, $alt="", $classes="", $id=""){
	return "<img .$classes."' id='".$id."' src='".$src."' alt='".$alt."' />";
}
function the_thumb($src, $alt="", $classes="", $id=""){
	echo get_thumb($src, $alt, $classes, $id);
}
function get_main_thumb($post_id, $alt="", $classes="", $id="", $width="", $height=""){
	$src = wp_get_attachment_image_src( get_post_thumbnail_id($post_id), "full", false);
	if($src[0]=="") return FALSE;
	$width = ($width!="") ? $width : "750";
	$height = ($height!="") ? $height : "250";
    $src = get_thumb_src($src[0], $width, $height);
	return "<img .$classes."' id='".$id."' src='".$src."' alt='".$alt."' />";
}
function the_main_thumb($post_id, $alt="", $classes="", $id="", $width="", $height=""){
	if($thumb = get_main_thumb($post_id, $alt, $classes, $id, $width, $height)){
		echo $thumb;
	}
}

Snippet Source/Credit: Binarymoon

Read Full Article
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

On simply adding the following snippet to your theme’s functions.php file, you will be able to remove WordPress standard gallery style.

add_filter( 'use_default_gallery_style', '__return_false' );

Read Full Article
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

The following snippet will let you show or hide widgets on specific WordPress pages.

add_filter( 'widget_display_callback', 'hide_widget_pages', 10, 3 );
function hide_widget_pages( $instance, $widget, $args ) {
  if ( $widget-&gt;id_base == 'pages' ) { // change 'pages' to widget name
     if ( !is_page( 'contact' ) ) {    // change page name
         return false;
     }
  }
}

Snippet Source/Credit: WPSnipp

Read Full Article
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

The following snippet will let you remove image sizes from media gallery in WordPress.

function wps_remove_image_size($sizes) {
                unset( $sizes['small'] );
                unset( $sizes['medium'] );
                unset( $sizes['large'] );
                return $sizes;
        }
        add_filter('image_size_names_choose', 'wps_remove_image_size');

Snippet Source/Credit: WPSnipp

Read Full Article

Read for later

Articles marked as Favorite are saved for later viewing.
close
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

Separate tags by commas
To access this feature, please upgrade your account.
Start your free month
Free Preview