最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Adding a LISTING of PHP code in a pagepost

programmeradmin3浏览0评论
Closed. This question is off-topic. It is not currently accepting answers.

Questions that are too localized (such as syntax errors, code with restricted access, hacked sites, hosting or support issues) are not in scope. See how do I ask a good question?

Closed 5 years ago.

Improve this question

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=""></script>
   <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'UA-XXXXXXXXX-X');
   </script>
   <?php
}

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.

Closed. This question is off-topic. It is not currently accepting answers.

Questions that are too localized (such as syntax errors, code with restricted access, hacked sites, hosting or support issues) are not in scope. See how do I ask a good question?

Closed 5 years ago.

Improve this question

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/gtag/js?id=UA-XXXXXXXXX-X"></script>
   <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'UA-XXXXXXXXX-X');
   </script>
   <?php
}

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.

Share Improve this question edited Jun 5, 2019 at 5:59 bikegremlin asked Jun 4, 2019 at 14:09 bikegremlinbikegremlin 18610 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

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.)

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/9641/hostmantis-review/#12

发布评论

评论列表(0)

  1. 暂无评论