Wa `alaykum as-Salamu wa Rahmatullahi wa Barakatuh.
In the Name of Allah, Most Gracious, Most Merciful.
All praise and thanks are due to Allah, and peace and blessings be upon His Messenger.
Dear brother, thank you for your question and for your interest to be more knowledgeable about Islam’s rulings.
This is a controversial issue among scholars; some allow it while others forbid it. Anyway, each case should be examined on its own applying the opinion that is beneficial for the child.
The scholars are unanimously agreed that the bed is the basic principle in establishing lineage. What is meant by the bed is marriage between the man and the woman.
For more clarification, let us cite this fatwa from, islamqa.info,
The scholars differed with regard to the zani (person who committed adultery) if he wants to acknowledge his illegitimate child and call him after himself: does that prove the child’s lineage in legitimate terms, or not? There are two well-known views:
1- That the illegitimate child is not to be attributed to the zani even if he acknowledges him and names him after himself.
This is the view of most of the scholars of the four madhhabs, and of the Zhahiris and others.
Based on this view, the illegitimate child – whether male or female – is not to be attributed to the zani and is not to be described as his child. Rather, he is to be attributed to his mother, and he is a mahram to her and inherits from her like her other children.
Fatwas were issued on the basis of this view by Shaykh Ibn Ibrahim, as in his Fatawa (11/146); Shaykh Ibn Baz, as in Majmu` al-Fatawa (18/124); and by the Standing Committee (al-Lajnah ad-Da’imah, 20/387).
That is because the Prophet (peace and blessings be upon him) said, “The child is to be attributed to the bed (i.e., to the husband) and the adulterer deserves nothing.” (Al-Bukhari and Muslim)
The relevant point here is that the Prophet (peace and blessings be upon him) did not describe the child as belonging to anyone other than the bed (i.e., the husband), and he said that the adulterer did not deserve anything at all. Attributing the child to the adulterer is attributing the child to someone other than the husband.
The words, “The child is to be attributed to the husband” imply that attribution of the child is to be to the marriage only.
2- That if the zani acknowledges his illegitimate child, then he should be attributed to him.
This is the view of `Urwah ibn az-Zubayr, Sulaymani bnYasar, al-Hasan al-Basri, Ibn Sireen, Ibrahim an-Nakh`i, and Ishaq ibn Rahawayh, as was narrated from them by Ibn Qudamah in al-Mughni (9/123).
This view was favored by Shaykhul-Islam Ibn Taymiyah (may Allah have mercy on him) and his student Ibn al-Qayyim.
Among contemporary scholars it was also favored by Shaykh Muhammad Rasheed Rida in Tafseer al-Manar (4/382), and by Shaykh Ibn `Uthaymeen (may Allah have mercy on him), as in Ash-Sharh al-Mumti` (12/127)
That is because this child was born from his sperm, so he is his son in reality, and there is no clear sound shar`i evidence to indicate that he should not be attributed to him.
With regard to the Hadith, “The child is to be attributed to the bed (i.e., to the husband) and the adulterer deserves nothing”, it refers to the case where there is a marriage, and the issue under discussion here is that where there is no marriage.
This is supported by what is mentioned in the story of the devoted worshipper Jurayj, when he spoke to the child whose mother had committed zina with the shepherd, “He said, ‘Who is your father, O child?’ The child said, ‘The shepherd.’” (Al-Bukhari and Muslim)
The speech of this infant was by way of a miracle and extraordinary event bestowed by Allah, and he stated that the shepherd was his father, even though the relationship was one of zina. This proves that paternity may be attributed to the zani.
Moreover, the Lawgiver seeks to protect lineages and take care of children, and to give them the best upbringing and protect them from being lost.
The view stating that it is forbidden or permissible (to attribute an illegitimate child to his father) are two scholarly views that carry weight. This issue is one of the matters that are open to ijtihad, so every case should be examined on its own merits.
If the child misses out on some religious or worldly interests (by not being attributed to his father), then we should adopt the view of permissibility to attribute him to his father, in the interest of protecting him and ensuring that he will be taken care of, which is a legitimate interest.
Allah Almighty knows best.