The functions.php file is where you add custom PHP code to extend DigiFlash’s functionality. This file executes before any template or block is rendered, making it ideal for registering features, adding hooks, and modifying WordPress behavior.
Understanding DigiFlash’s functions.php
DigiFlash’s core functions.php contains three main functions:
digiflash_styles()– Enqueues theme stylesheetsdigiflash_editor_style()– Loads editor stylesdigiflash_register_block_patterns()– Registers pattern categories
These functions use WordPress hooks to integrate with the system. When customizing, you can add to this file or use a child theme to avoid losing changes during updates.
Adding Custom Functions
Place new functions in functions.php of a child theme. Always wrap functions in conditional checks:
if (!function_exists('my_custom_function')) :
function my_custom_function() {
// Your code here
}
endif;
This prevents conflicts if the function name already exists elsewhere.
Registering Custom Post Types
Add custom post types for specialized content:
function digiflash_register_portfolio() {
$args = array(
'labels' => array(
'name' => __('Portfolio', 'digiflash'),
'singular_name' => __('Portfolio Item', 'digiflash'),
),
'public' => true,
'has_archive' => true,
'show_in_rest' => true,
'supports' => array('title', 'editor', 'thumbnail', 'excerpt'),
'menu_icon' => 'dashicons-portfolio',
);
register_post_type('portfolio', $args);
}
add_action('init', 'digiflash_register_portfolio');
The show_in_rest parameter enables block editor support.
Registering Custom Taxonomies
Create custom taxonomies for organizing content:
function digiflash_register_project_type() {
$args = array(
'labels' => array(
'name' => __('Project Types', 'digiflash'),
'singular_name' => __('Project Type', 'digiflash'),
),
'hierarchical' => true,
'show_in_rest' => true,
'show_admin_column' => true,
);
register_taxonomy('project_type', array('portfolio'), $args);
}
add_action('init', 'digiflash_register_project_type');
Adding Theme Support Features
Enable additional WordPress features:
function digiflash_custom_setup() {
// Add custom image sizes
add_image_size('digiflash-large', 1920, 1080, true);
add_image_size('digiflash-medium', 1200, 675, true);
add_image_size('digiflash-thumbnail', 400, 300, true);
// Enable additional theme supports
add_theme_support('align-wide');
add_theme_support('responsive-embeds');
add_theme_support('custom-spacing');
add_theme_support('custom-line-height');
}
add_action('after_setup_theme', 'digiflash_custom_setup');
Enqueueing Additional Scripts
Add custom JavaScript files:
function digiflash_custom_scripts() {
wp_enqueue_script(
'digiflash-custom',
get_stylesheet_directory_uri() . '/assets/js/custom.js',
array('jquery'),
'1.0.0',
true
);
// Pass PHP variables to JavaScript
wp_localize_script('digiflash-custom', 'digiflashData', array(
'ajaxUrl' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('digiflash-nonce'),
));
}
add_action('wp_enqueue_scripts', 'digiflash_custom_scripts');
The last parameter true loads the script in the footer for better performance.
Enqueueing Additional Stylesheets
Add custom CSS files:
function digiflash_custom_styles() {
wp_enqueue_style(
'digiflash-custom',
get_stylesheet_directory_uri() . '/assets/css/custom.css',
array('digiflash-style'),
'1.0.0'
);
}
add_action('wp_enqueue_scripts', 'digiflash_custom_styles', 20);
The priority 20 ensures it loads after DigiFlash’s main styles.
Registering Widget Areas
Although DigiFlash is a block theme, you can register widget areas for plugins:
function digiflash_register_sidebars() {
register_sidebar(array(
'name' => __('Sidebar', 'digiflash'),
'id' => 'sidebar-1',
'description' => __('Add widgets here', 'digiflash'),
'before_widget' => '<section id="%1$s" class="widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
));
}
add_action('widgets_init', 'digiflash_register_sidebars');
Adding Custom Block Pattern Categories
Extend the pattern library with custom categories:
function digiflash_custom_pattern_categories() {
if (function_exists('register_block_pattern_category')) {
register_block_pattern_category(
'team',
array(
'label' => __('Team', 'digiflash'),
'description' => __('Team member sections', 'digiflash'),
)
);
register_block_pattern_category(
'portfolio',
array(
'label' => __('Portfolio', 'digiflash'),
'description' => __('Portfolio showcase patterns', 'digiflash'),
)
);
}
}
add_action('init', 'digiflash_custom_pattern_categories');
Modifying Excerpt Length
Change the default excerpt length:
function digiflash_custom_excerpt_length($length) {
return 30;
}
add_filter('excerpt_length', 'digiflash_custom_excerpt_length');
Customizing Read More Link
Modify the excerpt “read more” text:
function digiflash_custom_excerpt_more($more) {
return '... <a class="read-more" href="' . get_permalink() . '">' . __('Continue Reading', 'digiflash') . '</a>';
}
add_filter('excerpt_more', 'digiflash_custom_excerpt_more');
Adding Custom Body Classes
Add contextual classes to the body tag:
function digiflash_custom_body_class($classes) {
if (is_page_template('templates/blank.html')) {
$classes[] = 'blank-template';
}
if (has_post_thumbnail()) {
$classes[] = 'has-thumbnail';
}
return $classes;
}
add_filter('body_class', 'digiflash_custom_body_class');
Disabling Block Patterns
Remove default WordPress patterns:
function digiflash_remove_core_patterns() {
remove_theme_support('core-block-patterns');
}
add_action('after_setup_theme', 'digiflash_remove_core_patterns');
Adding Custom MIME Types
Allow additional file types for upload:
function digiflash_custom_mime_types($mimes) {
$mimes['svg'] = 'image/svg+xml';
$mimes['webp'] = 'image/webp';
return $mimes;
}
add_filter('upload_mimes', 'digiflash_custom_mime_types');
Limiting Post Revisions
Control how many post revisions WordPress saves:
if (!defined('WP_POST_REVISIONS')) {
define('WP_POST_REVISIONS', 5);
}
Place this at the top of functions.php, outside any function.
Creating Shortcodes
Add reusable shortcodes:
function digiflash_button_shortcode($atts) {
$atts = shortcode_atts(array(
'text' => 'Click Here',
'url' => '#',
'style' => 'primary',
), $atts);
return '<a href="' . esc_url($atts['url']) . '" class="wp-block-button__link has-' . esc_attr($atts['style']) . '-background-color">' . esc_html($atts['text']) . '</a>';
}
add_shortcode('button', 'digiflash_button_shortcode');
Use in content: [button text="Learn More" url="/about" style="primary"]
Customizing Login Page
Modify the WordPress login screen:
function digiflash_custom_login_logo() {
echo '<style>
#login h1 a {
background-image: url(' . get_stylesheet_directory_uri() . '/assets/images/logo.png);
width: 300px;
height: 100px;
background-size: contain;
}
</style>';
}
add_action('login_enqueue_scripts', 'digiflash_custom_login_logo');
Disabling Emojis
Remove WordPress emoji scripts for performance:
function digiflash_disable_emojis() {
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');
}
add_action('init', 'digiflash_disable_emojis');
Adding Admin Notices
Display custom notices in the WordPress admin:
function digiflash_admin_notice() {
if (current_user_can('manage_options')) {
echo '<div class="notice notice-info is-dismissible">
<p>' . __('Welcome to DigiFlash! Check out the documentation.', 'digiflash') . '</p>
</div>';
}
}
add_action('admin_notices', 'digiflash_admin_notice');
Security Considerations
Always sanitize and escape data in functions.php:
// Sanitize input
$value = sanitize_text_field($_POST['field']);
// Escape output
echo esc_html($value);
echo esc_url($url);
echo esc_attr($attribute);
// Verify nonces for forms
if (isset($_POST['nonce']) && wp_verify_nonce($_POST['nonce'], 'action_name')) {
// Process form
}
Using Child Theme functions.php
When using a child theme, the child’s functions.php loads before the parent’s. This means you can define functions that the parent theme checks for:
// In child theme functions.php
if (!function_exists('digiflash_custom_function')) :
function digiflash_custom_function() {
// Override parent function
}
endif;
The child theme version will be used instead of the parent’s.