WASHINGTON—President Donald Trump may not block critics from reading his Twitter feed, a federal judge ruled on Wednesday, finding that doing so violates their right to free speech.

Judge Naomi Reice Buchwald of the Southern District of New York ruled that the president’s blocking of Twitter users from viewing his feed based on their political speech “constitutes viewpoint discrimination that violates the First Amendment.” She termed Mr. Trump’s Twitter feed, on which he issues tweets multiple times a day on a variety of issues, a “designated public forum.”

In July 2017, Columbia University’s Knight First Amendment Institute filed a lawsuit alleging the president’s habit of blocking users who “disagreed with, criticized or mocked” him violated the free-speech protections of the First Amendment to the U.S. Constitution.

The judge’s ruling named Mr. Trump and Dan Scavino, the White House social media director, but she said it applied to public officials more broadly.

“This case requires us to consider whether a public official may, consistent with the First Amendment, ‘block’ a person from his Twitter account in response to the political views that person has expressed, and whether the analysis differs because that public official is the President of the United States,” Judge Buchwald wrote. “The answer to both questions is no.”