简体   繁体   中英

Wordpress Functions Theme options formatting

I have created a wordpress theme and have created a load of options for the end users to alter certain areas of the site.

I am hving issues with a text area that is to deal with the users google analytics code.

The text area works but the output of the analytics code means it doesnt work.

In my functions.php file I have a text area for the user to entere their analytics code:

array (
                    'key' => 'field_52e7a858b4951',
                    'label' => __("Tracking code", 'dansiop'),
                    'name' => 'google_analytics',
                    'type' => 'textarea',
                    'instructions' => __("Copy and paste your Google Analytics tracking code here. You can create a free account at: http://www.google.com/analytics/", 'storini'),
                    'default_value' => '',
                    'placeholder' => '',
                    'maxlength' => '',
                    'formatting' => 'none',
                ),

No in my admin I have the text area as required:

[unable to post images due to being new on here] http://www.dansiop.com/analytics.png

So when the user enters their specific analytics code such as this:

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'XX-XXXXXXXX-X', 'auto');
  ga('send', 'pageview');

</script>

The code appears on the website in the footer. It is placed in the correct place but when I view source I see the Analytics code like this:

&lt;script&gt;
  (function(i,s,o,g,r,a,m){i[&#039;GoogleAnalyticsObject&#039;]=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,&#039;script&#039;,&#039;//www.google-analytics.com/analytics.js&#039;,&#039;ga&#039;);

  ga(&#039;create&#039;, &#039;XX-XXXXXXXX-X&#039;, &#039;auto&#039;);
  ga(&#039;send&#039;, &#039;pageview&#039;);

&lt;/script&gt;

I have masked the specific ID in the analytics code for obvious reasons.

Anyone have any ideas as to why this is happening?

Would greatly appreciate some help and can't find anything online regarding this matter.

EDIT: I am echoing the code in footer above /body with this:

<?php if( get_field('google_analytics', 'option') ) : ?><?php the_field('google_analytics', 'option'); ?><?php endif ?>

why don't you try to use html_entity_decode()

You should go to the footer and echo/print code using html_entity_decode('Your-analytic-code-variable');

<?php 
if( get_field('google_analytics', 'option') ) :
    echo html_entity_decode(get_field('google_analytics', 'option'), ENT_QUOTES, "ISO-8859-1");
endif 
?>

如果您打印或var_dump字段,该值是否已转义还是仍保留其未格式化的值?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM