Skip to content

Instantly share code, notes, and snippets.

View paulund's full-sized avatar

Paulund paulund

View GitHub Profile
@paulund
paulund / doctrine-dump.php
Created February 24, 2013 13:29
A code snippet to dump doctrine objects.
\Doctrine\Common\Util\Debug::dump($var, $maxDepth)
@paulund
paulund / delete-directory-and-files.php
Created March 29, 2013 22:13
Delete a directory and all files in the directory with PHP.
<?php
function delete_directory($dirname) {
if (is_dir($dirname))
$dir_handle = opendir($dirname);
if (!$dir_handle)
return false;
while($file = readdir($dir_handle)) {
if ($file != "." && $file != "..") {
if (!is_dir($dirname."/".$file))
unlink($dirname."/".$file);
@paulund
paulund / wordpress-always-checked-remember-me.php
Created April 2, 2013 20:39
There is a login box on the Wordpress admin area login page to make sure this is always checked use the following snippet. Only use this if you know the only time admin users access the admin area is going to be on a private computer. Example of using it is http://www.paulund.co.uk/set-remember-me-to-be-always-checked
<?php
function login_checked_remember_me() {
add_filter( 'login_footer', 'rememberme_checked' );
}
add_action( 'init', 'login_checked_remember_me' );
function rememberme_checked() {
echo "document.getElementById('rememberme').checked = true;";
}
@paulund
paulund / remove-non-ascii-characters.php
Created April 6, 2013 17:47
Replace anything that is not an 'a-z', 'A-Z', or '0-9' from the given $value
<?php
// Replace anything that is not an 'a-z', 'A-Z', or '0-9' from the given $value
$value = preg_replace( "/[^a-zA-Z0-9\s]/", "", $value );
?>
@paulund
paulund / minimal-comment-character-limit.php
Created April 7, 2013 17:13
Set a minimal character comment limit in Wordpress.
<?php
add_filter( 'preprocess_comment', 'minimal_comment_length' );
function minimal_comment_length( $commentdata ) {
$minimalCommentLength = 20;
if ( strlen( trim( $commentdata['comment_content'] ) ) < $minimalCommentLength )
{
wp_die( 'All comments must be at least ' . $minimalCommentLength . ' characters long.' );
}
@paulund
paulund / update-row-with-highest-id.sql
Created June 3, 2013 19:58
When you need to update a database row with the highest ID in the table. You can get the highest id by using the MAX() function but this function doesn't work in a nested select so you can't do this. UPDATE table SET name='test_name' WHERE id = (SELECT max(id) FROM table) Therefore you need to use a different way of getting the highest ID. This …
UPDATE table SET name='test_name' ORDER BY id DESC LIMIT 1;
@paulund
paulund / debug-to-console.php
Last active December 18, 2015 07:09
Instead of sending debug data to a log file, just display it in the browser console with the following PHP snippet.
<?php
/**
* Send debug code to the Javascript console
*/
function debug_to_console($data) {
if(is_array($data) || is_object($data))
{
echo("<script>console.log('PHP: ".json_encode($data)."');</script>");
} else {
echo("<script>console.log('PHP: ".$data."');</script>");
@paulund
paulund / delete-all-files-in-folder.php
Last active December 18, 2015 08:49
Delete all files from a directory
<?php
/*
* php delete function that deals with directories recursively
*/
function delete_files($target) {
if(is_dir($target)){
$files = glob( $target . '*', GLOB_MARK ); //GLOB_MARK adds a slash to directories returned
foreach( $files as $file )
{
@paulund
paulund / debug-array-php.php
Last active December 18, 2015 23:19
Debug Array in PHP
<?php
echo '<pre>';
print_r($debug_array);
echo '</pre>';
exit;
@paulund
paulund / cache-pages-with-php.php
Created June 25, 2013 18:27
Here is a quick code snippet which will allow you to easily cache pages in PHP.
<?php
//cache file
$cachefile = 'cached/'.date('M-d-Y').'.php';
//Total time the file will be cached in seconds set to 10 hours
$cachetime = 36000;
//If the cache file already exists and the cache file is over 10hours old then display the cache file
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
include($cachefile);