Skip to content

Instantly share code, notes, and snippets.

@MdrnWebDesigner
MdrnWebDesigner / addSVG.php
Created July 26, 2018 17:40
Add SVG Compatibility to WordPress
add_filter('upload_mimes', 'my_upload_mimes');
function my_upload_mimes($mimes = array()) {
$mimes['svg'] = 'image/svg+xml';
return $mimes;
}
@MdrnWebDesigner
MdrnWebDesigner / fontawesome.php
Created July 26, 2018 17:39
Add Fontawesome Icons to Your Site
add_action( 'wp_enqueue_scripts', 'enqueue_load_fa' );
function enqueue_load_fa() {
wp_enqueue_style( 'load-fa', 'https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css' );
}
@MdrnWebDesigner
MdrnWebDesigner / dynamicCopyrightDateFooter.php
Created July 26, 2018 17:37
Add a Copyright that Changes Based on the Date
function comicpress_copyright() {
global $wpdb;
$copyright_dates = $wpdb->get_results("
SELECT
YEAR(min(post_date_gmt)) AS firstdate,
YEAR(max(post_date_gmt)) AS lastdate
FROM
$wpdb->posts
WHERE
@MdrnWebDesigner
MdrnWebDesigner / php.ini
Created July 26, 2018 17:34
Increase Memory Limit in php.ini
define('WP_MEMORY_LIMIT', '300M');
@MdrnWebDesigner
MdrnWebDesigner / wordPressTagCloud.php
Created July 26, 2018 17:33
Add a WordPress Tag Cloud Anywhere on Your Site
<?php wp_tag_cloud(array(
'smallest' => 10, // size of least used tag
'largest' => 18, // size of most used tag
'unit' => 'px', // unit for sizing
'orderby' => 'name', // alphabetical
'order' => 'ASC', // starting at A
'exclude' => 6 // ID of tag to exclude from list
));
?>
@MdrnWebDesigner
MdrnWebDesigner / additionalImageSizes.php
Created July 26, 2018 17:31
Add Additional Image Sizes to Your WordPress Options
if ( function_exists( 'add_image_size' ) ) {
add_image_size( 'my-custom-image1', 400, 9999 ); //400 pixels wide (and unlimited height)
add_image_size( 'my-custom-image2', 320, 250, true ); //(true means the image will be cropped)
}
<?php echo get_the_post_thumbnail($post->ID, 'my-custom-image1'); ?>
@MdrnWebDesigner
MdrnWebDesigner / redirectUserAfterLogin.php
Created July 26, 2018 17:29
Redirect WordPress Users to Different Pages on Login
function redirect_user_on_role()
{
//retrieve current user info
global $current_user;
get_currentuserinfo();
//If login user role is Subscriber
if ($current_user->user_level == 0)
{
wp_redirect( home_url() ); exit;
}
@MdrnWebDesigner
MdrnWebDesigner / firstImageOfPostFeaturedImage.php
Created July 26, 2018 17:26
How to Set the First Image of Your Post as Featured
function autoset_featured() {
global $post;
$already_has_thumb = has_post_thumbnail($post->ID);
if (!$already_has_thumb) {
$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
if ($attached_image) {
foreach ($attached_image as $attachment_id => $attachment) {
set_post_thumbnail($post->ID, $attachment_id);
}
}
@MdrnWebDesigner
MdrnWebDesigner / hideFormForSubscribers.php
Created July 26, 2018 17:23
Hide Subscription Form from Returning Subscribers
<?php
/**
* Set a cookie whenever someone subscribes, storing the form ID
*/
add_action( 'mc4wp_form_subscribed', function( $form ) {
$expires = time() + 3600 * 24 * 90; // 90 days
setcookie( 'mc4wp_subscribed', $form->ID, $expires, '/' );
});
/**
* Prints CSS that hides a specific MailChimp for WordPress form if the "subscribed" cookie is set.
@MdrnWebDesigner
MdrnWebDesigner / removeQueryStringStaticResources.php
Created July 26, 2018 17:20
Remove Query Strings from Static Resources in WordPress
/**
* Remove query string from static resources
*/
function remove_cssjs_ver( $src ) {
if ( strpos( $src, '?ver=' ) )
$src = remove_query_arg( 'ver', $src );
return $src;
}
add_filter( 'style_loader_src', 'remove_cssjs_ver', 10, 2 );