Umar ibn al-Khattab (may Allah be pleased with him) was saying while seated on the pulpit: I heard from the Messenger of Allah ﷺ that he said: All deeds are judged by intentions, and every person will have only what he intended. So if someone’s migration is for the sake of acquiring worldly gain or to marry a woman, then his migration is in reality for that which he migrated for. (That is, all matters depend on intention: if the intention is worldly, there will be no reward for it, and if one intends some deed for the sake of the religion, there will be reward for it.)
Benefit:
In all the reliable and authentic books of hadith, the background of this incident is stated as follows: a man sent a marriage proposal to a woman named “Umm Qays”. She set a condition that he should migrate and come, and then the marriage could take place. So when he migrated, the woman married him. The Companions (may Allah be pleased with them) used to refer to this man as “Muhajir Umm Qays”.
This hadith makes clear the importance of intention, but this does not at all mean that evil deeds become good by having a good intention. Rather, what is meant is that permissible deeds become good with a good intention and become bad with a bad intention.