Praise be to Allah.
No act of worship will be accepted from a Muslim unless it meets two basic conditions:
1 – Sincerity of intention towards Allah, may He be exalted. This means that the aims of the person in his words and deeds, both outward and inward, should be to seek the Countenance of Allah alone.
2 – It should be in accordance with the ways in which Allah has ordained that He be worshipped only. That is achieved by following the Prophet (peace and blessings of Allah be upon him) and what he taught, and shunning anything that goes against that, and not introducing any new acts or forms of worship that have not been narrated in proven reports from him (peace and blessings of Allah be upon him).
The evidence for these two conditions is the verse in which Allah says (interpretation of the meaning):
" So whoever hopes for the Meeting with his Lord, let him work righteousness and associate none as a partner in the worship of his Lord" [al-Kahf 18:110]
Ibn Katheer (may Allah have mercy on him) said:
" So whoever hopes for the Meeting with his Lord" – means His reward
" let him work righteousness " – means, that is in accordance with the laws of Allah.
" and associate none as a partner in the worship of his Lord" – means the deed by which is sought the Countenance of Allah alone, with no partner or associate. These are the two features of the deed that is acceptable: it must be sincerely for the sake of Allah alone, and it must be in accordance with the sharee'ah of the Messenger of Allah (peace and blessings of Allah be upon him)."(Tafseer Ibn Katheer, 4/108)
Hence Allah says in the greatest soorah in the Quran (interpretation):
" You (Alone) we worship, and You (Alone) we ask for help (for each and everything)"
[al-Faatihah 1:5], to indicate that Tawheed and sincerity are essential in order for a deed to be valid.
The second condition comes immediately after that:
"Guide us to the Straight Way" [al-Faatihah 1:6]
No act of worship is valid unless it is in accordance with the right way, the straight path, which Allah has enjoined, by following His Prophet (peace and blessings of Allah be upon him). In al-Saheeh it is narrated from 'Aa'ishah (may Allah be pleased with her) that the Messenger of Allah (peace and blessings of Allah be upon him) said: "Whoever does any action that is not in accordance with this matter of ours will have it rejected." Narrated by Muslim, 1718. i.e., his deed will be thrown back at him and not accepted from him. If anyone goes against these two conditions (sincerity towards Allah alone and following His laws) in any action, then he will not benefit from it. The one who seeks goodness and wants to please his Lord, let him worship Him and draw near to Him by the means that He has ordained. Allah says (interpretation of the meaning):
"Say (O Muhammad to mankind): If you (really) love Allah, then follow me (i.e. accept Islamic Monotheism, follow the Quran and the Sunnah), Allah will love you and forgive you your sins. And Allah is Oft-Forgiving, Most Merciful" [Aal 'Imraan 3:31]
So it is not sufficient to have a good intention, and it will not intercede for him if he goes against sharee'ah and worships Allah by means of any kind of bid'ah (innovation). Many of those who invent innovations – because of their ignorance – invent these innovations in order to draw closer to Allah.
Hence when Ibn Mas'ood (may Allah be pleased with him) denounced those who gathered to remember Allah (dhikr), they gave the excuse that their intention was good and that they did not mean anything but good, and he said to them: "How many of those who intend good do not attain it!" Narrated by al-Daarimi, 204. So it is not sufficient to have a good intention in order to do good and attain the reward and draw closer to Allah. Rather it is essential to also act in accordance with sharee'ah and avoid bid'ah.
A good intention may intercede for a person in two cases:
1 – Turning habits into acts of worship
A good intention may turn a habit into an act of worship for which a person will be rewarded. So by eating and drinking he may intend to strengthen himself to obey Allah, and by getting married he may intend to keep himself and his wife chaste, and so on.
2 – Earning reward without doing any deed, if the Muslim is definite in his intention.
A Muslim may form a definite intention to do acts that are prescribed in sharee'ah, then something prevents him from doing that – but he will still be rewarded for it. There are a number of ahaadeeth concerning that, such as the following:
1 – It was narrated that Jaabir (may Allah be pleased with him) said: We were with the Prophet on a campaign and he said: "You did not travel any distance or cross any valley but in Madeenah there are men who were with you, but they were kept behind by sickness." According to another version: "… but they shared the reward with you." Narrated by Muslim, 1911.
2 – It was narrated from Abu'l-Darda' (may Allah be pleased with him) that the Prophet said: "Whoever goes to his bed intending to get up and pray at night, then his eyes overwhelm him and (he sleeps) until morning, the (reward for) that which he intended will be written for him, and his sleep is a charity for him given by his Lord, may He be glorified and exalted." Narrated by al-Nasaa’i, 1787; Ibn Maajah, 1344; classed as saheeh by Shaykh al-Albani in Saheeh al-Targheeb, 601.
3 – It was narrated from Sahl ibn Haneef that the Prophet said: "Whoever sincerely asks Allah for martyrdom will attain the status of the martyrs even if he dies in his bed." Narrated by Muslim, 1909.
And there are many other such reports, all of which indicate that whoever forms a sincere, definite intention to do good or obey Allah, then is prevented from doing that deed, Allah will decree the reward for him.
So this is the case where a sincere intention will intercede for a person until he is rewarded for it.
And Allah knows best..