Skip to content

Instantly share code, notes, and snippets.

View paulund's full-sized avatar

Paulund paulund

View GitHub Profile
@paulund
paulund / wordpress-force-pages-ssl.php
Created February 23, 2013 15:51
Force certain Wordpress pages to use SSL
function wps_force_ssl( $force_ssl, $post_id = 0, $url = '' ) {
if ( $post_id == 25 ) {
return true
}
return $force_ssl;
}
add_filter('force_ssl' , 'wps_force_ssl', 10, 3);
@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 / resize-image-keep-aspect-ratio.php
Last active November 5, 2021 10:33
Resize a image to a max width and height and keep aspect ratio.
<?php
public function getImageSizeKeepAspectRatio( $imageUrl, $maxWidth, $maxHeight)
{
$imageDimensions = getimagesize($imageUrl);
$imageWidth = $imageDimensions[0];
$imageHeight = $imageDimensions[1];
$imageSize['width'] = $imageWidth;
@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 )
{