Enable jQuery with the dollar sign ($) in Wordpress

Posted in Uncategorized on April 20th, 2009 by admin – Be the first to comment

Like many avid jQuery users, I was often frustrated when trying to develop my own theme or plugin for Wordpress and found that I could not use the $ shortcut for jQuery. While this is not much of a problem if I was writing my own program, I was trying to incorporate the plugins that other users had created. Unfortunately, there is no nice answer to this problem.

Wordpress by default loads prototype on the main blog, without jQuery. The admin pages of Wordpress use both prototype and jQuery.

Load jQuery on the main blog

To load jQuery on the main blog, you need to make use of Wordpress’s wp_enqueue_script function. Here is a sample chunk of php code to use to include jQuery. Simply put this at the beginning of your header.php file, before the Doctype declaration:

<?php

function loadScripts(){

wp_enqueue_script(‘jquery’);

}

add_action(‘wp_print_scripts’,loadScripts);

?>

This will load the jquery library when the other scripts Wordpress automatically inserts into your pages are added.

Use the $ sign in your jQuery plugin

To use the $ sign in your jQuery plugin, simply include the following line at the beginning:

var $ = jQuery;

This will override the global prototype shortcut which uses the $ sign for your plugin.

How do I incorporate other jQuery plugins?

To add a plugin for jQuery, you need to first load it with the wp_enqueue_script function, and then resolve the issue of the $ sign in one of three ways:

  1. Do a find-and-replace in the plugin javascript to replace all occurrences of ‘$‘ with ‘jQuery.’
  2. Include the line of code from the above step at the beginning of the plugin javascript.
  3. Load a javascript file before the plugin file that contains the line of code from the above step.

For example, to include the dropshadow and rounded corners plugins:

<?php

function loadScripts(){

wp_enqueue_script(‘jquery’);

//You can use an absolute path, where ‘/’ is the root of your Wordpress installation.

wp_enqueue_script(‘master’,‘/wp-content/themes/Katz%20Dental/scripts/js/master.js’);

wp_enqueue_script(‘jquery.dropshadow’,‘/../scripts/js/jquery.dropshadow.js’);

wp_enqueue_script(‘jquery.corners’,‘/../scripts/js/jquery-corners-0.3/jquery.corners.js’);

}

add_action(‘wp_print_scripts’,loadScripts);

?>

This will load your master.js file, which contains the line of code that allows you to reassign the $ sign shortcut.

Voila! We now have jQuery up and running on Wordpress!

wp_title conditional statement solution

Posted in Uncategorized on April 14th, 2009 by admin – Be the first to comment

Many users of wordpress have run into the problem of trying to determine which page they are on based on the title.   Before jumping to the conclusion that the appropriate way to do this is to reference the pageid, if the user has two blogs and replicates some posts from each, the ids will be difference, but the title will still be the same.

Example - we have a page with the title of ‘Welcome back!’

wp_title() //Will output ‘>> Welcome back!’

As we see, wp_title will append ‘>>’ to the title text.  Unfortunately, this does not help us when comparing the two titles.  wp_title, however, can take two parameters - the first to determine what is appended to the title, and the second to tell the function whether to echo or not.

wp_title(”,false) //Will output ‘  Welcome back!’

Looks good, right?  Unfortunately, this still will not work when comparing to our original title - even though we tell it not to append anything, there are still spaces (&nbsp;) appended.  Thankfully, using the trim function will make everything easier:

if( trim( wp_title(”,false)) === ‘Welcome back!’) //Code…

PHP Security 1::Error Reporting

Posted in Programming, Security on March 29th, 2009 by admin – Be the first to comment

Welcome to the first of the PHP Security posts.  I will be giving some broad, basic security concerns that are a must-read for any PHP developer.

The first topic of discussion will be the concept of error reporting.  This is an extremely useful feature of PHP, as it allows developers to debug their code quicker.  However, while error reporting is all well and good for development purposes, it is a big no-no for a production environment.  Read on for how to log your errors without displaying any sensitive debugging information to the end-user.

read more »

Arpspoof for Dummies - a HowTo Guide

Posted in HowTo, Security on March 29th, 2009 by admin – Be the first to comment

Welcome back everyone. This post is intended to provide a simple approach to learning the fundamentals of arp spoofing. By the end of this article, you will be fully equipped with how to arpspoof a machine.

Note: These techniques should be used only to test your own networks. It is illegal to spoof computers on networks you do not own, or have a court order for.
read more »

Website Optimization

Posted in HowTo, Programming on March 29th, 2009 by admin – Be the first to comment

Website optimization is becoming increasingly important as more and more users browse the internet. To attract visitors, it is important to make sure that the site loads quickly, and in this article I will discuss the common areas where website optimization can be applied, and recommendations on how to optimize your site.

read more »

Installing PHP5 on IIS 6 with the GD Library

Posted in Programming on March 29th, 2009 by admin – Be the first to comment

So after spending about three hours on my windows machine here at work trying to get PHP and GD to cooperate with IIS, I thought I would write about it to save everyone some headaches. Now, just as a preface, as much as I dislike windows, especially since thanks to darwinports or fink on my mac I am able to install everything I need with one command, I have to say that IIS really was not difficult at all to work with. Just make sure to adhere to the following steps and everything should work OK:

read more »

MySQL LOAD DATA LOCAL INFILE returns Column count does not match value count at row 1 error

Posted in HowTo, Programming on March 29th, 2009 by admin – Be the first to comment

Scenario: I have a comma-separated value list (*.csv) file that I would like to import into my database. However, I put in the code:

LOAD DATA LOCAL INFILE '/path/to/csv/file.csv'
INTO TABLE test_table
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'

and I get an error: Column count does not match value count at row 1.

Now, normally when I see this error I immediately think that I am trying to insert values into more columns than there actually are in the table. Once I verify that I am assigning one value per column, and that EVERY COLUMN gets a value, the next thing to check with an error like this is the primary key column. Usually this will be an auto_increment column, but it still needs a value when inserting into a row. If you are simply adding to the file, you can use ” or ‘\N’ as a value for this field - it will automatically assign itself the next proper number in the sequence.
read more »

Increase mac system volume above the max - VLC, QuickTime, and AudioHijack

Posted in HowTo, Mac on March 29th, 2009 by admin – Be the first to comment

After trying numerous times to figure out a way to get my laptops speakers to go louder than the default, I finally found a couple of solutions. My problem has been that I will be listening to an audio or video file, and it plays too quietly, and I need to make it louder. A big issue is that sometimes, it is not your computers fault; the file may have been recorded quietly, which will affect the playback volume.

First, not much of a solution, is to play back video or audio files with the VLC media player. The volume controls here can raise the volume to about 400% I believe.

For those users who use Quicktime, there is a hidden option to raise the volume above the max. If you are streaming media and Quicktime is embedded in your browser, simply holding the shift key and clicking the volume icon will display a larger volume slider. In the standalone Quicktime application, when the volume is already on max, hold shift and press the up arrow to raise the volume above the max. If this becomes too loud, or you want to play another file on normal volume, pressing up or down will reset the volume to the default max.

Finally, the last solution I have is to use Audio Hijack. Simply open up the application, and if the application you are using for playback is not in the sidebar, simply add it. Then hijack the program BEFORE you open it (unless you also have Instant Hijack installed), and then in the effects tab, slide the Master Gain slider all the way to the right. This will output audio at 200%. If, for some reason, you need to make the volume even louder, then on one of the effects squares, click on the square and select 4FXEffect -> Double Gain. Raising the In or Out knobs seems to raise the output volume even more - in that case I usually try to raise the knobs symmetrically, instead of leaving the In maxed and the Out low, but I do not think this makes a difference.

Hope this helps you, let me know how this works!