Examples of Using jQuery in Builder

Disclaimer

iThemes will not be responsible for any issues that may be the result of your attempts to change your site's functions.php file. You do not HAVE to change it. All code samples are for your information, possibly inspiration, and provided "as is". However, when properly implemented, the code samples are accurate and will work.

Warning

If you do not understand what it is you are doing, if the words "php", "opening tag", "FTP", "functions" and "backup" are not familiar to you proceed with caution. You can blow up your site due to invalid code in the functions.php file. Actually, even if you know or think you know it all, it still happens (it happens to me at least once a week - Ronald).

A single missing } or even a , or a semi-colon is all it takes to take your site down. This can be resolved by undoing the changes, however, this can not be done through the wp-dashboard > Appearance > Editor anymore (since your site is down). You then have to restore the functions.php file either through FTP or your hosting cPanel File Manager.

If you think you can not do that, or do not understand what all the above means, I suggest you refrain from editing functions.php.

Should anything go wrong, do not blame the code posted here. The code works. It just needs to be inserted a) the right way, and b) in the right place.

For everyone else feeling confident, and having read the Disclaimer and the paragraph on how to add PHP code to a PHP file, DO MAKE A BACKUP, at least of the file you are editing. It is also not recommended to do these changes through the wp-dashboard > Appearance > Editor. Setup a localhost server on your computer, and use a simple PHP Editor (with syntax checking), ensure that your additions do not break the site and only THEN FTP your files to your server.

How to add PHP code to a PHP file

When adding code to the functions.php file (or any PHP file), make sure it is in PHP format. HTML code is not PHP code, and it WILL break things when coded inside a block of PHP code.

PHP code can be identified by an opening tag: <?php
and a closing tag ?>

all code between these tags should be PHP code.

Most WordPress themes have the opening <?php tag in functions.php, coded all the way at the top of the file. Most (Builder Child theme) functions.php files do not have a closing ?> tag (ALL THE WAY) at the end of the file, since it is not required.

So if you add code at the end of your functions.php file, and do so before the closing ?> PHP tag (if any!) you (generally!) are inside a PHP block of code. But this is NOT guaranteed.

You can't add html code inside PHP tags.

You can't add PHP code outside PHP tags.

You can't add opening PHP tags INSIDE a block of PHP code (nesting <?php some php code ?> when there is no closing php tag before it).

Before making the final edits and saving and uploading the file to your server, make sure that the syntax of the entire functions.php is valid syntax. You can do so by using a PHP Editor, and there are online tools such as this one: http://www.piliapp.com/php-syntax-check/

example of correct PHP code

<?php
PHP code
?>
html code
<?php
some more PHP code
?>

example of incorrect PHP code 1

<?php
php code
<?php
some more php code
?>
?>

example of incorrect PHP code 2

<?php
html code
?>

example of incorrect PHP code 3

<?php
php code
?>
more php code

Introduction

Please note that code shown is for a particular child theme and for a specific version used when writing the articles. It would usually have to be modified to suit your needs. All code samples use certain selector names (for menus, for builder selectors etc.) This is entirely arbitrary, and it is highly unlikely that your selectors are the same. You have to adapt the code accordingly.

All code samples provided here ASSUME that it will be placed in already existing PHP tags. Therefore, you will not find an opening <?php code at the start, or a closing ?> tag at the end.

How to assign odd and even classes to menu items in nav bar

One typical usage of this would be to set different background colors to alternate menu items.

where menu-main-menu is the CSS ID of custom menu (usually <ul id="menu-main-menu">) shown in the nav bar. This is entirely arbitrary, and it is highly unlikely that your selectors are the same. You need to adapt the code accordingly.

Alternate Method:

Go to My Theme -> Settings -> Analytics and JavaScript Code. Paste the following in the text area under

"List any JavaScript or other code to be manually inserted inside the site's <head> tag in the input below."

How to Clear Placeholder Text Upon Focus in Gravity Forms Fields

Gravity Forms - Placeholders add-on plugin can be used to add HTML5 placeholder support to Gravity Forms' fields with a javascript fallback. gplaceholder CSS class should be added to text fields or textareas as needed, or to the form itself to enable this feature to all fields within it.

Ex.:

Now all Gravity Forms' field labels will appear inside the fields as placeholder text.

Before

After

The placeholder text will continue to appear when clicked or tabbed to inside a field, but will disappear when user starts typing.

If you would like the fields' placeholder text to be cleared when a field gets focus, add the following at the end of child theme's functions.php:

Easy Accordion jQuery plugin in iThemes Builder

Live Demo [Note: Due to differences in CSS properties like font sizes between child themes and other minor customizations, the demo (which uses Air child theme) looks slightly different than the screenshot below, which was taken in Default child theme]

Easy Accordion is a jQuery plugin to create a horizontal accordion and is a free alternative to Slidedeck.

The following steps outline how the plugin's demo can be set up in a WordPress site running iThemes Builder. Once you have the following working, you can then edit the HTML, javascript parameters and CSS to suit your needs.

1. Download the full sample zip file from here under the Conclusion section. Extract the zip file and upload the resulting jQuery-easyAccordion folder to assets directory under your site's root.

The if(is_page( '127' )) conditional is in place to ensure that javascript is loaded and called only where needed (in this case, a specific Page vs site-wide). You would need to either change the ID or remove the conditional to suit your needs.

5. Add the following at the end of child theme's style.css (WP dashboard -> Appearance -> Editor):