3
\$\begingroup\$

I am making a WordPress "framework" for myself. Looking at the functions.php file file, is there redundant or bad code that could be changed or some code that could be good to add?

The functions.php should be a file that I could use in every web project with little or no change.

Is this code good to put in the functions.php file?

Code (block scrollable):

/*
 * Default theme setup
 */
function beeFramework_setup() {

    // This theme styles the visual editor to resemble the theme style,
    // specifically font, colors, icons, and column width.
    add_editor_style( array( 'css/editor-style.css', 'fonts/genericons.css' ) );

    // Adds RSS feed links to <head> for posts and comments.
    add_theme_support( 'automatic-feed-links' );

    // Switches default core markup for search form, comment form, and comments
    // to output valid HTML5.
    add_theme_support( 'html5', array( 'search-form', 'comment-form', 'comment-list' ) );


    // This theme supports all available post formats by default.
    // See http://codex.wordpress.org/Post_Formats

    add_theme_support( 'post-formats', array(
        'aside', 'audio', 'chat', 'gallery', 'image', 'link', 'quote', 'status', 'video'
    ) );

    // This theme uses wp_nav_menu() in one location.
    //register_nav_menu( 'primary', __( 'Navigation Menu', 'twentythirteen' ) );

    // This theme uses its own gallery styles.
    add_filter( 'use_default_gallery_style', '__return_false' );
}
add_action( 'after_setup_theme', 'beeFramework_setup' );

/*
 * Disable WordPress Update Notifications For All But Administrator
*/
add_action( 'after_setup_theme', 'remove_core_updates' );
    function remove_core_updates()
    {
    if ( ! current_user_can( 'update_core' ) ) {
        return;
    }
    add_action( 'init', create_function( '$a', "remove_action( 'init', 'wp_version_check' );" ), 2 );
    add_filter( 'pre_option_update_core', '__return_null' );
    add_filter( 'pre_site_transient_update_core', '__return_null' );
}


if ( !current_user_can('administrator') ) {
    add_action( 'init', create_function( '$a', "remove_action( 'init', 'wp_version_check' );" ), 2 );
    add_filter( 'pre_option_update_core', create_function( '$a', "return null;" ) );
}


/*
 * Load Styles and Scrips Front end
*/
function twentythirteen_scripts_styles() {

    // Add Genericons font, used in the main stylesheet.
    wp_enqueue_style( 'genericons', get_template_directory_uri() . '/fonts/genericons.css', array(), '2.09' );

    // Loads our main stylesheet.
    wp_enqueue_style( 'twentythirteen-style', get_stylesheet_uri(), array(), '2013-07-18' );

    // Loads the Internet Explorer specific stylesheet.
    wp_enqueue_style( 'twentythirteen-ie', get_template_directory_uri() . '/css/ie.css', array( 'twentythirteen-style' ), '2013-07-18' );
    wp_style_add_data( 'twentythirteen-ie', 'conditional', 'lt IE 9' );
}
add_action( 'wp_enqueue_scripts', 'twentythirteen_scripts_styles' );


/*
 * Nice Title Text
*/
function bee_freame_wp_title( $title, $sep ) {
    global $paged, $page;

    if ( is_feed() )
        return $title;

    // Add the site name.
    $title .= get_bloginfo( 'name' );

    // Add the site description for the home/front page.
    $site_description = get_bloginfo( 'description', 'display' );
    if ( $site_description && ( is_home() || is_front_page() ) )
        $title = "$title $sep $site_description";

    // Add a page number if necessary.
    if ( $paged >= 2 || $page >= 2 )
        $title = "$title $sep " . sprintf( __( 'Page %s', 'twentythirteen' ), max( $paged, $page ) );

    return $title;
}
add_filter( 'wp_title', 'bee_freame_wp_title', 10, 2 );


/**
 * Prints HTML with meta information for current post: categories, tags, permalink, author, and date.
*/
if ( ! function_exists( 'twentythirteen_entry_meta' ) ) :
function twentythirteen_entry_meta() {
    if ( is_sticky() && is_home() && ! is_paged() )
        echo '<span class="featured-post">' . __( 'Sticky', 'twentythirteen' ) . '</span>';

    if ( ! has_post_format( 'link' ) && 'post' == get_post_type() )
        twentythirteen_entry_date();

    // Translators: used between list items, there is a space after the comma.
    $categories_list = get_the_category_list( __( ', ', 'twentythirteen' ) );
    if ( $categories_list ) {
        echo '<span class="categories-links">' . $categories_list . '</span>';
    }

    // Translators: used between list items, there is a space after the comma.
    $tag_list = get_the_tag_list( '', __( ', ', 'twentythirteen' ) );
    if ( $tag_list ) {
        echo '<span class="tags-links">' . $tag_list . '</span>';
    }

    // Post author
    if ( 'post' == get_post_type() ) {
        printf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s" rel="author">%3$s</a></span>',
            esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
            esc_attr( sprintf( __( 'View all posts by %s', 'twentythirteen' ), get_the_author() ) ),
            get_the_author()
        );
    }
}
endif;

if ( ! function_exists( 'twentythirteen_entry_date' ) ) :
/**
 * Prints HTML with date information for current post.
 *
 * Create your own twentythirteen_entry_date() to override in a child theme.
 *
 * @since Twenty Thirteen 1.0
 *
 * @param boolean $echo Whether to echo the date. Default true.
 * @return string The HTML-formatted post date.
 */
function twentythirteen_entry_date( $echo = true ) {
    if ( has_post_format( array( 'chat', 'status' ) ) )
        $format_prefix = _x( '%1$s on %2$s', '1: post format name. 2: date', 'twentythirteen' );
    else
        $format_prefix = '%2$s';

    $date = sprintf( '<span class="date"><a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s">%4$s</time></a></span>',
        esc_url( get_permalink() ),
        esc_attr( sprintf( __( 'Permalink to %s', 'twentythirteen' ), the_title_attribute( 'echo=0' ) ) ),
        esc_attr( get_the_date( 'c' ) ),
        esc_html( sprintf( $format_prefix, get_post_format_string( get_post_format() ), get_the_date() ) )
    );

    if ( $echo )
        echo $date;

    return $date;
}
endif;

/**
 * Returns the URL from the post.
 */
function twentythirteen_get_link_url() {
    $content = get_the_content();
    $has_url = get_url_in_content( $content );

    return ( $has_url ) ? $has_url : apply_filters( 'the_permalink', get_permalink() );
}

/**
 * Add postMessage support for site title and description for the Customizer.
 */
function twentythirteen_customize_register( $wp_customize ) {
    $wp_customize->get_setting( 'blogname' )->transport         = 'postMessage';
    $wp_customize->get_setting( 'blogdescription' )->transport  = 'postMessage';
    $wp_customize->get_setting( 'header_textcolor' )->transport = 'postMessage';
}
add_action( 'customize_register', 'twentythirteen_customize_register' );
\$\endgroup\$
0

1 Answer 1

4
\$\begingroup\$

The code seems pretty solid. I'll make some small observations:

  1. I'd remove add_action( 'after_setup_theme', 'remove_core_updates' ); and add a call to the function remove_core_updates() at the end of beeFramework_setup().

  2. The use of anonymous functions, create_function, prevents something like:

    # In your functions.php file
    add_action( 'init', 'remove_version_check', 2 );
    function remove_version_check() {
        remove_action( 'init', 'wp_version_check' );
    }
    
    # Later in a child theme or plugin
    remove_action( 'init', 'remove_version_check', 2 );
    
  3. The filter pre_option_update_core could be simply:

    add_filter( 'pre_option_update_core', '__return_null' );
    
  4. Make sure you know the difference between get_template_directory_uri() and get_stylesheet_directory_uri() and use them accordingly.

  5. Why would you check if Twenty Thirteen functions exists? Create good prefixes for your functions and drop the function_exists thing. I'd recommended using hooks instead of pluggable functions.

  6. And, finally, speaking of hooks, provide some of them so people can modify the theme behavior from a child theme or plugin:

    # In your functions.php file
    $post_formats = array( 'aside', 'audio', 'chat', 'gallery', 'image', 'link', 'quote', 'status', 'video' );
    add_theme_support( 'post-formats', apply_filters( 'my_framework_post_formats', $post_formats ) );
    
    # Later on a child theme or plugin we can reduce their number
    add_filter( 'my_framework_post_formats', 'my_new_post_formats' );
    function my_new_post_formats( $formats ) {
        return array( 'audio', 'gallery', 'image' );
    }
    
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.