3 Sept 2013

Placing JavaScript , PHP codes

Javascript/jQuery 

Javascript code will typically look something like this:
01jQuery(document).ready(function($) {
02
03    $('.gfield_checkbox input').click(function(){
04        var is_checked = $(this).is(':checked');
05        var parent = $(this).parent('li');
06
07        if(is_checked)
08            $(parent).addClass('selected');
09        else
10            $(parent).removeClass('selected');
11
12    });
13
14});
Some theme's will provide an init.js or, even better, a custom.js file. If so, pasting the custom Javascript in either of these files would be ideal. If neither is present, you can wrap your Javascript code in a script block and paste the code in your theme'sheader.php file below the wp_head() function call .
view sourceprint?
1script type="text/javascript"
2    jQuery(document).ready(function($) {
3
4        // custom code
5
6    });
7/script

PHP 

PHP code will typically look something like this:
01function gform_add_post_format($entry) {
02     
03    $post_id = rgar($entry'post_id');
04     
05    if(!$post_id)
06        return;
07     
08    wp_set_object_terms($post_id'chat''post_format');
09     
10}
PHP snippets will almost always be pasted in your theme's functions.php file. A few things to be aware of when adding PHP snippets.
  1. Your functions.php will already have an opening  tag. If the snippet you are copying also has an opening PHP tag, be sure to remove it before pasting it into your functions.php file.
  2. If the PHP snippet you are copying has opening and closing PHP tags inside the code itself, do not remove these. This simply means that the code is exiting "PHP mode" to output content directly to the screen.

No comments:

Post a Comment