He may have had a strategy, but Rudolph W. Giuliani hatched it almost entirely in secret.

The White House counsel had no idea. Neither did the White House chief of staff, nor the White House press secretary, nor the new White House lawyer overseeing its handling of the Russia investigation.

They watched, agog, as Giuliani, the president’s recently installed personal attorney, freestyled on live television Wednesday night about the president’s legal troubles and unveiled an explosive new fact: that Trump reimbursed his longtime personal attorney Michael Cohen for the $130,000 paid to adult-film actress Stormy Daniels to ensure her silence about an alleged sexual encounter with Trump.

Giuliani’s attempt to defuse a ticking time bomb exposed Trump’s failure to divulge the full story about the Daniels hush money and highlighted contradictory public statements from him and White House spokesmen. One month ago, Trump told reporters that he did not know about the payment to Daniels, whose real name is Stephanie Clifford, or where Cohen got the money to make it.

Aides and advisers to the president — who were scrambling Thursday morning to manage the fallout of Giuliani’s interview with Sean Hannity, a Trump-friendly Fox News Channel host who also has been a Cohen client — expressed a mixture of exasperation and horror. One White House official texted a reporter a string of emoji characters in response, including a tiny container of popcorn.

A second White House official, who like most others interviewed for this article spoke on the condition of anonymity to discuss a sensitive topic, said of the president, “His story is obviously not consistent anymore.”

The episode was just the latest convulsion for a White House that perpetually navigates turbulence, careening from one crisis to another, most of them of the president’s own making. It has become standard operating procedure for Trump and his aides to deceive the public with false statements and shifting accounts.

“It’s about time that our public officials started telling us the truth,” said one former Trump adviser who remains close to the White House. “There is nobody in America who didn’t think the president had the affair with the porn star. I doubt there’s anybody in America who didn’t think the president had Michael Cohen pay off the porn star.”

In this case, Giuliani said he was trying to solve one problem for Trump — by establishing that the payment to Daniels came from personal funds and was “funneled” through a law firm, arguing it therefore did not violate campaign finance laws.

Giuliani said in an interview with The Washington Post that he discussed the issue with Trump a few days ago and that they agreed that he would reveal details about the reimbursement.

How the Trump team's Stormy Daniels story shifted again and again

“He was well aware that at some point when I saw the opportunity, I was going to get this over with,” Giuliani said.

Asked whether he might be fired for what he told Hannity, Giuliani replied, “No, no, no! I’m not going to get fired.” Laughing, he added: “But if I do, I do. It wouldn’t be the first time it ever happened. But I don’t think so, no.”

Tony Carbonetti, a longtime friend and adviser to Giuliani, said he was dining with the former New York mayor on Manhattan’s Upper East Side on Wednesday night before he went on Fox.

He warned that those lampooning Giuliani are mistaken.

“If you’ve been around Rudy, there’s always a reason for it,” he said. “If you knew a narrative was coming out, wouldn’t you want to tell the story on your terms? . . . He wanted to get ahead of it.”

Outside the government, Trump’s band of informal advisers and alumni cheered Giuliani’s move.

“I loved it,” said Jason Miller, a former Trump campaign official who also has worked for Giuliani. “They got this news out there on their terms, and they didn’t wait around for enterprising journalists to break it. This is PR 101. . . . The president deserves his own team defending him, and now finally he has it.”

'Cleaning this mess up'

In a trio of tweets Thursday morning, Trump attempted to do some damage control, writing in a notably restrained style — complete with honorifics, although marred by one misspelling — that Cohen had received a monthly retainer that did not come from the campaign and insisted that no campaign finance laws had been violated.

Aides speculated that the tweets may have been drafted by members of the president’s legal team, noting that they did not seem to be written in Trump’s singular Twitter voice.

Sam Nunberg, a former political adviser to Trump, said that the president’s “typical knee-jerk reaction to everything is deny, deny, deny” but that his lawyers have impressed upon him the importance of being truthful.

“The president has to understand that what he did in the private sector, as a mogul, celebrity and entrepreneur, where you can simply deny things in the press — when you’re under federal investigation, the FBI and Justice Department takes everything that is said publicly literally,” Nunberg said. “So I think they’re cleaning this mess up.”

The Cohen payment disclosure was not the only problematic comment from Giuliani in his wide-ranging interview with Hannity. He offered a reason for Trump firing FBI Director James B. Comey — because he would not publicly state that Trump was not under FBI investigation — that differed from the one provided by the administration at the time of Comey’s firing last May.

In addition, Giuliani said scrutiny from special counsel Robert S. Mueller III toward Ivanka Trump, the president’s daughter, was inappropriate because she is “a fine lady.” But Giuliani said it would be acceptable for Mueller to scrutinize her husband, Jared Kushner, because Kushner was “disposable,” as he jokingly put it. Both Ivanka Trump and Kushner are senior White House advisers.

Privately, some in Trump’s orbit were skeptical about the hiring of Giuliani, wondering if the combative and colorful former prosecutor who is at home sparring with reporters was the right choice to lead a white-collar defense team.

Andrew Kirtzman, a Giuliani biographer, said he did not see any “strategic sense” to Giuliani’s comments. “While he has always been bombastic, there has always been a logic to that bombast,” Kirtzman said. “I don’t see the logic here.”

But if Trump was upset with his lawyer’s performance, he did not show it. Indeed, the president was party to hatching the strategy, according to three people involved in the discussions. In recent weeks — in phone calls, dinners and private Oval Office huddles — Giuliani and Trump have talked through the thicket of legal issues facing the president beyond the Russia investigation, these people said.

Giuliani at first was reluctant to join Trump’s legal team because he preferred to offer informal advice, but the president pushed him to sign on formally because of the complexity of personal matters he was confronting, the people said.

Once on board, Giuliani spent days reading and being briefed about the many issues facing Trump, including the Cohen case and the payment made by Cohen to Daniels. Giuliani and Trump decided that it would be best to try to explain why Trump and Cohen acted as they did and clarify their business relationship, the people said.

“Giuliani is going to be dealing with all the existential threats to the Trump presidency,” said Douglas Brinkley, a presidential historian at Rice University. “If he gets Trump out of this jam, he becomes the indispensable man.”

Giuliani, in particular, viewed potential federal scrutiny of election law as an issue that Trump had to address head-on rather than dodge because he believed that it could fester in the coming months, according to one person who spoke with him last month. This person added that Trump was prone to trust Giuliani’s position because they had bonded privately over their shared frustrations with the Justice Department, and both men wanted to run a more combative, freewheeling media campaign.

“Giuliani’s value to Trump is not just that he’s got gravitas in the room with Mueller,” said Kirtzman, author of “Rudy Giuliani: Emperor of the City.” “It’s not just that he is a television celebrity. He’s a confidant. Trump needs a confidant more than ever. He hasn’t been able to find a confidant among the lawyers he hired. The question is whether Giuliani winds him up or calms him down.”

Out of the loop

Cohen’s attorney, Stephen M. Ryan, has been aware of Trump’s repayment for several weeks, possibly months, according to a person familiar with Cohen’s account. Cohen was averse to sharing this information publicly because he didn’t want to appear to be contradicting Trump’s denial in early April that he knew about the payment.

But Trump’s sensitivity over the Daniels matter led Giuliani to be one of the only people fully aware of the plan for the former mayor to attempt to swat away the matter on Fox News, a White House official said, leaving many other aides bewildered about what could come next.

“Rudy and Trump are talking by phone, and others aren’t in the loop,” one senior White House official said.

Neither White House counsel Donald McGahn nor Emmet Flood, the White House attorney recently hired to handle the Russia investigation, knew that Trump had reimbursed Cohen before Giuliani revealed it, according to a person familiar with their knowledge.

McGahn and Flood also were not informed in advance of Giuliani’s plan to disclose the repayment information in his Fox interview, nor were other senior aides in the White House. The communications and media staff run by press secretary Sarah Huckabee Sanders did not book Giuliani’s appearance on Hannity’s show and were not involved in helping him strategize his talking points.

Asked about White House officials being caught off guard by his disclosure, Giuliani told CNN on Thursday: “They were. There was no way they wouldn’t be. The president is my client. I don’t talk to them.”

David Axelrod, a senior White House adviser under President Barack Obama, said, “It is beyond bizarre to think that one set of lawyers who are poised to defend him should hear from another of his lawyers on TV plainly material facts about his case and his own conduct. This is the reason so many lawyers apparently refused the assignment.”

Surrounded by reporters Thursday morning outside the West Wing, Sanders declined to comment beyond what Giuliani and Trump had said earlier, citing “ongoing litigation.” Later, at the daily press briefing, Sanders referred questions to Giuliani.

“I haven’t had that conversation with the president,” Sanders said, again and again. “I’d refer you back to the president’s outside counsel.”

Carol D. Leonnig and Rosalind S. Helderman contributed to this report.