Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
Wordpress

WordPress: Enqueue Scripts and Styles the Right Way

- 05.06.26 - ErcanOPAK
📦 Never Hardcode Script Tags Again

Hardcoded <script> tags cause conflicts. wp_enqueue_script() handles dependencies, order, and conditional loading.

📝 Enqueue in functions.php

function my_theme_assets() {
    // Styles
    wp_enqueue_style('my-theme', get_stylesheet_uri());
    wp_enqueue_style('custom', get_template_directory_uri() . '/css/custom.css', array(), '1.0.0');
    
    // Scripts
    wp_enqueue_script('my-script', get_template_directory_uri() . '/js/main.js', array('jquery'), '1.0.0', true);
    
    // Localize script (pass PHP data to JS)
    wp_localize_script('my-script', 'myAjax', array(
        'ajax_url' => admin_url('admin-ajax.php'),
        'nonce' => wp_create_nonce('my_nonce')
    ));
}
add_action('wp_enqueue_scripts', 'my_theme_assets');

🎯 Conditional Loading

// Only on single post pages
if (is_single()) {
    wp_enqueue_script('post-specific', '...');
}

// Only on contact page
if (is_page('contact')) {
    wp_enqueue_script('google-maps', 'https://maps.googleapis.com/...');
}

// Only for logged-in users
if (is_user_logged_in()) {
    wp_enqueue_script('logged-in', '...');
}

// Deregister unused scripts (performance)
wp_deregister_script('wp-embed');
wp_dequeue_style('wp-block-library');

💡 Best Practices

  • Always load jQuery from WordPress (not external CDN)
  • Set $in_footer = true for non-critical scripts
  • Version numbers for cache busting
  • Deregister scripts you don’t need (emoji, embed)

“Hardcoded jQuery CDN then loaded another. Conflicts everywhere. Enqueue system fixed dependency order. WordPress best practice non-negotiable.”

— WordPress Developer

Related posts:

WordPress: Create Custom Widget Areas with register_sidebar()

WordPress: Use Heartbeat API Control to Reduce CPU Usage by 80%

WordPress: Replace WP-Cron with Real Cron for Scheduled Tasks

Post Views: 4

Post navigation

Photoshop: Use Liquify Tool for Portrait Retouching
Kubernetes: Use Topology Spread Constraints for Even Pod Distribution

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

June 2026
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  
« May    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (953)
  • How to add default value for Entity Framework migrations for DateTime and Bool (882)
  • Get the First and Last Word from a String or Sentence in SQL (838)
  • How to select distinct rows in a datatable in C# (808)
  • How to make theater mode the default for Youtube (805)
  • Add Constraint to SQL Table to ensure email contains @ (580)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (579)
  • Average of all values in a column that are not zero in SQL (538)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (505)
  • Find numbers with more than two decimal places in SQL (454)

Recent Posts

  • C#: Use String Interpolation Instead of Concatenation
  • C#: Use Tuples to Return Multiple Values from Methods
  • SQL: Use ISNULL and NULLIF for Smart NULL Handling
  • .NET Core: Use Data Annotations for Model Validation
  • Git: Use Git Clean to Remove Untracked Files
  • Ajax: Add Custom Headers to Fetch Requests
  • JavaScript: Use console.table to Display Arrays as Tables
  • HTML: Use Spellcheck Attribute to Enable Browser Spell Check
  • CSS: Use user-select to Prevent Text Selection
  • Windows 11: Use Snipping Tool for Instant Screenshots

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (953)
  • How to add default value for Entity Framework migrations for DateTime and Bool (882)
  • Get the First and Last Word from a String or Sentence in SQL (838)
  • How to select distinct rows in a datatable in C# (808)
  • How to make theater mode the default for Youtube (805)

Recent Posts

  • C#: Use String Interpolation Instead of Concatenation
  • C#: Use Tuples to Return Multiple Values from Methods
  • SQL: Use ISNULL and NULLIF for Smart NULL Handling
  • .NET Core: Use Data Annotations for Model Validation
  • Git: Use Git Clean to Remove Untracked Files

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com