I sprayed WD40 on my disk rotor and now my rotor and pads are contaminated and now my rear brakes are practically useless.Is there any way to fix the problem without swapping out the brake pads and rotor ?
The rotor needs thorough degreasing, finishing with alcohol to ensure there is no residue. The trick is that when there's actual oil on it, you should use something nasty like brake cleaner, but then finish with alcohol. If it's really nasty another good way of getting the bulk of the contamination off is soak it in boiling water with some dish soap, then wipe/rinse, then finish with alcohol. That is a good way of getting all the millions of little holes cleaned out at once.
For the pads, there's not a guaranteed fix, but you can usually burn it out. Heat it slow and steady with a heat gun. Get it to smoke and then stop smoking. Test ride it in controlled circumstances to see how effective it was. There is some risk of ruining the bond with the back plate and have it fail in use, so it's at your own risk. Typically this works but it does take the right technique.
No sorry. You've contaminated the pads by braking while there is a contaminant on the rotor.
Had you cleaned the rotor before braking, the pads might have survived.
As it stands, your best solution is to remove the wheel, clean the rotor thoroughly with Isopropyl alcohol (especially between the rotor's spokes and in all the holes) and to replace your brake pads with new.
It may be possible to clean the brake pads by blotting, but in my experience that never works. Some people use heat to make oils drip out of pads, but again, that's never worked for me.
The only prevention is to be much more careful with overspray in the future. Use a piece of cardboard or rag/cloth to protect vulnerable parts. You can also not use aerosols and use drop bottles instead. There's no reason WD40 should be near a bicycle anyway.
I can conform Criggie's experience that properly de-contaminating pads is a rather hopeless endeavour. You can get them to work again, but they won't have a proper, reliable, controllable bite – they only start screaming when you try to brake harder. To get your brakes to prime condition again, the only reliable thing is new pads.
What I found to be somewhat successful for the rear brake is to burn it out by its own power: select a properly steep fire road, 400 m with 100 m of elevation should do well. Ride it down at 10-15 km/h, while simultaneously braking (back brake only) and pedalling hard in a middle gear. That should bring the brake to smoking-hot temperature, you'll notice from that it will stop being responsive to changes in lever pressure. At that point halt (only then using the front brake) and quickly spray some water over the rear brake (this should cause steam sizzling).
I found that this self-cleaning made the brake work better afterwards than anything I could do with direct cleaning or heating, probably because it combines thermal and mechanical cleaning at the pad-rotor surface. And it's a low-effort solution (except for the climb before the descent, but that's just good training...)