Adding custom code to WordPress can help you tweak styles, add functionality, or fix theme/plugin issues β but doing it the wrong way can break your site.
This guide shows you safe methods to add custom CSS, JavaScript, and PHP without risking crashes or losing changes after updates.
π§΅ 1. How to Add Custom CSS in WordPress
β Easiest Way: WordPress Customizer
- Go to Appearance > Customize
- Click Additional CSS
- Paste your CSS code
- Click Publish
π‘ Best for minor style changes like fonts, colors, spacing.
β Advanced: Use a Child Theme or Custom Plugin
If you’re managing large stylesheets:
- Add CSS to your child theme’s
style.css
- Or enqueue a custom CSS file via a plugin (see below)
π» 2. How to Add Custom JavaScript
β Method 1: Use a Plugin (Recommended)
Use a plugin like:
- WPCode (formerly Insert Headers & Footers)
- Simple Custom CSS and JS
Steps:
- Install & activate the plugin
- Add your JS code to the header, footer, or a specific post/page
- Save and test
This keeps your code separate from theme files, so it wonβt be lost during updates.
β Method 2: Enqueue Script via Child Theme
For developers:
- Create or edit your child theme’s
functions.php
- Add:
phpCopyEditfunction wpwise_enqueue_custom_script() {
wp_enqueue_script('my-custom-js', get_stylesheet_directory_uri() . '/js/custom.js', array(), '1.0', true);
}
add_action('wp_enqueue_scripts', 'wpwise_enqueue_custom_script');
- Upload
custom.js
to your child themeβs/js/
folder
βοΈ 3. How to Add Custom PHP Code
β οΈ Never paste PHP directly into a parent theme. Youβll lose it during updates or break your site if there’s a syntax error.
β Option 1: Use a Code Snippets Plugin
Recommended plugin: WPCode
Steps:
- Go to Plugins > Add New β Search for “WPCode”
- Install & activate
- Add a new snippet and paste your PHP code
- Choose the location (site-wide, frontend, etc.)
- Save and activate
βοΈ Safer than editing functions.php
manually
βοΈ You can toggle code on/off without touching files
β Option 2: Use a Child Theme
- Create a child theme (or make sure you have one)
- Edit
functions.php
in the child theme folder - Paste your PHP code at the end
Example:
phpCopyEdit// Disable WordPress admin bar
add_filter('show_admin_bar', '__return_false');
π Always back up before editing PHP.
π§ Bonus Tips
- Use Codepen or JSFiddle to test CSS/JS before adding
- Always clear your cache (browser + caching plugin) after edits
- Use PHP Code Checker to catch errors before saving
π¨ What to Avoid
β Editing core WordPress files (wp-config.php
, core PHP
)
β Adding code to a parent theme directly
β Pasting unvetted code from forums without understanding it
π¦ Summary Table
Code Type | Best Method | Plugin Option |
---|---|---|
CSS | Customizer / Child Theme | Simple Custom CSS & JS |
JS | Enqueue or Plugin | WPCode |
PHP | Snippets Plugin or Child Theme | WPCode |
π¬ Need Help?
Still unsure where to put your code? Drop your snippet in the comments and weβll help you place it the smart (and safe) way.