As early as mid-1915, Spain was seeking to reassert its place as a major world power. This included a secret plan known as the Caribbean Stratagem, a plan to retake Cuba and potentially conduct hit-and-run operations on Florida. The plans were abandoned when tensions in Europe required Spain's undivided attention.
In 1916, France declared war on Spain for unknown reasons (Spain and Germany, though, suspected that it was a war of aggression to keep Spain in line). Britain and Siam also declared war on Spain. The Franco-Spanish War of 1917 ended with the Treaty of Le Havre, which forced Spain to give up all of its colonial possessions in Africa to France. This left the Spanish people embittered and seeking revenge.
In 1918, Spain joined the Central Powers (which, at the time, was composed of Germany, the Ottoman Empire, and Brazil), which was followed later in the year by the emergence of the Spanish flu and a massive build-up of Spain's military in anticipation of war with France.
Finally, in mid 1919, the First Great War began. Shortly beforehand, Spain moved troops, planes, and ships via the Panama Canal to the Pacific in order to protect German colonial holdings from China, which it did. When the First Great War began, Spain declared war on France in 1920 and immediately steamrolled into France. France eventually surrendered in 1922, and the Treaty of Versailles was signed, giving Spain back all of its colonial holdings and many other French colonial holdings in Africa, as well as Southern France.
After King Alfonso XIII was killed in a terrorist bombing in 1924 and King Jaime I was coronated, Spain declared war on Serbia in 1926 to assist its Austrian allies.