NEW YORK—Michael Cohen, the president’s former personal lawyer, told a federal judge that Donald Trump had directed him during the 2016 campaign to buy the silence of two women who said they had affairs with Mr. Trump.

Mr. Cohen made the statement as he pleaded guilty Tuesday to eight criminal charges, including campaign-finance violations. He said he paid $130,000 to adult-film actress Stephanie Clifford, known professionally as Stormy Daniels, and coordinated a $150,000 payment by the publisher of the National Enquirer to former Playboy model Karen McDougal.

Mr. Cohen, who has described himself as Mr. Trump’s “fixer,” said he made both payments “for the purpose of influencing the election” and acted at the direction of “the candidate,” referring to Mr. Trump. That was the first time Mr. Cohen has admitted to coordinating with the president on the hush-money deals with women, both of which were first reported by The Wall Street Journal.

The surprising admission by Mr. Cohen directly implicated Mr. Trump in a federal crime, escalating pressure on the president. Three other Trump associates have been charged with felonies and a fourth was convicted by a jury on Tuesday.

Mr. Trump and his representatives previously denied that he knew about the payments at the time they were made.