Code snipits to help ‘CMS’ your WordPress sites

11.09.10 Posted in CMS, php, The Lab by 1 Comment

Add these to your functions.php inside your theme

Using shortcodes to show members-only content – source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
add_shortcode( 'visitor', 'visitor_check_shortcode' );
function visitor_check_shortcode( $atts, $content = null ) {
	 if ( ( !is_user_logged_in() && !is_null( $content ) ) || is_feed() )
		return $content;
	return '';
}

add_shortcode( 'member', 'member_check_shortcode' );
function member_check_shortcode( $atts, $content = null ) {
	 if ( is_user_logged_in() && !is_null( $content ) && !is_feed() )
		return $content;
	return '';
}

Search only posts – source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* Search only posts */
function SearchFilter($query) {
	if ($query->is_search) {
		$query->set('post_type', 'post');
	}
	return $query;
}
add_filter('pre_get_posts','SearchFilter');

Adjust jpeg quality – source

1
2
3
4
5
6
7
function gpp_jpeg_quality_callback($arg) {
	return (int)100; // change 100 to whatever you prefer, but don't go below 60

}
add_filter('jpeg_quality', 'gpp_jpeg_quality_callback');

Add an excerpt box for pages

1
2
3
4
5
6
7
8
9
10
11
12
13
/*Add an excerpt box for pages*/
if ( function_exists('add_post_type_support') ){
    add_action('init', 'add_page_excerpts');
    function add_page_excerpts(){
        add_post_type_support( 'page', 'excerpt' );
    }
}

Customise your admin footer

1
2
3
4
5
6
7
8
9
/*change admin footer*/
function remove_footer_admin () {
    echo 'Fueled by <a href="http://www.wordpress.org" target="_blank">WordPress</a> | Designed by <a href="http://thriveweb.com.au/" target="_blank">thrive</a>';
}
add_filter('admin_footer_text', 'remove_footer_admin');

Define your own login logo

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
/*Define your own login logo*/
function my_custom_login_logo() {
    echo 'h1 a { background-image:url('.get_bloginfo('template_directory').'/images/logo.png) !important;}';
}
add_action('login_head', 'my_custom_login_logo');

function change_wp_login_url() {
    echo bloginfo('url');
}
function change_wp_login_title() {
    echo get_option('blogname');
}
add_filter('login_headerurl', 'change_wp_login_url');
add_filter('login_headertitle', 'change_wp_login_title');

Hide the Themes menu item.

1
2
3
4
5
6
7
8
9
10
11
12
13
/*Remove theme menus*/
add_action('admin_init', 'remove_theme_menus');
function remove_theme_menus() {
    global $submenu;
    unset($submenu['themes.php'][5]);
    unset($submenu['themes.php'][15]);
}

Add a favicon to your admin area

1
2
3
4
5
6
7
8
9
// add a favicon for your admin

function admin_favicon() {
	echo '<link rel="Shortcut Icon" type="image/x-icon" href="'.get_bloginfo('stylesheet_directory').'/favicon.png" />';
}
add_action('admin_head', 'admin_favicon');

Remove unused menu items – source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function remove_menus () {
global $menu;
        $restricted = array(__('Dashboard'), __('Posts'), __('Media'), __('Links'), __('Pages'), __('Appearance'), __('Tools'), __('Users'), __('Settings'), __('Comments'), __('Plugins'));
        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_menus');

Remove admin tool bar – source

1
2
3
4
5
6
7
8
9
//Remove admin tool bar

function my_function_admin_bar(){
    return 0;
}
add_filter( 'show_admin_bar' , 'my_function_admin_bar');

Limit the max upload size – source

1
2
3
4
5
6
7
8
9
10
11
12
13
add_filter('wp_handle_upload_prefilter', function($file) {
    $size = $file['size'];
    if ($size > 500 * 1024) {
       $file['error'] = '"Files larger than X bytes are prevented from uploads.';
    }
    return $file;
});

One Response

  1. Thanks for the great read, Glad to see all of these culminated on one post. great reference, Will definitely utilize on my client sites.

Leave a Reply