Search
 
SCRIPT & CODE EXAMPLE
 

PHP

how to add javascript in php

//this is my code 

you can add javascript to your code by using the echo statement and rap all 
your javascript between "" and if there is "" in the javascript code
then add  before any "", and for the php inside rap it with ''
  
  
<?php
  echo "<script>document.write("hello world")</script>"
?>
  
//other example

<?php
  $a = 5;
  echo "<script>document.write('$a')</script>"
?>

//other example
  
<?php 
$TokenData = Auth::user();

echo "<script type="text/javascript">
             localStorage.setItem("id", '$TokenData->id');
             localStorage.setItem("name", '$TokenData->name');
             localStorage.setItem("email", '$TokenData->email');
      </script> ";
Comment

add javascript to functions.php

/****
  To get started, locate and open your functions.php file, 
  then copy and paste the following code snippet:
****/
function ti_custom_javascript() {
    ?>
        <script>
          // your javscript code goes here
        </script>
    <?php
}
add_action('wp_head', 'ti_custom_javascript');

/****
This code above will add JavaScript to your header. 
To apply it to a single post only, you can use the following:
****/
function ti_custom_javascript() {
  if (is_single ('3')) { 
    ?>
        <script type="text/javascript">
          // your javscript code goes here
        </script>
    <?php
  }
}
add_action('wp_head', 'ti_custom_javascript');
/****
Note that you’ll need to replace the 3 
in the above example with the post ID number 
to which you want to add the code. To locate 
that number, navigate to the post from your 
admin dashboard, and then click on Edit. In 
the browser bar’s URL, the ID number is the 
number next to “post=”:
Comment

PREVIOUS NEXT
Code Example
Php :: merge two objects php laravel 
Php :: laravel @disabled in laravel-9 
Php :: list() php 
Php :: what does defined do in php 
Php :: laravel routes not working on production server 
Php :: Termlaravel validation exists array rules 
Php :: laravel search 
Php :: move_uploaded_file 
Php :: wordpress custom post type query 
Php :: while true php 
Php :: laravel Please provide a valid cache path 
Php :: acf create post with fields 
Php :: laravel model withCount relationship condition 
Php :: php unique associative nested array by value 
Php :: laravel count distance lat/longtidue 
Php :: Remove revisions from Wordpress pages 
Php :: laravel start que listener 
Php :: php explode end 
Php :: how to get meta information from pagination in laravel controller 
Php :: laravel delete multiple rows 
Php :: php last item of array 
Php :: Composer detected issues 
Php :: php laravel dump 
Php :: laravel search multiple tables 
Php :: laravel collection forget 
Php :: laravel blade fontawesome 
Php :: Multiple image upload with CodeIgniter 
Php :: aws sdk php 
Php :: IlluminateContractsContainerBindingResolutionException 
Php :: wordpress use jquery in plugin 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =