I am trying to add text through functions.php just after subscription-price">
I can do this with direct code editing, but need help to apply filter through functions.php without changing orignal files.
$option_description = apply_filters( 'wcsatt_single_product_subscription_option_description', '<span class="' . $option_price_class . ' subscription-price">Text Here' . $sub_price_html . '</span>', $sub_price_html, $has_price_filter, false === $force_subscription, $product, $subscription_scheme );
$option = array(
'class' => 'subscription-option',
'value' => $scheme_key,
'selected' => $default_subscription_scheme_option_value === $scheme_key,
'description' => $option_description,
'data' => $option_data
);
something like that
add_filter( 'wcsatt_single_product_subscription_option_description', 'add_custom_text_to_subscription_option');
function add_custom_text_to_subscription_option( $product) {
}
This should suffice, basically anything you assign to the $option_descreption variable will be displayed
Replace Your new text in this answer, depending on your needs
function filter_wcsatt_single_product_subscription_option_description( $option_description, $sub_price_html, $has_price_filter, $force_subscription, $product, $subscription_scheme ) {
// Class
$option_price_class = 'subscription-option';
// New description
$option_description = '<span class="' . $option_price_class . ' subscription-price">Your new Text ' . $sub_price_html . '</span>';
return $option_description;
}
add_filter( 'wcsatt_single_product_subscription_option_description', 'filter_wcsatt_single_product_subscription_option_description', 10, 6 );
Related
I have the following code in my child theme's functions.php file, and it works great.
function themeprefix_custom_price_message( $price ) {
global $post;
$product_id = $post->ID;
$my_product_array = array( 270,373,378,506,1306,1311,1312,1444,1445,1447,1449,1930,1932,1933,1934,1935,1963,4146,4152,4153,4154 );//add in product IDs
if ( in_array( $product_id, $my_product_array )) {
$textafter = ' Each/Min 12'; //add your text
return $price . '<span class="price-description">' . $textafter . '</span>';
}
else
{
return $price;
}
}
add_filter( 'woocommerce_get_price_html', 'themeprefix_custom_price_message' );
However, now I need to add another group of products and enable different text after the price, i.e., Each/Min 8. I have tried various changes to the above and have brought the site down each time. How do I adjust the above to add another (and then possibly another) group of product IDs with different text after the price? Thanks in advance!
This answer is for the question which was asked here: Add custom text after the price in WooCommerce
However, it can also be used in the above question.
You can try something like that if you want dynamic text after every product.
/* add custom field in single product */
add_action('woocommerce_product_options_general_product_data', function() {
woocommerce_wp_text_input([
'id' => '_custom_text_after_price',
'label' => __('Custom Text after Price', 'themedomain'),
]);
});
You can see the above changes in your product.
Now add the below code to save meta in your product
/* save meta field */
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
function woocommerce_product_custom_fields_save($post_id)
{
$woocommerce_custom_product_text_field = $_POST['_custom_text_after_price'];
if (!empty($woocommerce_custom_product_text_field))
update_post_meta($post_id, '_custom_text_after_price', esc_attr($woocommerce_custom_product_text_field));
}
And now to display custom code on the front end you need to use 2 hooks.
/* show custom text on front end */
function cw_change_product_price_display( $price ) {
global $post, $product;
if(get_post_meta( $post->ID, '_custom_text_after_price', true )){
$text = get_post_meta( $post->ID, '_custom_text_after_price', true );
}
$price .= ' '.$text;
return $price;
}
add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display' );
add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display' );
In the front end, it will look like this.
You could leave the existing array as it is. Simplify the code by removing the else from the if and then start declaring & checking new product arrays before returning the default price-only text...
function themeprefix_custom_price_message( $price ) {
global $post;
$product_id = $post->ID;
$my_product_array = array( 270,373,378,506,1306,1311,1312,1444,1445,1447,1449,1930,1932,1933,1934,1935,1963,4146,4152,4153,4154 );//add in product IDs
if ( in_array( $product_id, $my_product_array )) {
$textafter = ' Each/Min 12'; //add your text
return $price . '<span class="price-description">' . $textafter . '</span>';
}
$my_product_array2 = array( 9990,9991,9992 );
if ( in_array( $product_id, $my_product_array2 )) {
$textafter = ' Each/Min 8'; //add your text
return $price . '<span class="price-description">' . $textafter . '</span>';
}
$my_product_array3 = array( 9993,9994,9995 );
if ( in_array( $product_id, $my_product_array3 )) {
$textafter = ' Each/Min 4'; //add your text
return $price . '<span class="price-description">' . $textafter . '</span>';
}
return $price;
}
add_filter( 'woocommerce_get_price_html', 'themeprefix_custom_price_message' );
We have a referrer URL logic in Wordpress which looks like https://example.com/?ref=userid. We are trying to switch this to something like https://example.com/ref/userid. The code we use right now is:
// Ref Button
function func_register_button( $atts ) {
if(isset($_GET['ref']) && is_valid_username($_GET['ref'])){
$enter_link = 'https://example.com/sponsor/' . $_GET['ref'];
}else{
$enter_link = 'https://example.com/sign-up';
}
$a = shortcode_atts( array(
'text' => __('Yes, I want to join now')
), $atts );
$btn = '<a class="cta-btn" href="' . $enter_link . '" target="_blank">' . $a['text'] . '</a>';
return $btn;
}
add_shortcode( 'register_button', 'func_register_button' );
I am looking forward to hear your ideas and suggestions. Hope you can push us into the right direction :-)
I'm a designer (not a developer) working on a Wordpress site for a customer. Using the ACF-plugin I've set up a custom field on media files for photo credits. This works fine on featured images, where I can call it in single.php like this:
$post_thumbnail = get_post(get_post_thumbnail_id());
$credit = get_field('media_credit', $post_thumbnail);
if($credit):
echo '<div class="media-credit"><p>Photo: '.$credit.'</p></div>';
endif;
So I know the custom field works, and outputs the right data. However, I can't get it to work on image attachments in posts. What I have is this:
add_filter( 'img_caption_shortcode', 'my_img_caption_shortcode', 10, 3 );
function my_img_caption_shortcode( $empty, $attr, $content ){
$attr = shortcode_atts( array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => ''
), $attr );
if ( 1 > (int) $attr['width'] || empty( $attr['caption'] ) ) {
return '';
}
if ( $attr['id'] ) {
$attr['id'] = 'id="' . esc_attr( $attr['id'] ) . '" ';
}
//OUTPUT CREDIT
$photographer = get_field( 'media_credit', $attachment_id );
if ($photographer):$media_byline = '<br/><span class="media-credit">Photo: '.$photographer.'</span>';endif;
return '<div ' . $attr['id']
. 'class="wp-caption ' . esc_attr( $attr['align'] ) . '" '
. do_shortcode( $content )
. '<p class="wp-caption-text">' . $attr['caption'] . '' . $media_byline . '</p>'
. '</div>';
}
If I remove the if-statement in OUTPUT it shows «Photo: » within the captions, after the text like it should, but it doesn't get any data. What am I missing?
(BTW – I know there are plugins that outputs image credits, but they tend to have styles and features I have to override, resulting in a spaghetti mess I'd hate to hand over to the next guy working on this site.)
Finally got this to work! :-D Instead of using $attachment_id, I got the ID from $attr, and then stripped the 'attachment_' prefix from output.
I also made separate fields for photographer and bureau, but I guess that's beside the point.
Here is the code:
function my_img_caption_shortcode( $empty, $attr, $content ){
$attr = shortcode_atts( array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => ''
), $attr );
if ( 1 > (int) $attr['width'] || empty( $attr['caption'] ) ) {
return '';
}
$credit_id = $attr['id'];
$credit_id = str_replace( 'attachment_', '', $credit_id );
$photographer = get_field( 'media_credit', $credit_id );
$bureau_credit = get_field( 'media_bureau', $credit_id );
if ( $photographer && $bureau_credit ): $dash = ' / ';
endif;
if ( $photographer || $bureau_credit ): $media_byline = '<br/><span class="media-credit">PHOTO: '
. $photographer . ''
. $dash . '<span class="bureau-credit">'
. $bureau_credit
. '</span></span>';
endif;
return '<div id="attachment_' . $credit_id . '"'
. 'class="wp-caption ' . esc_attr( $attr['align'] ) . '" '
. do_shortcode( $content )
. '<p class="wp-caption-text">' . $attr['caption'] . '' . $media_byline . '</p>'
. '</div>';
}
add_filter( 'img_caption_shortcode', 'my_img_caption_shortcode', 10, 3 );
This solution is something I lifted from the AFC Media Credit-plugin, so credits to the developer.
I hope this is useful for anybody who wants to achieve something similar.
I have been attempting to create my own shortcode for my Wordpress theme to make life a bit easier. Unfortunately I have run into issues with my code.
Below, the code in the Javascript section is what I have placed in my functions.php file, and in the HTML section is what I have placed on my page in Wordpress as the shortcode, however nothing seems to appear?
1: JAVASCRIPT
2: HTML
function hire_equipment_func($atts) {
extract( shortcode_atts( array(
'img' => 'no-img',
'user' => 'no-user',
'text' => 'no-text',
), $atts ) );
if ( $atts['img'] == '' || $atts['user'] == '' || $atts['text'] == '' ) return;
$output =
'<div>
<div>' . $atts['img'] . '</div><br />
<div>' . $atts['user'] . '</div>
<div>' . $atts['text'] . '</div>
</div>';
return $output;
}
add_shortcode( 'hire_equipment', 'hire_equipment_func' );
[hire_equipment img="" user="Test Float" text="Can be used anywhere"]
Any help with this issue would be GREATLY appreciated!!
Kind regards,
Jesse
This will return nothing if one of the attributes is not set, and will return each attribute in a div otherwise.
function hire_equipment_func($atts) {
$atts = array_change_key_case((array)$atts, CASE_LOWER);
if (!isset($atts['img']) || !isset($atts['user']) || !isset($atts['text'])) return;
$output =
'<div>
<div>' . $atts['img'] . '</div><br />
<div>' . $atts['user'] . '</div>
<div>' . $atts['text'] . '</div>
</div>';
return $output;
}
add_shortcode( 'hire_equipment', 'hire_equipment_func' );
function hire_equipment_func($atts) {
extract( shortcode_atts( array(
'img' => 'no-img',
'user' => 'no-user',
'text' => 'no-text',
), $atts ) );
if ( $atts['img'] == 'no-img' || $atts['user'] == 'no-user' || $atts['text'] == 'no-text' ) return;
$output =
'<div>
<div>' . $atts['img'] . '</div><br />
<div>' . $atts['user'] . '</div>
<div>' . $atts['text'] . '</div>
</div>';
return $output;
}
add_shortcode( 'hire_equipment', 'hire_equipment_func' );
[hire_equipment img="" user="Test Float" text="Can be used anywhere"]
Okay, for some reason I placed the same text from each attribute (e.g. 'no-img' from attribute 'img') into each text area in the IF statement, and it worked!
So I have answered my own question! However, I am still interested in wondering WHY that works the way it did. I am willing to mark the question as ANSWERED once someone helps me in understanding the concept reason behind it.
Thanks!
Jesse
This will remove sections that are not specified, so if you toss this snippet of PHP into your functions.php file (in your child theme, preferably):
function hire_equipment_func( $atts ) {
extract(
shortcode_atts(
array(
'img' => 'no-img'
, 'user' => 'no-user'
, 'text' => 'no-text'
)
, $atts
)
);
$output = '<div>' . PHP_EOL;
if( isset( $atts[ 'img' ] ) ) { $output .= ' <div>' . $atts[ 'img' ] . '</div><br>' . PHP_EOL; }
if( isset( $atts[ 'user' ] ) ) { $output .= ' <div>' . $atts[ 'user' ] . '</div>' . PHP_EOL; }
if( isset( $atts[ 'text' ] ) ) { $output .= ' <div>' . $atts[ 'text' ] . '</div>' . PHP_EOL; }
$output .= '</div>';
return $output;
}
add_shortcode( 'hire_equipment', 'hire_equipment_func' );
and place this WordPress shortcode on your page:
[hire_equipment img="" user="Test Float" text="Can be used anywhere"]
you will get:
Test Float
Can be used anywhere
Example 2:
[hire_equipment img="/file/path/image.svg" user="Test Float" text="Can be used anywhere"]
yields
/file/path/image.svg
Test Float
Can be used anywhere
Example 3:
[hire_equipment img="/file/path/image.svg" text="Can be used anywhere"]
outputs
/file/path/image.svg
Can be used anywhere
Example 4:
[hire_equipment img="" user="" text=""]
shows
caused by that line break in your markup.
If you're wondering, PHP_EOL is a PHP constant denoting the correct 'End Of Line' symbol for your platform.
I have an shortcode that i would like it to pass an different class once a certain attribute is added to the shortcode. How do you do that? or what is the best way to do this?
Shortcode:
function one_half_columns($atts, $content = null){
$type = shortcode_atts( array(
'default' => 'col-md-6',
'push' => 'col-xs-6'
), $atts );
return '<div class="' . $type['push'] . '">' . do_shortcode($content) . '</div>';;
}
add_shortcode('one_half', 'one_half_columns');
Example when wordpress user enter [one_half type="push"] i want it to use the value of push in array col-xs-6.
You're example has a couple of problems - you are passing an argument of "type" in your shortcode, but then expecting arguments of "default" and "push" in your shortcode. What you want to do is assign the results of shortcode_atts() to $atts and then use an if statement or switch case on $atts['type'];
function one_half_columns($atts, $content = null){
// populate $atts with defaults
$atts = shortcode_atts( array(
'type' => 'default'
), $atts );
// check the value of $atts['type'] to set $cssClass
switch( $atts['type'] ){
case 'push':
$cssClass = 'col-xs-6';
break;
default:
$cssClass = 'col-md-6';
break;
}
return '<div class="' . $cssClass . '">' . do_shortcode($content) . '</div>';
}
add_shortcode( 'one_half', 'one_half_columns' );
Now when you call:
[one_half type="push"]my content[/one_half]
You should get the output:
<div class="col-xs-6">my content</div>