patternphpMinor
WordPress Functions.php file
Viewed 0 times
functionsphpfilewordpress
Problem
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 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
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 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
Solution
The code seems pretty solid. I'll make some small observations:
-
I'd remove
-
The use of anonymous functions,
-
The filter
-
Make sure you know the difference between
-
Why would you check if Twenty Thirteen functions exists? Create good prefixes for your functions and drop the
-
And, finally, speaking of hooks, provide some of them so people can modify the theme behavior from a child theme or plugin:
-
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().-
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 );-
The filter
pre_option_update_core could be simply:add_filter( 'pre_option_update_core', '__return_null' );-
Make sure you know the difference between
get_template_directory_uri() and get_stylesheet_directory_uri() and use them accordingly.-
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.-
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' );
}Code Snippets
# 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 );add_filter( 'pre_option_update_core', '__return_null' );# 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' );
}Context
StackExchange Code Review Q#29587, answer score: 4
Revisions (0)
No revisions yet.