The correct option is
B To work hard if they wanted to get success
The given sentence is an advice.
In an imperative
sentence, the verb of the reported speech takes 'to' i.e. to go, to do,
etc. In the negative sentence the infinitive (to+ verb) is preceded by
'not'.
Infinitive always takes the basic form of the verb preceded by to. So, 'work' will change into 'to work'. The second person pronoun 'you' will change into third person plural 'they' in the reported speech.
Thus, the complete sentence must be,
The teacher told the boys to work hard if they wanted to get success.
Thus option B is the correct answer.
Options A,C,D are incorrect as they do not follow the rules of Narration and Reporting.