6. April 2022
$hostname = wp_parse_url( home_url(), PHP_URL_HOST );
$tld = end( explode( '.', $hostname ) );
6. April 2022
$hostname = wp_parse_url( home_url(), PHP_URL_HOST );
$tld = end( explode( '.', $hostname ) );
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
15. June 2021
if( get_field('field_from_admin') ) { echo '<p>Test</p>'; }
17. February 2021
$total_keys = array_keys($total_rows);
$fee_name = 'fee_';
foreach ($total_keys as $key) {
if (strlen($key) > 4 && substr($key, 0, 4) === 'fee_' ) {
$fee_name = $key;
break;
}
}
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 );
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);
23. March 2020
function twstudio_which_template() { if ( is_super_admin() ) { global $template; print_r( $template ); } } add_action( 'wp_footer', 'twstudio_which_template' );
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 );
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' ) );
}
}
?>
20. September 2019
<?php echo do_shortcode( '[fl_builder_insert_layout id="19"]' ); ?>
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');
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');
10. July 2018
$require_login = get_field( 'require_logged_in' ); <?php if (! $require_login) : ?> //Do something <?php endif; ?>
<?php
if ( is_user_logged_in() ) {
Do something
} else {
Do something else
}
?>
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'; ?>" />
16. May 2018
Write this after the <?php in top of your functions.php
die( 'Message' );
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/.)
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 );
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' );
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>
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>';