WordPress's editor rewrites or strips pasted code, and Author accounts cannot save scripts at all. Paste video addresses on their own line, and have your developer put forms and widgets in shortcodes or template slots.

Pasted, saved and gone

Someone in the office copies a few lines of code from a booking system's settings, opens a WordPress page, pastes the code in and presses Update.

The page shows nothing where the form should be, or a block of code as plain text, or an empty box. Back in the editor, half the code has gone.

The same happens with sign-up forms from mailing services, maps and videos. The code was fine; the editor changed it.

How the editor loses code

WordPress can change pasted code in several ways, and more than one may be at work:

The last is the most puzzling, because the form worked for weeks and broke when someone corrected a spelling mistake two paragraphs away.

Why WordPress does it

The visual editor is built for text: headings, paragraphs, links and images. To keep the page tidy, it rewrites anything it does not understand, and embed code is full of things it does not understand.

The restriction on scripts is deliberate. A script on a page runs in every visitor's browser, so an account that can save one could use it against visitors or other users of the site.

On a standard site, only Administrator and Editor accounts may save scripts. Authors and Contributors have them removed when they save, whichever tab they used.

The two tabs

Above the editing box are two tabs. Visual shows the page roughly as it will look; Text, called HTML in older versions, shows the markup itself.

Code pasted into the Text tab by an Editor or Administrator survives the first save. It is still exposed, because anyone who later opens the page in the Visual tab gives the editor a chance to rewrite it.

So pasting into the right tab is a workaround, not a fix. The code needs to live somewhere the editor never touches.

Videos have a simpler route

For YouTube and several other video sites, skip the embed code entirely. Paste the video's address on a line of its own, and WordPress turns it into a player when the page is shown.

The page stores only the address, so there is nothing for the editor to strip, and the player appears in the same place whatever tab anyone uses.

A shortcode instead of pasted code

A shortcode is a short tag in square brackets that staff type into a page, such as a word naming the booking form. When the page is shown, WordPress replaces the tag with the full code.

The code itself sits in a file your developer controls, outside the editor's reach. Staff can move the tag, edit the text around it and switch tabs freely.

When the booking system changes its code, the developer updates one file, and every page with the tag follows.

A slot in the template

Some embeds belong on many pages: a sign-up form in the sidebar, a booking button in the header. Those go in the theme's template or a widget area, placed once by the developer.

Template changes belong in a child theme, which keeps them safe when the parent theme is updated.

A worked example

A bike courier in downtown Victoria carries documents and small parcels for law offices, print shops and architects. Its dispatch system offers an online pickup form as a few lines of code.

The office manager, working from an Author account, pasted the code into the Book a Pickup page. After Update, the form was gone.

The company's founder pasted it again from an Administrator account, and it worked until a dispatcher fixed the hours on the same page in the Visual tab.

The developer created a shortcode for the pickup form, with the code kept in a small file of the site's own. The page now holds a single tag, and the hours can be edited in either tab.

The same form also went into the sidebar of the rates page through a widget area, so a customer reading prices can book without leaving the page.

Staff can now edit any page as they like, and the form survives, because nothing they touch contains it.

Who can add scripts

Keep Administrator and Editor accounts to the few people who need them. Staff who write and update pages rarely need the right to save scripts.

Embed code from outside services runs on your pages, in your visitors' browsers. Add it only from services you trust, and keep a short record of what is embedded where and who holds each login.

Remove embeds for services you no longer use. Old code can keep loading scripts from places no one checks any more.

The counter-case

Pasted code can last for years on a site where one Administrator edits every page in the Text tab and never switches to Visual.

Some embeds are simple links or images that the editor leaves alone, and those can stay in the page.

But once more than one person edits, or anyone uses the Visual tab, pasted code is fragile. A developer can set up a shortcode in short order, and the code then sits where no edit can reach it.

Moving embeds out of the editor's reach

  1. List every embed on the site and the page it sits on.
  2. Check each page still shows its form, widget or video.
  3. Replace video code with the video's address on its own line.
  4. Limit script rights to the accounts that need them.
  5. Ask your developer for a shortcode for each form or widget.
  6. Place site-wide items in a template slot or widget area.
  7. Tell everyone who edits to leave shortcodes as they are.

Step five ends the cycle of pasting and losing. Once the code lives in a file and the page holds only a short tag, anyone can edit the page in either tab without breaking the form.

A form that works can still send the same entry twice when a visitor presses refresh, a separate problem with its own fix, described in duplicate enquiries from visitors who press refresh.


Frequently asked questions

Why does WordPress remove code I paste into a page?

The visual editor rewrites markup it does not recognise, switching tabs changes more, and accounts below Editor are not allowed to save scripts at all, for security reasons.

Which WordPress accounts can save scripts?

On a standard single site, Administrators and Editors. Authors and Contributors have scripts and embedded frames removed when they save, whichever tab they used.

How should I add a YouTube video?

Paste the video's address on a line of its own in the editor. WordPress turns it into a player when the page is shown, so there is no code to lose.

What is a shortcode?

A short tag in square brackets that staff type into a page. When the page is shown, WordPress replaces it with code kept in a file the editor never touches.

Should I give staff Editor accounts so they can paste code?

That widens who can add scripts that run in every visitor's browser. Keep script rights to a few accounts and put embeds in shortcodes instead.

Where does a form that appears on every page belong?

In the theme's template or a widget area, placed once by the developer, so it is managed in one place and the editor cannot change it.

West Coast Media Solutions Inc. provides web design, web development, hosting, digital marketing, and business consulting to organisations across Canada, drawing on more than twenty-five years in the field.

A booking form that keeps vanishing?

Send us the page and the code you pasted, and we will set it up as a shortcode the editor leaves alone.

Start a Conversation