WordPress Functions

Here are the most useful additions to functions.php where possible I have noted the author of the code.

Warning: Experts only! Back up your site (db + files) before attempting.

  • Custom user role:

    (This one is great for sites designed for clients. It gives clients the ability to change widgets and other appearance things, without the admin ability, so they won’t be able to edit code and otherwise mess with the site.)

    /* Custom user role - thx to xcellence-it.com http://bit.ly/1eiS5Vn/ */

    // get the the role object
    $role_object = get_role('editor');
    // add $cap capability to this role object

  • Jetpack content width:

    (If you use Jetpack it helps to have this function to tell it your content width)

    // tell jetpack the content width
    if ( ! isset( $content_width ) )
    $content_width = 980;

  • Loading some css just for the homepage:

    // Loading some css just on the homepage (find your home page's id number and change out 664 here):
    function enqueueHomeCSS(){
    if( is_page('664') ) {
    wp_enqueue_style( 'homestyle','/wp-content/themes/your_theme_name/css/homestyle.css');
    add_action('wp_enqueue_scripts', 'enqueueHomeCSS');

  • Turning off comments on images (to stop spammers):

    // thx to Gary Darling WordPress user - http://wordpress.org/support/topic/spam-on-pictures
    add_filter( 'comments_open', 'no_media_comments', 10, 2 );

    function no_media_comments( $open, $post_id ) {

    $post = get_post( $post_id );
    // wordpress refers to images as attachments
    if ( 'attachment' == $post->post_type )
    $open = false;

    return $open;

Leave a comment...

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s