Skip to content

Code

  • Change empty basket text in WooCommerce

    							
    								function woocommerce_empty_cart_message() {
    
    		$message = 'Your basket is currently empty.';
    
    		echo '<div class="o-notice o-notice--error">'. $message .'</div>';
    
    	}
    	add_filter( 'wc_empty_cart_message', 'woocommerce_empty_cart_message' );							
    						
  • Display user notifications in PHP

    							
    								function display_notification($text, $type = 'error') {
    		echo sprintf('
    			<div class="o-notice o-notice--%s">
    				<p class="o-notice__message">%s</p>
    			</div>
    		', $type, $text);
    	}							
    						
  • Display Advanced Custom Fields

    							
    								function starter_get_acf_field( $key ) {
    		$value = get_field( $key );
    		return ( false === empty( $value ) ) ? $value : '';
    	}
    
    	function starter_get_acf_sub_field( $key ) {
    		$value = get_sub_field( $key );
    		return ( false === empty( $value ) ) ? $value : '';
    	}							
    						
  • Insert WordPress featured image

    							
    								function wp_post_thumbnail( $size = 'large' ) {
    
    		if( has_post_thumbnail() )
    		{
    			global $post;
    			
    			//Thumbnail ID
    			$thumbnail_id = ( get_post_thumbnail_id( $post->ID ) ) ? get_post_thumbnail_id( $post->ID ) : '';
    
    			//Alt Attribute
    			$alt = ( get_post_meta( $post->ID, '_wp_attachment_image_alt', true ) ) ? get_post_meta( $post->ID, '_wp_attachment_image_alt', true ) : 'Featured Image for '. trim( strip_tags( $post->post_title ) );
    
    			//Title Attribute
    			$title = ( get_the_title( $thumbnail_id ) ) ? get_the_title( $thumbnail_id ) : trim( strip_tags( $post->post_title ) ) .' Featured Image';
    
    			//Image Meta
    			$metadata = wp_get_attachment_metadata( $thumbnail_id );
    
    			if( $metadata ) {
    				$copyright = ( $metadata['image_meta']['copyright'] ) ? $metadata['image_meta']['copyright'] : '';
    			}
    			//Render image
    			echo '<figure class="o-thumbnail u-relative">';
    
    			the_post_thumbnail( $size , array( 'class' => 'o-thumbnail__image', 'itemprop' => 'image', 'alt' => $alt, 'title' => $title ) );
    			
    			if ( $caption )
    			{
    				echo '<figcaption class="o-thumbnail__caption u-relative">'. $caption .'</figcaption>';
    			}
    
    			if ( $copyright ) {
    				echo '<small class="o-thumbnail__copyright">'. $copyright .'</small>';
    			}
    
    			echo '</figure>';
    		}
    	}							
    						
  • WordPress responsive navigation

    							
    								function wp_navigation( $theme_location, $menu_toggle = false ) 
    	{
    		if ( has_nav_menu( $theme_location ) ) 
    		{ 
    			if( $menu_toggle === true ) {
    				echo '<a class="c-toggle c-toggle--'. $theme_location .' u-hidden@md u-hidden@lg u-hidden@print" href="#">Menu</a>';	
    		    }
    
    		    wp_nav_menu( array(
    		        'theme_location' 	=> $theme_location,
    		        'container'      	=> 'nav',
    		        'container_class' 	=> 'c-navmenu c-navmenu--'. $theme_location .' u-hidden@print',
    		        'menu_class'     	=> 'c-navmenu__menu c-navmenu__menu--'. $theme_location,
    		        'items_wrap'     	=> '<ul class="%2$s">%3$s</ul>'
    		    ));
    		}
    	}							
    						
  • Add class to custom WordPress Logo

    							
    								function wp_logo_class() {
    	    $custom_logo_id = get_theme_mod( 'custom_logo' );
    
    	    $html = sprintf( '<a href="%1$s" class="custom-logo-link" rel="home" itemprop="url">%2$s</a>',
    	            esc_url( home_url( '/' ) ),
    	            wp_get_attachment_image( $custom_logo_id, 'full', false, array(
    	                'class'    => 'site-logo',
    	            ) )
    	        );
    	    return $html;   
    	}
    	add_filter( 'get_custom_logo', 'wp_logo_class' );							
    						
  • Add styles to WordPress editor

    							
    								function wp_add_editor_styles() {
    	if( file_exists(  STARTER_THEME_INC . 'assets/css/editor-style.css' ) ) {
    	    	add_editor_style( STARTER_THEME_INC . 'assets/css/editor-style.css' );
    	}
    }
    add_action( 'admin_init', 'starter_add_editor_styles' );							
    						
  • Get custom WordPress image

    							
    								function wp_custom_logo() {
    	$custom_logo_id = get_theme_mod( 'custom_logo' );
    	if( false === empty( $custom_logo ) ) {
    		$image = wp_get_attachment_image_src( $custom_logo_id , 'full' );
    		return $image[0];
    	}
    }							
    						
  • Remove inline width and height from WordPress images

    							
    								function wp_remove_image_attributes( $html ) {
       $html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
       return $html;
    }							
    						
  • Add WordPress sidebar

    							
    								function starter_sidebars() {
    	register_sidebar( array( 
    		'name' => 'Site Sidebar',
    		'id' => 'site-sidebar', 
    		'description'   => __( 'Add widgets to the sidebar throughout the site.', 'starter' ),
    		'before_widget' => '<aside class="o-widget">',
    		'after_widget'  => '</aside>',
    		'before_title'  => '<h2 class="o-widget__title">',
    		'after_title'   => '</h2>', 
    	) );	
    }
    add_action( 'widgets_init', 'starter_sidebars' );							
    						
© 2018 Graeme Pirie