4 Ways You Can Make Your Reps More Productive Today – Download Now

Adding styles/scripts only when a shortcode is present in content

Shortcodes are one of the wonders of WordPress and allows the developer an easy way to provide a functionality to the end user. However, it can be slightly tricky to add a script or a stylesheet to a shortcode which only loads when the shortcode is there (so basically instead of loading the script/stylesheet sitewide WordPress only loads it when the content contains the shortcode).

Basically the trick is to register the style/script and then enqueue it only when you need it.

So in your functions.php (or main plugin file) do something like this:

[php] add_action( ‘init’, ‘my_shortcode_scripts_styles’ );   //Hook your function to when WP initializes
function my_shortcode_scripts_styles() {
wp_register_style( ‘my-shortcode-style’, ‘url-to-the-stylesheet’);
}
//The function above registers the style (or scripts if you need those too) [/php]

Remember to change the parameters to suit your needs. Then in your function that renders the shortcode do this:

[php] wp_enqueue_style( ‘my-shortcode-style’ ); [/php]

 

Getting WooCommerce product detail in a page template

We recently had a project where there was a need to get the product description on a page. The page basically needed to show the product description and some other details only (not the full product) on a page. Here is how we handled it – any suggestions or comments on improving this would be welcome:

Show different excerpt length for different post types

If you have custom post types then there is a likelihood that you may want to show them on the same page but have different excerpt length for each of theme. Here is a simple snippet to help do this:

Replace “my-custompost-type1” with the slug of your custom post type. Obviously you can also add another if statement and adjust for other custom post types that you may have.

Contact forms and spam

One of the big issues on having a contact form on your website is the spam it attracts. Once your site gets listed on the radar for these spammers then there is simply no end to the endless stream of spam that will show up in your mailbox. There are 2 main ways to handle this:
1. Use a captcha/formula
These are the funny looking characters/numbers that are randomly generated so that bots cannot go through them. Equally irritating is the fact that humans too may have a tough time getting through these! A variation of these captcha is the formula based captcha where a basic algebraic equation is presented and the user is requested to compute the missing value.

2. Honeypot captcha
This relies on the weakness in spambots to just fill in fields that seem to be marked as mandatory but which are actually just a bait to know that the submission is coming from a bot. A human user would never see (or if using a text browser clearly see that the label for the field shows that it is for spammers only) this field and the system would be able to filter out if the submission is valid or not. This clearly seems to be a better solution and can be applied to both contact forms and comment forms in WordPress.

Choose any of the anti-spam methods that seems to work well for your situation – but do ensure that you have at least some mechanism in place.