NEW YORK — Donald Trump was held in contempt of court Tuesday and fined $9,000 for repeatedly violating a gag order that barred him from making public statements about witnesses, jurors and some others connected to his New York hush money case. If he does it again, the judge warned, he could be jailed.

Prosecutors alleged 10 violations, but New York Judge Juan Merchan found there were nine. Trump stared down at the table frowning slightly as the judge read the ruling.

It was a stinging rebuke of the presumptive Republican presidential nominee’s insistence that he was exercising his free speech rights and a reminder that he’s a criminal defendant subject to the harsh realities of trial procedure. And the judge’s remarkable threat to jail a former president signaled that Trump’s already precarious legal standing could further spiral depending on his behavior during the remainder of the trial.

Merchan wrote that he is “keenly aware of, and protective of,” Trump’s First Amendment rights, “particularly given his candidacy for the office of President of the United States.”

“It is critically important that defendant’s legitimate free speech rights not be curtailed, that he be able to fully campaign for the office which he seeks and that he be able to respond and defend himself against political attacks,” Merchan wrote.

Still, he warned that the court would not tolerate “willful violations of its lawful orders and that if necessary and appropriate under the circumstances, it will impose an incarceratory punishment.”

“This gag order is totally unconstitutional,” Trump said as court adjourned.

Tuesday’s ruling came at the start of the second week of testimony in the historic case, which involves allegations that Trump and his associates took part in an illegal scheme to influence the 2016 presidential campaign by purchasing and then burying seamy stories.

The payouts went to a doorman with a torrid yarn; ex-Playboy model Karen McDougal, who had accusations of an affair; and to porn performer Stormy Daniels, who alleged a sexual encounter with Trump. He has pleaded not guilty and says the stories are all fake.

Trump deleted, as ordered, the offending posts from his Truth Social account and campaign website and has until Friday to pay the fine. The judge was also weighing other alleged gag-order violations by Trump and will hear arguments Thursday.

He also announced that he will halt the trial on May 17 to allow Trump to attend his son Barron’s high school graduation.

Testimony resumed Tuesday with a banker who helped Cohen open accounts, including one used to buy Daniels’ silence.

Trump’s attorneys have suggested that the payments were aimed at protecting his name and his family — not influencing the outcome of the presidential election.

Jurors also began hearing from Keith Davidson, a lawyer who represented McDougal and Daniels in their negotiations with the National Enquirer and Cohen. He testified that he arranged a meeting at his Los Angeles office during the summer of 2016 to see whether the tabloid’s parent company was interested in McDougal’s story. At first they demurred, saying she “lacked documentary evidence of the interaction,”

Davidson testified.

But the tabloid at the behest of publisher David Pecker eventually bought the rights, and Davidson testified that he understood — and McDougal preferred — it would never be published. Asked why the tabloid’s parent company, American Media Inc., would buy a story it didn’t intend to run, Davidson said he was aware of two reasons.

“One explanation I was given is they were trying to build Karen into a brand and didn’t want to diminish her brand,” he said.

“And the second was an unspoken understanding that there was an affiliation between David Pecker and Donald Trump and that AMI wouldn’t run this story, any story related to Karen, because it would hurt Donald Trump.”

As for Daniels, the October 2016 leak of Trump’s 2005 “Access Hollywood” tape — in which Trump bragged about grabbing women sexually without asking permission — had “tremendous influence” on the marketability of her story. Before the video was made public, “there was very little if any interest” in her claims, Davidson told jurors.

A deal was reached with the tabloid for $120,000 for Daniels story, but the Enquirer backed out.

Though Pecker testified that he had agreed to serve as the Trump campaign’s “eyes and ears” by helping to squelch unflattering rumors and claims about Trump and women, he drew the line with Daniels after paying out $180,000 to scoop up and sit on stories.

Davidson began negotiating with Cohen directly, hiked up the price to $130,000, and reached a deal.

But Daniels and Davidson grew frustrated as weeks passed and instead of the money, she got excuses from Cohen about broken computers, Secret Service “firewalls” and the Jewish holiday of Yom Kippur.

“I thought he was trying to kick the can down the road until after the election,”

Davidson said.

While Cohen never explicitly said he was negotiating the deal on Trump’s behalf, Davidson felt the implication was clear.

“Every single time I talked to Michael Cohen, he leaned on his close affiliation with Donald Trump,” Davidson said. Plus, he figured that Trump “was the beneficiary of this contract.”