I'm trying to make a post display some PHP code example. Using WordPress 5.2.1 with PHP 7.3

The website works fine otherwise. However, when I'm editing a post, select "Formating - Code" and enter some PHP code within the block, it won't save the updated post version.

"updating failed".

Has anyone else had this problem? Tried disabling WordFence - didn't help.

So it's either the (shared) hosting provider, or something with the WordPress.

EDIT: The "problematic" code example:

// BEGIN Google Analytics

function ns_google_analytics() { ?>
   <script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX-X"></script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'UA-XXXXXXXXX-X');

add_action( 'wp_head', 'ns_google_analytics', 10 );

// END Google Analytics

To avoid misunderstanding: My attempt is/was to display the code in a post, like it is displayed here. When implementing it on a website, this goes into child theme's functions.php. But the post I'm trying to write is a tutorial one and should display the code sample so it can be easily copied by any visitor. The code (as shown) is not supposed to run on that post.

2 Answers 2


You can't put PHP code in a post, without the use of plugins that allow that. You might be able to put a <code></code> tag around the code in a text editor, but that might depend on your theme.

But the code in your question looks like it belongs in the functions.php of your theme (preferably a Child Theme, since you don't want to change the theme code). When the code is in the functions.php file, it gets executed on every page, and will do what you want. (It's not clear from your question where you put the code block.)

  • Edited the original post to explain better what I was trying to do. Got it working (as explained in my answer). Jun 5, 2019 at 6:00

OK, to answer my own question - did the simplest thing:

Migrated the website to another hosting provider. It all works there. Migrated back - it doesn't work.

So it's most probably something to do with provider's setup, not with the WordPress, or plugins.

EDIT: According to the provider's technical support, it was a ModSecurity rule. They managed to fix it. Didn't have this problem with every hosting provider.

More details on the resolution: https://io.bikegremlin.com/9641/hostmantis-review/#12

Not the answer you're looking for? Browse other questions tagged or ask your own question.