find this code in the business bloomber. I want to add target blank attribute in this code can any help me?
function bbloomer_image_link_external_url( $html,
$post_thumbnail_id ) {
global $product;
if ( ! $product->is_type( 'external' ) ) return $html;
$url = $product->add_to_cart_url();
$pattern = "/(?<=href=(\"|'))[^\"']+(?=(\"|'))/";
$html = preg_replace( $pattern, $url, $html);
return $html ;
}
You can use str_replace. just add the below line after preg_replace.
$html = str_replace('<a', '<a target="_blank" ', $html);
Complete code.
function bbloomer_image_link_external_url( $html, $post_thumbnail_id ) {
global $product;
if ( ! $product->is_type( 'external' ) ) return $html;
$url = $product->add_to_cart_url();
$pattern = "/(?<=href=(\"|'))[^\"']+(?=(\"|'))/";
$html = preg_replace( $pattern, $url, $html );
$html = str_replace('<a', '<a target="_blank" ', $html);
return $html;
}
add_filter( 'woocommerce_single_product_image_thumbnail_html', 'bbloomer_image_link_external_url', 100, 2 );
Tested and works.
Related
Hi i enabled the review on woocommerce and doesn't show on products page and i put this code in snippets
function bbloomer_product_reviews_shortcode( $atts ) {
if ( empty( $atts ) ) return '';
if ( ! isset( $atts['id'] ) ) return '';
$comments = get_comments( 'post_id=' . $atts['id'] );
if ( ! $comments ) return '';
$html .= '<div class="woocommerce-tabs"><div id="reviews"><ol class="commentlist">';
foreach ( $comments as $comment ) {
$rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
$html .= '<li class="review">';
$html .= get_avatar( $comment, '60' );
$html .= '<div class="comment-text">';
if ( $rating ) $html .= wc_get_rating_html( $rating );
$html .= '<p class="meta"><strong class="woocommerce-review__author">';
$html .= get_comment_author( $comment );
$html .= '</strong></p>';
$html .= '<div class="description">';
$html .= $comment->comment_content;
$html .= '</div></div>';
$html .= '</li>';
}
$html .= '</ol></div></div>';
return $html;
}
add_shortcode( 'woocommerce_after_single_product', 'bbloomer_product_reviews_shortcode' );
I checked the enable review to the all products and in woocommerce to and in the discussions
You can add a new tab using the action hook woocommerce_product_tabs. check the below code. code goes in your active theme functions.php file.
add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tabs' );
function woo_custom_product_tabs( $tabs ) {
$tabs['products_review_tab'] = array(
'title' => __( 'Reviews', 'woocommerce' ),
'priority' => 120,
'callback' => 'products_review_tab_content'
);
return $tabs;
}
Now you can echo do_shortcode[] inside products_review_tab_content callback.
function products_review_tab_content() {
global $product;
echo do_shortcode( '[product_reviews_shortcode id="'.$product->id.'"]' );
}
function product_reviews_shortcode( $atts ) {
$atts = shortcode_atts( array(
'id' => ''
), $atts, 'product_reviews_shortcode' );
$comments = get_comments( array(
'post_id' => $atts['id']
) );
if ( ! $comments ) return '';
$html .= '<div class="woocommerce-tabs"><div id="reviews"><ol class="commentlist">';
foreach ( $comments as $comment ) {
$rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
$html .= '<li class="review">';
$html .= get_avatar( $comment, '60' );
$html .= '<div class="comment-text">';
if ( $rating ) $html .= wc_get_rating_html( $rating );
$html .= '<p class="meta"><strong class="woocommerce-review__author">';
$html .= get_comment_author( $comment );
$html .= '</strong></p>';
$html .= '<div class="description">';
$html .= $comment->comment_content;
$html .= '</div></div>';
$html .= '</li>';
}
$html .= '</ol></div></div>';
return $html;
}
add_shortcode( 'product_reviews_shortcode', 'product_reviews_shortcode' );
Tested and works.
I want to create a custom textrea field in Contact form 7. Were I can pre-populate it with data. I have the below which shows on the front end and creates a text area field. But when I submit the form the tag is always blank within the email so the data doesn't get emailed. The tag also does not show in the list of available tags in Contact form 7. Any ideas?
add_action( 'wpcf7_init', 'wpcf7_add_form_tag_pathtag' );
function wpcf7_add_form_tag_pathtag() {
wpcf7_add_form_tag(
array( 'pathtag', 'pathtag*'), 'pathtag_form_tag_handler', array( 'name-attr' => true )
);
}
function pathtag_form_tag_handler( $tag ) {
$tag = new WPCF7_FormTag( $tag );
if ( empty( $tag->name ) ) {
return '';
}
$atts = array();
$class = wpcf7_form_controls_class( $tag->type );
$atts['class'] = $tag->get_class_option( $class );
$atts['id'] = $tag->get_id_option();
$atts['name'] = $tag->name;
$atts = wpcf7_format_atts( $atts );
$output = '';
$output .= '<textarea cols="40" rows="10">';
foreach ( $_SESSION['question-path'] as $path ) {
$output .= $path ."\n\n";
}
$output .= '</textarea>';
return $output;
}
You did not add the attributes to the textarea code. Also, make sure you enter your shortcode in the contact form as
[pathtag field-name]
function pathtag_form_tag_handler($tag) {
$tag = new WPCF7_FormTag($tag);
if (empty($tag->name)) {
return '';
}
$atts = array();
$class = wpcf7_form_controls_class($tag->type);
$atts['class'] = $tag->get_class_option($class);
$atts['id'] = $tag->get_id_option();
$atts['name'] = $tag->name;
$atts = wpcf7_format_atts($atts);
$output = '';
// Make sure to add the $class and $atts to the form tag
$output .= sprintf('<textarea cols="40" rows="10" %s %s>', $class, $atts);
foreach ($_SESSION['question-path'] as $path) {
$output .= $path . "\n\n";
}
$output .= '</textarea>';
return $output;
}
I need to create a function that allows me to display a taxonomy via shortcode.
I try this but doesn't work:
function category_in_content($atts){
global $post;
return get_the_terms( $post, 'course_category' );
}
add_shortcode( 'catcorso', 'category_in_content' );
The taxonomy name is "course_category".
By default add_shortcode return html.
You can try the following code:
add_shortcode( 'catcorso', 'category_in_content' );
function category_in_content($atts){
global $post;
$html = '';
$taxonomy = 'course_category';
$terms = get_the_terms( $post, $taxonomy );
if ( !empty( $terms ) ) {
foreach ($terms as $term) {
$html .= '' . $term->name . '';
}
}
return $html;
}
solved with this:
function cat_title(){
global $post;
$categories = get_the_terms( $post, 'course_category' );
if ( isset( $categories[0] ) ) {
return '' . esc_html( $categories[0]->name ) . '';
}
}
add_shortcode( 'catcorso', 'cat_title' );
I need custom class for filter to wp_get_attachment_link. So what I so:
function modify_attachment_link( $markup ) {
global $post;
return str_replace( '<a href', '<a class="view" rel="galleryid-'. $post->ID .'" href', $markup );
}
add_filter( 'wp_get_attachment_link', 'modify_attachment_link' );
It's work fine. But what I need to do in case if Link thumbnails to: Attachment Page
I mean I don't need a custom class at this case. Any help please?
And core function for wp_get_attachment_link is:
function wp_get_attachment_link( $id = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false ) {
$id = intval( $id );
$_post = & get_post( $id );
if ( empty( $_post ) || ( 'attachment' != $_post->post_type ) || ! $url = wp_get_attachment_url( $_post->ID ) )
return __( 'Missing Attachment' );
if ( $permalink )
$url = get_attachment_link( $_post->ID );
$post_title = esc_attr( $_post->post_title );
if ( $text )
$link_text = esc_attr( $text );
elseif ( $size && 'none' != $size )
$link_text = wp_get_attachment_image( $id, $size, $icon );
else
$link_text = '';
if ( trim( $link_text ) == '' )
$link_text = $_post->post_title;
return apply_filters( 'wp_get_attachment_link', "<a href='$url' title='$post_title'>$link_text</a>", $id, $size, $permalink, $icon, $text );
}
So I mean if ( $permalink ) I don't need to add custom class for this function.
Try
function modify_attachment_link( $markup, $id, $size, $permalink ) {
global $post;
if ( ! $permalink ) {
$markup = str_replace( '<a href', '<a class="view" rel="galleryid-'. $post->ID .'" href', $markup );
}
return $markup;
}
add_filter( 'wp_get_attachment_link', 'modify_attachment_link', 10, 4 );
That may work
I have a custom post type in which I have custom taxonomies setup.
I want to print out the categories(custom taxonomy) that a post is included in, but exclude one. I cannot find a solution to exclude the category though.
Here is my code to output a list of the categories the custom post type is filed under:
<?php the_terms( $post->ID, 'critter_cat', 'Critter Type: ', ', ', ' ' ); ?>
How do I exclude a specific category?
Thanks.
You could create a function in your functions.php file that calls get_the_terms to return the list of terms as an array and then remove the item you don't want.
Give this a try:
function get_excluded_terms( $id = 0, $taxonomy, $before = '', $sep = '', $after = '', $exclude = array() ) {
$terms = get_the_terms( $id, $taxonomy );
if ( is_wp_error( $terms ) )
return $terms;
if ( empty( $terms ) )
return false;
foreach ( $terms as $term ) {
if(!in_array($term->term_id,$exclude)) {
$link = get_term_link( $term, $taxonomy );
if ( is_wp_error( $link ) )
return $link;
$excluded_terms[] = '' . $term->name . '';
}
}
$excluded_terms = apply_filters( "term_links-$taxonomy", $excluded_terms );
return $before . join( $sep, $excluded_terms ) . $after;
}
and then use it like this:
<?php echo get_excluded_terms( $post->ID, 'critter_cat', 'Critter Type: ', ', ', ' ', array(667)); ?>