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 :-)
Related
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 );
I try to change a class, which is stored in a variable without making any changes in the plugin file. I'm not sure how to target the variable inside the function.
This is the function: function learndash_mark_complete( $post, $atts = array() ) { Inside this function, there is a variable called $button_class.
if ( isset( $atts['button']['class'] ) ) {
$button_class = ' class="learndash_mark_complete_button ' . esc_attr( $atts['button']['class'] ) . '" ';
} else {
$button_class = ' class="learndash_mark_complete_button" ';
}
Is there a simple way to change the code to..
if ( isset( $atts['button']['class'] ) ) {
$button_class = ' class="myclass' . esc_attr( $atts['button']['class'] ) . '" ';
} else {
$button_class = ' class="myclass" ';
}
without change the plugin file. Maybe to create a function in functions.php or in a seperate (new) plugin?
I saw articles on the internet about add_filter, but I'm not sure how to target the part I need without wipe the whole function.
I know I could change the class with jQuery or something, and maybe it should be a lot easier, but I like to learn new things.
If plugin does not provide filter hook to change the class name then you can not you filter value.
But, if you are writing your own plugin then you can use this type of logic to change the value without touching original source.
This code should be in the plugin file,
$arrayToFilter = array(
'class_name' => 'learndash_mark_complete_button',
'key1' => 'value1', // So on
);
$value = apply_filters( 'change_class', $arrayToFilter );
if ( isset( $atts['button']['class'] ) ) {
$button_class = ' class="'.$value['class_name'].'' . esc_attr( $atts['button']['class'] ) . '" ';
} else {
$button_class = ' class="'.$value['class_name'].'" ';
}
And add this filter into theme functions.php file to modify change_class hook's value
function example_callback($args) {
// Here you can get $arrayToFilter as $args
$args['class_name'] = 'myclass';
return $args;
}
add_filter( 'change_class', 'example_callback',10 , 1 ); // Where $priority is 10, $accepted_args is 1.
I hope you got basic idea of add_filter().
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'm basically trying to remove taxonomy bases from my urls e.g from this:
http://www.example.com/posttype/tax/term1/tax/term2
To this:
http://www.example.com/postype/term1/term2
I've read many posts stating that this is impossible to achieve. However I've come across many sites that have achieved this. So I'm asking this question in hope that someone out here has managed to achieve this.
My rewrite code:
function add_query_vars( $query_vars ) {
$new_vars = array( 'vehicle-type', 'fuel', 'color', 'status', 'gearbox', 'interior', 'exterior', 'safety', 'extra', 'location', 'vehicle-year', 'model' );
return array_merge( $new_vars, $query_vars );
}
add_filter( 'query_vars', 'add_query_vars' );`
function add_rewrite_rules() {
global $wp_rewrite;
$new_rules = array(
'vehicles/vehicle_status/(.+?)/vehicle_fuel_type/(.+?)/vehicle_color/(.+?)/vehicle_status/(.+?)/vehicle_gearbox/(.+?)/vehicle_interior_feature/(.+?)/vehicle_exterior_feature/(.+?)/vehicle_safety_feature/(.+?)/vehicle_extra/(.+?)/vehicle_location/(.+?)/vehicle_year/(.+?)/vehicle_model/(.+?)/?$' => 'index.php?post_type=vehicles&vehicle_status=' . $wp_rewrite->preg_index(1) . '&vehicle_fuel_type=' . $wp_rewrite->preg_index(2) . '&vehicle_color=' . $wp_rewrite->preg_index(3) . '&vehicle_status=' . $wp_rewrite->preg_index(4) . '&vehicle_gearbox=' . $wp_rewrite->preg_index(5) . '&vehicle_interior_feature=' . $wp_rewrite->preg_index(6) . '&vehicle_exterior_feature=' . $wp_rewrite->preg_index(7) . '&vehicle_safety_feature=' . $wp_rewrite->preg_index(8) .'&vehicle_extra=' . $wp_rewrite->preg_index(9) .'&vehicle_location=' . $wp_rewrite->preg_index(10) . '&vehicle_year=' . $wp_rewrite->preg_index(11) . '&vehicle_model=' . $wp_rewrite->preg_index(12)' ) ;
}
add_filter('rewrite_rules_array', 'add_rewrite_rules');`
Go to settings -> permalinks in wordpress panel. Select permalink structure as you want.
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>