Solution 1:
In WordPress, when you enqueue jQuery:
- By default, you must use jQuery instead of $ to avoid conflicts with other libraries.
- Wrapping your code in a function works fine, or you can load jQuery differently (though this is generally not recommended).
- If you want to use $(document).ready(), you can safely pass $ into the function:
jQuery(function($) {
// Your code using $ safely here
});
✅ Explanation:
- This ensures $ references jQuery only within your function, avoiding conflicts with other scripts.
- This is the recommended approach for WordPress-compatible jQuery code.