NIGERIAN PRESIDENT Goodluck Jonathan is expected to seek reelection next year despite opposition from northern Nigeria’s majority Muslim states. His attempt to win them over has now taken a toxic turn: sanction of an extraordinarily repressive new law against homosexuality.

Gay sex has been banned in Nigeria since British colonial times. It is prohibited in 38 of Africa’s 54 countries. But the new Ni­ger­ian law, which Mr. Jonathan signed Jan. 7, goes further. It mandates a 14-year prison sentence for anyone entering a same-sex union and a 10-year term for “a person or group of persons who supports the registration, operation and sustenance of gay clubs, societies, organizations, processions or meetings.” Public displays of affection by gay men and lesbians are also criminalized.

The law has triggered a wave of anti-gay violence in parts of Nigeria. According to the Associated Press, police in the northern state of Bauchi arrested 38 men beginning in late December after the law obtained final approval in Congress. Several men were allegedly tortured into naming gays they knew. The New York Times described Sunday a man who was publicly whipped for gay sex and said a mob tried to attack other gays who were brought into court.

One of the Islamic sharia courts operating in northern Nigeria administered the whipping. Both they and the new law appear to violate Nigeria’s constitution, which, as Secretary of State John F. Kerry has pointed out, guarantees freedom of assembly. Nigeria is also in contravention of international treaties it has ratified : As United Nations human rights chief Navi Pillay put it, “Rarely have I seen a piece of legislation that in so few paragraphs directly violates so many basic, universal human rights.”

Sadly, gay men and lesbians are easy targets for demagogues in Africa, where large majorities still hold the bigoted attitudes until recently prevalent in the West. Pressure from the international community is needed as a countervailing force. How that can work was evident recently in Uganda, a country dependent on Western aid. President Yoweri Museveni vetoed a law threatening gays with life imprisonment even as he described them as the product of “random breeding” when “nature goes wrong.”

Nigeria, the most populous country in Africa and a major oil producer, is harder to influence. But Britain still delivers hundreds of millions of dollars in development aid, while the United States buys 70 percent of Nigeria’s oil. Both should be aggressively using their leverage to protect the vulnerable gay community. As a starting point, they should let Mr. Jonathan know he and his government will be unwelcome in Washington and London until the law is repealed.