WASHINGTON — A Republican congressman from Staten Island is walking a legislative tightrope between the Trump administration and his constituents, proposing legislation to hit sanctuary cities with financial penalties — but not target funding to the NYPD.

Rep. Dan Donovan (R-S.I.), the borough’s former district attorney, plans to introduce legislation this week that would cut off federal funding to sanctuary cities like New York, tied to one immigration-related grant, after previously declining to back sanctuary city measures pushed by the president.

Donovan said he’s always opposed sanctuary cities policies but couldn’t support efforts that endanger federal funding for the NYPD‘s anti-terror and homeland security efforts.

“Our government’s greatest responsibility is to protect the American people, and sanctuary city policies prevent us from fulfilling that duty,” Donovan said in a statement.

He added: “It’s common sense that those unwilling to cooperate with federal authorities not be eligible to receive certain federal grant funding. The message this bill sends is clear: no person, state, or locality is above the law.”

Under his bill, cities that fail to honor federal immigration detainer requests would be penalized by not getting State Criminal Alien Assistance Program grant dollars, which reimburse correctional officers’ salaries for costs associated with jailing undocumented criminal aliens.

In 2016, the feds doled out $188 million in the program, including about $30 million to New York state and $10.5 million to New York City, according to Donovan’s office.

Donovan’s legislation comes as he’s locked in a tough primary fight with former Rep. Michael Grimm, who has hit Donovan as not being tough enough on illegal immigration. Grimm called the new legislation a stunt to cover up his “liberal” track record.

“Desperate Dan is now making it clear he’ll do anything and say anything in a pathetic gimmick to try and fool the voters,” Grimm said in a statement.

But Donovan believes his legislation offers a legal alternative in the face of a federal court ruling in California this month that found the Justice Department cannot require that local police departments help immigration agents in order to receive funds.

The Trump administration had been threatening to take away police grants, including the Edward Byrne Memorial Justice grant, named for the NYPD cop killed in the line of duty in 1988. Donovan believes targeting the corrections officer grant program is more appropriate, since it deals directly with immigration detention costs.

Democratic Mayor Bill de Blasio has blasted the Trump administration’s attempts to take away NYPD funding and pledged a court fight if Trump tries to take back anti-terrorism funding.

The mayor backed out of a planned White House meeting with Trump and other mayors in January over Trump’s threats to punish so-called sanctuary cities. Trump accused the boycotting mayors of putting “the needs of criminal illegal immigrants over law-abiding Americans.”