This is my collection of WordPress, CSS, JS and PHP code

July 1, 2014 (3 years ago)
General

This is a personal collection of snippets I use on client sites. Some are original, some highly modified or cobbled together from other sources, and some copied (only from open sources such as this.)

I generally prefer not to use plugins for simple features and functions, especially if my clients don’t need options, which is quite often the case.

Free support for these snippets is probably not an option for your unique applications, due to my work load, but it never hurts to ask.

Copy, share and code on!

WordPress ScrollTo Hash

October 31, 2017 (2 weeks ago)
WordPress, JS

I needed to pass a hash, and scroll to the matching anchor on a new page.

Not as easy as it sounds in WordPress. (Pretty permalinks make it not work.)

After much testing and research, here’s the solution that worked best:

I added the “-150” after the offset to show the header above the anchor.

WooCommerce – Conditional Ship to a Different Address Selection

October 31, 2017 (2 weeks ago)
WooCommerce, WordPress, JS, Functions

The scenario is this:

The site has multiple shipping methods (multiple client locations, and the option to ship to “Other”.)

I wanted to show the address panel if “Other” location was chosen, and hide the “Ship to Another Location”  option heading on the shipping page if a client location was chosen.

None of this was strictly necessary, but it makes for a smoother customer experience.

This went into my functions.php file.

Then, this is the hide_ship_to.js script file. (uploaded to the theme directory)

 

Adding FontAwesome Icon to CSS

October 20, 2017 (4 weeks ago)
CSS

To add an icon using only CSS (for example, before list items), use this code:

Of course, you’ll need to have the FontAwesome font installed on your site.

Get the unicodes for icons here:  http://fontawesome.io/cheatsheet/

A Simple jQuery Accordion

February 27, 2017 (9 months ago)
JS, Functions

No need for the overhead of a plugin.

In this case, I have a custom post type for FAQs, where the title is the question, and the content is the answer.

This script hides all the answers until one of the questions is clicked.

Here’s the JavaScript:

And the HTML:

 

Then, this little bit of CSS brings it together:

 

Change the Text of Reset Password Email

September 25, 2016 (1 year ago)
WordPress, PHP, Functions

Send a custom message to users who request a password re-set:

 

Remove WP Version Number

September 23, 2016 (1 year ago)
WordPress, Best Practices, Functions

Prevent robot scrapers from easily finding out which version of WordPress you’re running.

Not a substitute for keeping WP updated, but a handy fallback for clients who may not.

 

List all Posts

August 4, 2016 (1 year ago)
WordPress

Outputs a single list of all your posts.

Thanks, Mickey! (Edited a bit for simplicity)

Create Admin User via FTP

July 9, 2016 (1 year ago)
Functions

When you are locked out of your WordPress site and have no access to PHPMyAdmin, add this to your functions.php file to create a new admin user account.

Remove it from the functions file after you’re successfully logged in. (The user will remain.)

Thanks for the snippet, WPBeginner!

Adding Content to the Admin Edit Screen

October 30, 2015 (2 years ago)
WordPress, Functions

When creating custom post types, you often need custom fields for them. In the past, your options were “normal” or “side” with priorities. Depending on what else is displayed on your edit screen, these boxes could vary in where they appeared.

And sometimes you just need to add a comment or note to the edit pages.

Good News! We now have new hooks to place them precisely:

Here’s an article that explains the usage: https://make.wordpress.org/core/2012/12/01/more-hooks-on-the-edit-screen/

 

Change Date Format on Import to MySQL from CSV File

September 2, 2015 (2 years ago)
Database

You have a .csv file with a date column that is formatted like this:  12/1/2015 (‘m/d/Y’).

You need to import it into a MySQL database DATE field named “my_dates”.

A straight import will result in all your dates showing up as ‘0000-00-00’, because MySQL uses the format ‘Y-m-d’, and won’t translate it natively.

Here’s the solution:

Create a VARCHAR field named “my_text_dates” in the database, and import the dates there.

Then, run this query:

Then, just delete the “my_text_dates” field, and you’re all set!

NOTE: the ‘%m/%d/%Y’ portion of the update statement needs to match exactly the format of your text date field.

Next »

Skip to toolbar