Loading...

Follow Code And Me - WordPress Code Snippet Repository on Feedspot

Continue with Google
Continue with Facebook
Or

Valid


The following snippet will let you disable sidebars and widgets on page template in WordPress.

<?php
/* Disables widget areas. */
add_filter( 'sidebars_widgets', 'remove_sidebars' );
function remove_sidebars( $sidebars_widgets ) {
	global $wp_query;
	if ( is_singular() ) {
		$template = get_post_meta( $wp_query->post->ID, "_wp_{$wp_query->post->post_type}_template", true );
		// Disables all widgets
		if ( preg_match("/(-no-widgets.php)$/i", $template ) )
			$sidebars_widgets = array( true );
		// Disables 'primary' & 'secondary' sidebars widgets
		if ( preg_match("/(-no-sidebars.php)$/i", $template ) )
			$sidebars_widgets['primary'] = false;
			$sidebars_widgets['secondary'] = false;
	}
	return $sidebars_widgets;
}
?>
Read Full Article
Visit website
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

Adding the following snippet to your theme’s functions.php file, you will be able to set a maximum word count on post titles.

function maxWord($title){
    global $post;
    $title = $post->post_title;
    if (str_word_count($title) >= 10 ) //set this to the maximum number of words
        wp_die( __('Error: your post title is over the maximum word count.') );
}
add_action('publish_post', 'maxWord');

Snippet Source/Credit: Pippin

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

The following snippet display the WordPress excerpt dynamic length.

function ms_rp_excerpt($limit) {
		$excerpt = explode(' ', get_the_excerpt(), $limit);
		if (count($excerpt) >= $limit) {
			array_pop($excerpt);
			$excerpt = implode(' ', $excerpt).'...';
		}
		else {
			$excerpt = implode(' ', $excerpt);
		}
		$excerpt = preg_replace('`\[[^\]]*\]`', '', $excerpt);
		return $excerpt;
	}

Snippet Source/Credit: Snipplr

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

Using the following snippet in your theme’s .htaccess code will let you fix HTTP error in WordPress.

<IfModule mod_security.c>
<Files async-upload.php>
SecFilterEngine Off
SecFilterScanPOST Off
</Files>
</IfModule>
Read Full Article
Visit website
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

Simply paste the snippet below into your functions.php file.

add_action('load-page-new.php','add_custom_help_page');
add_action('load-page.php','add_custom_help_page');
function add_custom_help_page() {
   //the contextual help filter
   add_filter('contextual_help','custom_page_help');
}
function custom_page_help($help) {
   //keep the existing help copy
   echo $help;
   //add some new copy
   echo "<h5>Custom Features</h5>";
   echo "<p>Content placed above the more divider will appear in column 1. Content placed below the divider will appear in column 2.</p>";
}

Snippet Source/Credit: WP Tuts

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

The following snippet will let you register a sidebar in function.php.

/* register sidebars */
if ( function_exists('register_sidebar') ){
	register_sidebar(array('name' => 'Sidebar',
            'before_widget' => '<li class=wpSidebar >',
            'after_widget' => '</li>',
            'before_title' => '<h3>',
            'after_title' => '</h3>',
        ));
	register_sidebar(array('name' => 'Footer',
            'before_widget' => '<li class=wpFooterSidebar >',
            'after_widget' => '</li>',
            'before_title' => '<h3>',
            'after_title' => '</h3>',
        ));
 }

Snippet Source/Credit: Snipplr

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

The following snippet below will let you remove WordPress admin menus.

function remove_menu_items() {
	global $menu;
	$restricted = array(
		__('Links'),
		__('Comments'),
		__('Media'),
		__('Plugins'),
		__('Tools'),
		__('Users')
	);
	end ($menu);
	while (prev($menu)) {
		$value = explode(' ',$menu[key($menu)][0]);
		if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){
			unset($menu[key($menu)]);
		}
	}
}
add_action('admin_menu', 'remove_menu_items');

Snippet Source/Credit: Snipplr

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

Using the following snippet, you will be able to list all available WordPress roles.

$roles_obj = new WP_Roles();
$roles_names_array = $roles_obj->get_names();
echo '<select name="role">';
foreach ($roles_names_array as $role_name) {
        echo '<option>'.$role_name.'</option>';
}
echo '</select>';

Snippet Source/Credit: WPSnipp

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

The following snippet will let you change login logo, login URL and login title.

// Change login logo
function custom_login_logo() {
  echo '<style type="text/css">h1 a { background: url('.get_bloginfo('template_directory').'http://cdn.css-tricks.com/images/logo-login.gif) 50% 50% no-repeat !important; }</style>';
}
add_action('login_head', 'custom_login_logo');
//  Change logo URL
function change_wp_login_url() {
  return bloginfo('url');
}
add_filter('login_headerurl', 'change_wp_login_url');
// Change login title
function change_wp_login_title() {
  return get_option('blogname');
}
add_filter('login_headertitle', 'change_wp_login_title');

Snippet Source/Credit: CSS-Tricks

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

The following snippet will let you delete both width and height attributes from image tag.

<?php
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'thumbnail' );
if ($image) : ?>
    <img src="<?php echo $image[0]; ?>" alt="" />
<?php endif; ?>

Snippet Source/Credit: Stackexchange

Read Full Article
Visit website

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 year
Free Preview