Add custom ACF field to Woo Attributes

10. November 2021

// Adds a custom rule type.
add_filter( 'acf/location/rule_types', function( $choices ){
    $choices[ __("Other",'acf') ]['wc_prod_attr'] = 'WC Product Attribute';
    return $choices;
} );

// Adds custom rule values.
add_filter( 'acf/location/rule_values/wc_prod_attr', function( $choices ){
    foreach ( wc_get_attribute_taxonomies() as $attr ) {
        $pa_name = wc_attribute_taxonomy_name( $attr->attribute_name );
        $choices[ $pa_name ] = $attr->attribute_label;
    }
    return $choices;
} );

// Matching the custom rule.
add_filter( 'acf/location/rule_match/wc_prod_attr', function( $match, $rule, $options ){
    if ( isset( $options['taxonomy'] ) ) {
        if ( '==' === $rule['operator'] ) {
            $match = $rule['value'] === $options['taxonomy'];
        } elseif ( '!=' === $rule['operator'] ) {
            $match = $rule['value'] !== $options['taxonomy'];
        }
    }
    return $match;
}, 10, 3 );

This goes to functions.php and afterwards will be visible in Custom Fields where you set where the custom field should be visible

Translation with gettext

18. August 2020

function this_translations( $translation, $text, $domain ) {
	if ( 'woocommerce' === $domain ) {
		switch ( $translation ) {
			case 'Includes':
				$translation = 'Inkluderet';
				break;
			case 'MVA':
				$translation = 'moms';
				break;
		}
	}

	return $translation;
}
add_filter( 'gettext', 'this_translations', 10, 3 );
Replace ‘Dismiss’ with an X in Woo store notice

2. April 2020

function twstudio_demo_store_filter($text) {

	$text = str_replace(array('<p class="woocommerce-store-notice demo_store">', '</p>', 'Dismiss'), array('<div class="woo-notice-container"><p class="woocommerce-store-notice demo_store">', '</p></div>', 'X'), $text);
	return $text;
}
add_filter('woocommerce_demo_store', 'twstudio_demo_store_filter', 10, 1);
Remove Uncategorized from widget

20. March 2020

function remove_uncategorized_category( $terms, $taxonomy, $query_vars, $term_query ) {
if ( is_admin() )
return $terms; if ( $taxonomy[0] == 'product_cat' ) {
foreach ( $terms as $k => $term ) {
if ( $term->term_id == get_option( 'default_product_cat' ) ) {
unset( $terms[$k] );
}
}
} return $terms;
}
add_filter( 'get_terms', 'remove_uncategorized_category', 10, 4 );
Simple function based on queried page

12. February 2020

<?php
$term = get_queried_object();
$type = $term->post_type;
if ( $type === 'event' ) {
	if ( has_post_thumbnail() ) {
		$size = apply_filters( 'twstudio_default_thumbnail_size', 'large' );
		the_post_thumbnail( $size, array( 'class' => 'featured-image' ) );
	}
}
?>
Check if browser is IE

22. May 2019

$ua = htmlentities($_SERVER['HTTP_USER_AGENT'], ENT_QUOTES, 'UTF-8');
if (preg_match('~MSIE|Internet Explorer~i', $ua) || (strpos($ua, 'Trident/7.0') !== false && strpos($ua, 'rv:11.0') !== false)) {
	// do stuff for IE
}

Add it to a WordPress site with an ACF Option page that have a field: ie_message:

/**
 * Check if browser is Internet Explorer
 */
function twstudio_check_if_browser_is_ie() {
	$ua = htmlentities($_SERVER['HTTP_USER_AGENT'], ENT_QUOTES, 'UTF-8');
	if (preg_match('~MSIE|Internet Explorer~i', $ua) || (strpos($ua, 'Trident/7.0') !== false && strpos($ua, 'rv:11.0') !== false)) {
		if (get_field('ie_message', 'option')) {
			echo '<style>.internet-explorer-banner {position: fixed; background:red; color: white; top: 0; z-index: 999; left: 0; right: 0; font-size: 20px; text-align: center;}</style>';
			echo '<div style="" class="internet-explorer-banner">';
			the_field('ie_message', 'option');
			echo '</div>';
		}
	}
}
add_action('wp_head', 'twstudio_check_if_browser_is_ie');
Replace a text string in PHP/WordPress

8. March 2019

function translate_this( $text_to_translate ) {
	$search  = array('One','Two');
	$replace = array('En','To');
	return str_replace( $search, $replace, $text_to_translate );
}
add_filter('the_content', 'translate_this');
Get site url

8. July 2018

Gives you the sites url..

<?php echo get_site_url(); ?>

Gives you a specific page:

<?php the_permalink( 9595 ); ?>

Gives you an image:

<img src="<?php echo get_template_directory_uri() . '/images/logo.png'; ?>" />
Set the Thumbnail size

15. May 2018

This goes into your theme setup function in functions.php

set_post_thumbnail_size( 1260, 700, true );

(To register new sizes: https://developer.wordpress.org/reference/functions/add_image_size/.)

Hook example

8. May 2018

/**
 * Add wrapper to single event content
 */
function customname_event_before_single_content() {
	echo '<div class="thumbnail">';
	the_post_thumbnail();
	echo '</div>';

	echo '<div class="event-content-wrapper">';
}
add_action( 'twstudio_event_before_single_content', 'customname_event_before_single_content', 55 );

function customname_event_after_single_content() {
	echo '</div>';
}
add_action( 'twstudio_event_after_single_content', 'customname_event_after_single_content', 5 );
Another ONE:

/**
 * Adds wrapper around single product image and main product data
 */
function twstudio_single_product_wrapper_before() {
   echo '<div class="single-product-container">';
}
add_action( 'woocommerce_before_single_product_summary', 'twstudio_single_product_wrapper_before', 5 );


function twstudio_single_product_wrapper_after() {
   echo '</div>';
}
add_action( 'woocommerce_after_single_product_summary', 'twstudio_single_product_wrapper_after', 5 );
Add Taxonomies for media gallery

6. May 2018

Goes into functions.php

//adds categorys
function wptp_add_categories_to_attachments() {
	register_taxonomy_for_object_type( 'category', 'attachment' );
}
add_action( 'init' , 'wptp_add_categories_to_attachments' );

//adds tags
function wptp_add_tags_to_attachments() {
	register_taxonomy_for_object_type( 'post_tag', 'attachment' );
}
add_action( 'init' , 'wptp_add_tags_to_attachments' );

// register new taxonomy which applies to attachments
function wptp_add_location_taxonomy() {
	$labels = array(
		'name'              => 'Locations',
		'singular_name'     => 'Location',
		'search_items'      => 'Search Locations',
		'all_items'         => 'All Locations',
		'parent_item'       => 'Parent Location',
		'parent_item_colon' => 'Parent Location:',
		'edit_item'         => 'Edit Location',
		'update_item'       => 'Update Location',
		'add_new_item'      => 'Add New Location',
		'new_item_name'     => 'New Location Name',
		'menu_name'         => 'Location',
	);

	$args = array(
		'labels' => $labels,
		'hierarchical' => true,
		'query_var' => 'true',
		'rewrite' => 'true',
		'show_admin_column' => 'true',
	);

	register_taxonomy( 'location', 'attachment', $args );
}
add_action( 'init', 'wptp_add_location_taxonomy' );
Ny first PHP code

24. April 2018

<div class="forloop">
	<?php
	for ( $x = 1; $x <= 4; $x ++ ) {
		echo "The number is: $x <br>";
	}
	?>
</div>

<div class="array">
	<?php
	$names = array( "Anders", "Kim", "Fredrik" );
	print_r( $names );
	?>
</div>

<div class="if">
	<?php
	$var1 = 20;
	$var2 = 40;

	if ( $var1 > $var2 ) {
		echo "$var1 er større enn $var2";
	} else {
		echo "$var1 er mindre enn $var2";
	}
	?>
</div>

<div class="function">
	<?php
	function first_function() {
		echo "Min første PHP funksjon";
	}

	first_function()
	?>
</div>

 

PHP/WP nice to know things

20. April 2018

//Prints out everything that has to do with a certain object to do.
echo '<pre>';
print_r($image);
echo '</pre>';
//Check if a posts is not a certain category
<?php if ( ! is_category( 'school-projects' ) ) { the_category(); } ?>
//Test a filter function
function test() {
	global $wp_filter;
	echo '<pre>';
	print_r( $wp_filter['woocommerce_before_shop_loop'] );
	echo '</pre>';
}
add_action( 'wp', 'test' );
//Check whats queried (nice to use with taxonomies)
$queried_object = get_queried_object();

echo '<pre>';
print_r( $queried_object );
echo '</pre>';