Skip to content

Instantly share code, notes, and snippets.

@paulund
paulund / wordpress-force-pages-ssl.php
Created Feb 23, 2013
Force certain Wordpress pages to use SSL
View wordpress-force-pages-ssl.php
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 Feb 24, 2013
A code snippet to dump doctrine objects.
View doctrine-dump.php
\Doctrine\Common\Util\Debug::dump($var, $maxDepth)
@paulund
paulund / resize-image-keep-aspect-ratio.php
Last active Nov 5, 2021
Resize a image to a max width and height and keep aspect ratio.
View resize-image-keep-aspect-ratio.php
<?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 Mar 29, 2013
Delete a directory and all files in the directory with PHP.
View delete-directory-and-files.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 Apr 2, 2013
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
View wordpress-always-checked-remember-me.php
<?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 Apr 6, 2013
Replace anything that is not an 'a-z', 'A-Z', or '0-9' from the given $value
View remove-non-ascii-characters.php
<?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 Apr 7, 2013
Set a minimal character comment limit in Wordpress.
View minimal-comment-character-limit.php
<?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 Jun 3, 2013
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 …
View update-row-with-highest-id.sql
UPDATE table SET name='test_name' ORDER BY id DESC LIMIT 1;
@paulund
paulund / debug-to-console.php
Last active Dec 18, 2015
Instead of sending debug data to a log file, just display it in the browser console with the following PHP snippet.
View debug-to-console.php
<?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 Dec 18, 2015
Delete all files from a directory
View delete-all-files-in-folder.php
<?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 )
{