Category: Code

Add custom field to WooCommerce product category

// Add term page
function custom_url_taxonomy_add_new_meta_field() {
	// this will add the custom meta field to the add new term page
	?>
	<div class="form-field">
		<label for="term_meta[custom_term_meta]"><?php _e( 'Custom url category', 'custom_url_category' ); ?></label>
		<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="">
		<p class="description"><?php _e( 'Inserisci un custom url prodotto per la categoria','custom_url_category' ); ?></p>
	</div>
<?php
}
add_action( 'product_cat_add_form_fields', 'custom_url_taxonomy_add_new_meta_field', 10, 2 );
// Edit term page
function custom_url_taxonomy_edit_meta_field($term) {
 
	// put the term ID into a variable
	$t_id = $term->term_id;
 
	// retrieve the existing value(s) for this meta field. This returns an array
	$term_meta = get_option( "taxonomy_$t_id" ); ?>
	<tr class="form-field">
	<th scope="row" valign="top"><label for="term_meta[custom_term_meta]"><?php _e( 'Custom url category', 'custom_url_category' ); ?></label></th>
		<td>
			<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="<?php echo esc_attr( $term_meta['custom_term_meta'] ) ? esc_attr( $term_meta['custom_term_meta'] ) : ''; ?>">
			<p class="description"><?php _e( 'Inserisci un custom url prodotto per la categoria','custom_url_category' ); ?></p>
		</td>
	</tr>
<?php
}
add_action( 'product_cat_edit_form_fields', 'custom_url_taxonomy_edit_meta_field', 10, 2 );
// Save extra taxonomy fields callback function.
function save_taxonomy_custom_meta( $term_id ) {
	if ( isset( $_POST['term_meta'] ) ) {
		$t_id = $term_id;
		$term_meta = get_option( "taxonomy_$t_id" );
		$cat_keys = array_keys( $_POST['term_meta'] );
		foreach ( $cat_keys as $key ) {
			if ( isset ( $_POST['term_meta'][$key] ) ) {
				$term_meta[$key] = $_POST['term_meta'][$key];
			}
		}
		// Save the option array.
		update_option( "taxonomy_$t_id", $term_meta );
	}
}  
add_action( 'edited_product_cat', 'save_taxonomy_custom_meta', 10, 2 );  
add_action( 'create_product_cat', 'save_taxonomy_custom_meta', 10, 2 );

 

How to Show Registration Date in WordPress User Profiles

If you show registration date in Author page, Use this code in author.php

<?php
    global $wp_query;
    $registered = date_i18n( "M m, Y", strtotime( get_the_author_meta( 'user_registered', $wp_query->queried_object_id ) ) );
    echo 'Member since ' . $registered;
?>

If you show registration date in Single post, Use this code in single.php

<?php
  $user_ID = $post->post_author;
  $registered = date_i18n( "M m, Y", strtotime( get_the_author_meta( 'user_registered', $user_ID ) ) );
  echo 'Member since ' . $registered;
?>

Thank you 🙂

How to Enqueue Google Fonts on WordPress Theme

Register custom fonts

/**
 * Register custom fonts.
 */
function themeslug_fonts_url() {
	$fonts_url = '';
	$fonts     = array();
	$subsets   = 'latin,latin-ext';

	/*
	 * Translators: If there are characters in your language that are not supported
	 * by Oswald, translate this to 'off'. Do not translate into your own language.
	 */
	if ( 'off' !== _x( 'on', 'Oswald font: on or off', 'themeslug' ) ) {
		$fonts[] = 'Oswald:400,700';
	}

	/*
	 * Translators: If there are characters in your language that are not supported
	 * by Montserrat, translate this to 'off'. Do not translate into your own language.
	 */
	if ( 'off' !== _x( 'on', 'Montserrat font: on or off', 'themeslug' ) ) {
		$fonts[] = 'Montserrat:400,700';
	}


	if ( $fonts ) {
		$fonts_url = add_query_arg( array(
			'family' => urlencode( implode( '|', $fonts ) ),
			'subset' => urlencode( $subsets ),
		), 'https://fonts.googleapis.com/css' );
	}


	return esc_url_raw( $fonts_url );
}

Continue Reading

How to display WordPress post view count without a plugin

Tutorial 1

Use this code in functions.php

function rm_post_view_count(){
	if ( is_single() ){
		global $post;
		$count_post = esc_attr( get_post_meta( $post->ID, '_post_views_count', true) );
		if( $count_post == ''){
			$count_post = 1;
			add_post_meta( $post->ID, '_post_views_count', $count_post);
		}else{
			$count_post = (int)$count_post + 1;
			update_post_meta( $post->ID, '_post_views_count', $count_post);
		}
	}
}
add_action('wp_head', 'rm_post_view_count');

Continue Reading

Visual Composer Get images from “attach_images”

In VC_MAP definition you have:

array(
	"type"        => "attach_images",
	"heading"     => esc_html__( "Screenshots", "text-domain" ),
	"description" => esc_html__( "Add screenshots.", "text-domain" ),
	"param_name"  => "screenshots",
	"value"       => "",
),

In shortcode function you should have: Continue Reading