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 Allaah, 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 Allaah alone.
2 – It should be in accordance with the ways in which Allaah has ordained that He be worshipped only. That is achieved by following the Prophet (peace and blessings of Allaah 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 Allaah be upon him).
The evidence for these two conditions is the verse in which Allaah 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"
Ibn Katheer (may Allaah 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 Allaah.
" and associate none as a partner in the worship of his Lord" – means the deed by which is sought the Countenance of Allaah 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 Allaah alone, and it must be in accordance with the sharee'ah of the Messenger of Allaah (peace and blessings of Allaah be upon him). End quote.
Tafseer Ibn Katheer, 4/108
Hence Allaah says in the greatest soorah in the Qur'aan (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"
No act of worship is valid unless it is in accordance with the right way, the straight path, which Allaah has enjoined, by following His Prophet (peace and blessings of Allaah be upon him). In al-Saheeh it is narrated from 'Aa'ishah (may Allaah be pleased with her) that the Messenger of Allaah (peace and blessings of Allaah 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 Allaah 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. Allaah says (interpretation of the meaning):
"Say (O Muhammad to mankind): If you (really) love Allaah, then follow me (i.e. accept Islamic Monotheism, follow the Qur’aan and the Sunnah), Allaah will love you and forgive you your sins. And Allaah 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 Allaah 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 Allaah.
Hence when Ibn Mas'ood (may Allaah be pleased with him) denounced those who gathered to remember Allaah (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 Allaah. 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 Allaah, 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 Allaah 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 Allaah 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-Albaani in Saheeh al-Targheeb, 601.
3 – It was narrated from Sahl ibn Haneef that the Prophet said: "Whoever sincerely asks Allaah 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 Allaah, then is prevented from doing that deed, Allaah 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.
See also the answers to questions no. 21519 and 21362
And Allaah knows best..