Where Do I Put This Code?

Where Do I Put This Code?

IntroductionImportant!CSSJavascript/jQueryPHPThird Party Plug-Ins

Introduction

Depending on what code you are trying to add, and the complexity of the theme you are using, there can be a number of different places that code snippets can be added to extend your forms. This article covers most of the common options.

Important!

Replacing or updating your theme can overwrite the theme directory in your site folder, and that means modifications like these you may have made to files there will be lost. Always keep regular back-ups, consider the use of a child theme, and check out some of the third party plug-ins that provide safer alternatives for adding code.

CSS

CSS code will typically look something like this:

body .gform_wrapper { border:1px solid red }

In most cases, custom CSS should be pasted at the bottom of your theme’s style.css file. Your theme may have a custom.css. If so, pasting your custom styles at the very end of that file is preferable. Generally, when a theme includes any type of file named custom it indicates that this file should be used for custom code specific to the file type.

Javascript/jQuery

Javascript code will typically look something like this:

jQuery(document).ready(function($) {
$(‘.gfield_checkbox input’).click(function(){
var is_checked = $(this).is(‘:checked’);
var parent = $(this).parent(‘li’);
if(is_checked){
$(parent).addClass(‘selected’);
} else {
$(parent).removeClass(‘selected’);
}
});
});

Community Library Alternative The team at Gravity Wiz have a free Custom JavaScript add-on which you can use to include custom scripts for forms.(see disclaimer at bottom of article)

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

Additional Information on using JavaScript with WordPress can be found here: https://codex.wordpress.org/Using_Javascript

PHP

PHP code will typically look something like this:

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.

Your functions.php will already have an opening

上次修改 2021.12.28