Title: Javascript wrapped in php
Last modified: July 5, 2019

---

# Javascript wrapped in php

 *  [snazzydray](https://wordpress.org/support/users/snazzydray/)
 * (@snazzydray)
 * [6 years, 11 months ago](https://wordpress.org/support/topic/javascript-wrapped-in-php/)
 * Hi,
 * I’m having a lot of trouble running this script in a PHP snippet (and variations
   of it, tried everything i could think of)
 * could anyone help me ? (get_field is from ACF)
 * add_action(‘init’,’fn_concours’);
    function fn_concours() { if (in_array(‘Oui’,
   get_field(‘concours_actif’))){ echo ‘<script type=”text/javascript”>’; echo ‘
   $(document).ready(function(){ $(“.menu-item-4336 a”).css(“background-color”,”#
   cc0000 !important”); $(“.menu-item-4336 a”).css(“color”,”#fff !important”); });’;
   echo ‘</script>’; } }
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fjavascript-wrapped-in-php%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [Temyk](https://wordpress.org/support/users/webtemyk/)
 * (@webtemyk)
 * [6 years, 10 months ago](https://wordpress.org/support/topic/javascript-wrapped-in-php/#post-11709184)
 * Hi,
 * Why do you use `add_action('init','fn_concours');`?
 * This code works fine via shortcode in the post and when you run everywhere:
 *     ```
       if (get_field(‘concours_actif’) && in_array(‘Oui’,get_field(‘concours_actif’))){
       echo ‘<script type=”text/javascript”>’;
       echo ‘$(document).ready(function(){
       $(“.menu-item-4336 a”).css(“background-color”,”#cc0000 !important”);
       $(“.menu-item-4336 a”).css(“color”,”#fff !important”);
       });’;
       echo ‘</script>’;
       }
       ```
   
    -  This reply was modified 6 years, 10 months ago by [Temyk](https://wordpress.org/support/users/webtemyk/).
 *  Thread Starter [snazzydray](https://wordpress.org/support/users/snazzydray/)
 * (@snazzydray)
 * [6 years, 10 months ago](https://wordpress.org/support/topic/javascript-wrapped-in-php/#post-11711344)
 * Thanks for the reply !
 * however, when I try your code, the site breaks…
 * going crazy with this code !
 *  [Temyk](https://wordpress.org/support/users/webtemyk/)
 * (@webtemyk)
 * [6 years, 10 months ago](https://wordpress.org/support/topic/javascript-wrapped-in-php/#post-11712881)
 * Excuse me. Why is there quotation marks are replaced by strange characters. Here
   is the code with normal quotes
 *     ```
       if (get_field('concours_actif') && in_array('Oui',get_field('concours_actif'))){
       echo '<script type="text/javascript">';
       echo '$(document).ready(function(){
       $(".menu-item-4336 a").css("background-color","#cc0000 !important");
       $(".menu-item-4336 a").css("color","#fff !important");
       });';
       echo '</script>';
       }
       ```
   

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Javascript wrapped in php’ is closed to new replies.

 * ![](https://ps.w.org/insert-php/assets/icon-256x256.gif?rev=3523853)
 * [Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts](https://wordpress.org/plugins/insert-php/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/insert-php/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/insert-php/)
 * [Active Topics](https://wordpress.org/support/plugin/insert-php/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/insert-php/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/insert-php/reviews/)

## Tags

 * [ACF](https://wordpress.org/support/topic-tag/acf/)
 * [javascript](https://wordpress.org/support/topic-tag/javascript/)

 * 3 replies
 * 2 participants
 * Last reply from: [Temyk](https://wordpress.org/support/users/webtemyk/)
 * Last activity: [6 years, 10 months ago](https://wordpress.org/support/topic/javascript-wrapped-in-php/#post-11712881)
 * Status: not resolved