In: PHP
30 Aug 2010In order to prevent SQL injection, we should clean our input first, and then use parameters we got. In order to clean input that came from our user, just use function “clean” from following snippet: 1 2 3 4 function clean( $input ) { if (get_magic_quotes_gpc()) return mysql_real_escape_string( stripslashes( $input ) ); else return mysql_real_escape_string( [...]
Sometimes there is a need to get posts outside of WordPress. In order to get latest couple of posts and present them as news outside of WP, I have written a function that might be useful. This function has two parameters. First one is database connection, and second is optional array of parameters used to [...]
In: PHP
25 Aug 2010In order to use PHP to count words in some text, you can use str_word_count – a PHP word count function. This is probably the simplest way to count words in your text. PHP count words example: 1 2 3 4 5 //this line will output "3" echo str_word_count("PHP count words"); //this line will [...]
Recently, on website wprecipes.com, I have stumbled upon an interesting script displaying how to integrate your Google Analytics code into your WordPress theme by modifying only functions.php. To do so, just copy and paste following snippet into your functions.php in your current WordPress theme. 1 2 3 4 5 add_action(’wp_header’, ‘ga’); function ga() { [...]
You might want to use WordPress to display random post. In case you want to do so, just use following snippet: 1 2 3 4 5 $rand_post = get_posts(’numberposts=1&orderby=rand’); $post = current($rand_post); the_title(); the_content();
In: PHP
18 Aug 2010Did you ever had a need for logging all parameters sent to your script? If your answer is yes – this is how you can do it in only one line. PHP log REQUEST: error_log( print_r($_REQUEST, true), 3, $_SERVER[’DOCUMENT_ROOT’] . ‘/info_index_log.txt’); This script will grab all data sent to your script (POST or GET) and [...]
In: PHP
17 Aug 2010Whenever you are building dynamic (PHP) website it is a good option to consider caching. Why should you consider PHP caching? You won’t be running a whole PHP script on your server. You won’t send queries to database. You don’t have to have connection to a database. You will have less load on server. You [...]
In: PHP
12 Aug 2010In this snippet we show you how to create simple PHP captcha protection for your website forms. In root of your domain create PHP file called captcha.php, and paste following snippet into it. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 [...]
In: PHP
30 Jul 2010Very often you need to know exact URL of your current page. To get URL of current page, you should just implement following function and apply it, as shown in example below. 1 2 3 4 5 6 function get_URL() { $s = empty($_SERVER["HTTPS"]) ? ” : ($_SERVER["HTTPS"] == "on") ? "s" : ""; $protocol [...]