The Code Editor allows you to add custom PHP code to your DigiFlash site without modifying theme files. PHP snippets extend WordPress functionality, create custom functions, modify theme behavior, and add powerful server-side logic to your site.
Creating a PHP Snippet
Navigate to Code Snippets in your WordPress admin menu and click Add New. This opens the code snippet editor where you’ll create your custom PHP code.
Enter a descriptive title for your snippet in the title field at the top. Use clear names that identify the function’s purpose, such as “Custom Post Type Registration” or “Redirect Login Page” for easy management.
In the Code Type dropdown, select PHP. This tells DigiFlash Pro to treat your code as PHP and execute it on your WordPress site.
Understanding Execution Modes
PHP snippets in DigiFlash Pro offer two execution modes to accommodate different coding styles and complexity levels.
Automatic Mode – Your PHP code is automatically wrapped in a function and executed on the WordPress hook you select. Write your code without opening <?php or closing ?> tags unless you need to switch between PHP and HTML within the snippet. This mode works well for simple, straightforward code that performs a single task.
Manual Mode (Custom) – You write complete, standalone PHP code exactly as you would in a functions.php file. This includes defining your own functions, adding hooks, and managing all PHP tags yourself. This mode provides full control for complex implementations with multiple functions, conditional logic, or advanced WordPress integration.
Configuring Execution Mode and Hook
After selecting PHP as the code type, the Execution Mode dropdown appears with several options.
For Automatic Mode, choose from these hooks:
- Init (Automatic) – Runs during WordPress initialization, after WordPress is fully loaded but before headers are sent
- WP Loaded (Automatic) – Runs after WordPress, plugins, and theme are fully loaded
- WP Head (Automatic) – Executes in the site header
- WP Footer (Automatic) – Executes in the site footer
- Enqueue Scripts (Automatic) – Runs when WordPress loads scripts and styles
- Admin Init (Automatic) – Runs during WordPress admin initialization
- Admin Enqueue Scripts (Automatic) – Runs when WordPress loads admin scripts and styles
For Manual Mode, select:
- Custom (Manual – Like functions.php) – Your code executes exactly as written, like it would in your theme’s functions.php file
The Priority field appears for Automatic Mode, allowing you to control execution order when multiple functions hook into the same location. Lower numbers run earlier, with 10 being the default.
Writing Automatic Mode PHP
In Automatic Mode, write your PHP code as if you’re inside a function. DigiFlash Pro handles the function wrapper and hook registration automatically.
Here are examples for Automatic Mode:
Simple Echo Output (wp_footer hook)
echo '<div class="custom-footer-text">';
echo 'Copyright © ' . date('Y') . ' Your Company';
echo '</div>';
Modify Excerpt Length (init hook)
add_filter('excerpt_length', function($length) {
return 50;
});
Add Custom Body Class (wp_head hook)
add_filter('body_class', function($classes) {
if (is_front_page()) {
$classes[] = 'custom-homepage';
}
return $classes;
});
Enqueue Custom Script (wp_enqueue_scripts hook)
wp_enqueue_script(
'my-custom-script',
get_template_directory_uri() . '/assets/js/custom.js',
array('jquery'),
'1.0.0',
true
);
Change Login Logo URL (init hook)
add_filter('login_headerurl', function() {
return home_url();
});
Mixed PHP and HTML Output (wp_footer hook)
?>
<div class="custom-notice" style="background: #f0f0f0; padding: 15px; text-align: center;">
<p>Welcome to our site! Today is <?php echo date('F j, Y'); ?></p>
</div>
<?php
Disable WordPress Emojis (init hook)
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');
remove_action('admin_print_scripts', 'print_emoji_detection_script');
remove_action('admin_print_styles', 'print_emoji_styles');
Writing Manual Mode PHP
In Manual Mode, write complete PHP code with full control over structure, functions, and hooks. This mode works exactly like adding code to your theme’s functions.php file.
Here are examples for Manual Mode:
Register Custom Post Type
<?php
function my_custom_post_type() {
register_post_type('portfolio',
array(
'labels' => array(
'name' => 'Portfolio',
'singular_name' => 'Portfolio Item'
),
'public' => true,
'has_archive' => true,
'supports' => array('title', 'editor', 'thumbnail'),
'show_in_rest' => true,
)
);
}
add_action('init', 'my_custom_post_type');
Create Custom Shortcode
<?php
function custom_button_shortcode($atts) {
$atts = shortcode_atts(array(
'text' => 'Click Me',
'url' => '#',
'color' => 'blue'
), $atts);
return '<a href="' . esc_url($atts['url']) . '" class="custom-button btn-' . esc_attr($atts['color']) . '">' . esc_html($atts['text']) . '</a>';
}
add_shortcode('custom_button', 'custom_button_shortcode');
Add Custom Widget Area
<?php
function register_custom_sidebar() {
register_sidebar(array(
'name' => 'Custom Sidebar',
'id' => 'custom-sidebar',
'description' => 'Custom sidebar area',
'before_widget' => '<div class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
));
}
add_action('widgets_init', 'register_custom_sidebar');
Redirect After Login
<?php
function custom_login_redirect($redirect_to, $request, $user) {
if (isset($user->roles) && is_array($user->roles)) {
if (in_array('subscriber', $user->roles)) {
return home_url('/dashboard');
}
}
return $redirect_to;
}
add_filter('login_redirect', 'custom_login_redirect', 10, 3);
Add Custom Admin Menu Page
<?php
function add_custom_admin_page() {
add_menu_page(
'Custom Settings',
'Custom Settings',
'manage_options',
'custom-settings',
'render_custom_admin_page',
'dashicons-admin-generic',
20
);
}
add_action('admin_menu', 'add_custom_admin_page');
function render_custom_admin_page() {
?>
<div class="wrap">
<h1>Custom Settings</h1>
<p>Your custom admin page content here.</p>
</div>
<?php
}
Choosing Between Automatic and Manual Mode
Use Automatic Mode when you need simple, single-purpose code that performs one task, want DigiFlash Pro to handle function wrapping and hook registration automatically, are adding filters or actions without complex logic, or are new to WordPress development and want a simpler approach.
Use Manual Mode when you need to define multiple functions that work together, require complex conditional logic or class definitions, want complete control over hook priorities and execution, are creating custom post types, taxonomies, or widgets, or need to replicate code that would normally go in functions.php.
Publishing Your PHP Snippet
Once you’ve written your PHP code and configured the execution mode, click Publish to activate the snippet. The code executes immediately according to your selected hook and mode.
If you want to save the snippet without activating it yet, click Save Draft instead. This stores your PHP without executing it on the live site.
Testing Your PHP Code
After publishing, verify your PHP code works correctly by checking the expected behavior on your site. For example, if you added a custom post type, check that it appears in the WordPress admin. If you modified query behavior, verify the changes on the frontend.
Always test PHP snippets on a staging site first before activating them on your live site. PHP errors can break your site and make it inaccessible.
Use browser developer tools and WordPress debug mode to identify any errors or warnings generated by your code.
Handling PHP Errors
If your PHP snippet contains errors, it can cause your site to display error messages or become inaccessible. DigiFlash Pro attempts to detect broken snippets and disable them automatically to prevent site crashes.
If you encounter a white screen or PHP error after activating a snippet, access your WordPress admin and navigate to Code Snippets. The problematic snippet will be marked as broken. Edit the snippet to fix the error, then republish it.
Always validate your PHP syntax before publishing. Test code logic carefully to ensure it produces the expected results without conflicts.
Security Considerations
PHP code has full access to your WordPress site and database. Only add PHP snippets from trusted sources. Never copy and paste PHP code without understanding what it does. Sanitize and validate any user input your code processes. Use WordPress functions like esc_html(), esc_attr(), and wp_kses() when outputting data.
Poorly written PHP can create security vulnerabilities, so follow WordPress coding standards and best practices.
Managing Multiple PHP Snippets
You can create unlimited PHP snippets, each handling different functionality. Organize snippets by purpose such as “Custom Post Types”, “User Management”, “Query Modifications”, or “Admin Customizations”.
Each snippet can be independently enabled or disabled from the Code Snippets list. This makes troubleshooting easier by allowing you to isolate which code causes issues.