CodeHQ

Registration white list | WordPress

Add this code in to your functions.php to create a domain whitelist for user registrations.

add_action('registration_errors', 'sizeable_restrict_domains', 10, 3);
function sizeable_restrict_domains( $errors, $login, $email ) {
	$whitelist = array(
        'domain.com', 
        'website.com'
    );
	if ( is_email($email) ) {
		$parts = explode('@', $email);
		$domain = $parts[count($parts)-1];
		$to = 'your@adminemail.com';
		$subject = 'Subject of email';
		$message = 'A user tried to register with the following email address: ' . $email;
		if ( !in_array(strtolower($domain), $whitelist) ) {
			$errors->add('email_domain', __('ERROR: You may only register with an approved email address.'));
		wp_mail( $to, $subject, $message );
		}
	}
	return $errors;
}

If user is logged in | WordPress

Use to show content if a user is logged in.

<?php if ( is_user_logged_in() ) { ?>
  .....Content here......
<?php }  ?>

Use ! is_user_logged_in() if you want to show if NOT logged in.

Custom post type at the top of WordPress search results

Paste this in to your functions.php file – this assumes your CPT is called ‘products’

add_filter( 'posts_search_orderby', function( $search_orderby ) {
    global $wpdb;
    return "{$wpdb->posts}.post_type LIKE 'products' DESC, {$search_orderby}";
});

Hiding WP-Login page | WordPress

Using the below code in your functions.php file, you would then only be able to access the WP dashboard by navigating to domain.com/wp-login.php?admin=access.

// Hiding wp-admin and wp-login.php 

function protection_for_login_page() {
$QS = '?admin=access';
$theRequest = 'http://' . $_SERVER['SERVER_NAME'] . '/' . 'wp-login.php' . '?'. $_SERVER['QUERY_STRING'];

	if ( site_url('/wp-login.php').$QS == $theRequest ) {
		echo 'Query string matches';
	} else {
		header( 'Location: http://' . $_SERVER['SERVER_NAME'] . '/' );
	}
}
add_action('login_head', 'protection_for_login_page');

Adding SVG support to WordPress

Add to functions.php

function cc_mime_types( $mimes ){
	$mimes['svg'] = 'image/svg+xml';
	return $mimes;
}
add_filter( 'upload_mimes', 'cc_mime_types' );

Using media gallery image sizes with ACF | WordPress

You can use thumbnail, medium, large or full as the sizes variable to serve the size image you require.

<?php $image = get_field('the_image'); ?>
<img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['caption']; ?>" />

Hiding empty fields with ACF | WordPress

<?php if( get_field('video_url') ):?>
	Show this if field exists <?php the_field('video_url');?>
<?php else : ?>
    Show this if not
<?php endif;?>			

ACF – Flexible content | WordPress

Useful code for Advanced custom fields (ACF) flexible content – perfect for page builders

<?php
if( have_rows('pagebuilder') ):
    while ( have_rows('pagebuilder') ) : the_row();
        if( get_row_layout() == 'hero' ):
        	
        	get_template_part( 'assets/parts/pagebuilder/hero');
     
		elseif( get_row_layout() == 'call_to_action' ):
		
        	get_template_part( 'assets/parts/pagebuilder/call-to-action');
 
        endif;
    endwhile;
else :
    // no layouts found
endif;
?>

WordPress OEmbed – responsive video

Adds a container around iframes to allow responsive video when utilising WordPress OEmbed functionality.

jQuery

$("iframe").wrap("<div class='iframe-container'/>");

SCSS

.iframe-container {
    position: relative;
    padding-bottom: 56.25%;
    height: 0;
    overflow: hidden;
    iframe {
        position: absolute;
        top:0;
        left: 0;
        width: 100%;
        height: 100%;
    }
}

Original source