How to remove taxonomy base from url - php

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.

Related

Customize “subsription option” string generated by WooCommerce Subscribe All The Things plugin

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 );

Referrer URL logic Wordpress

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 :-)

Making custom field appear in captions for image attachments (Wordpress)

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.

Add Note only if a certain certain field gets updated

On the admin side of Gravity form, I want to add a note if an admin user changes the status of the form entry. I have an admin only field so that bit is working and I know how to add note.
But can't work out, how to add note only if a certain field is changed. I need it to say something like 'status was updated from "approved" to "closed" by xxx'
Any help would be much appreciated.
Thanks.
add_action( 'gform_after_update_entry', function ( $form, $entry_id ) {
$current_user = wp_get_current_user();
$note = 'status updated from' . $status_from . ' to ' . $status_to . ' by ' . $current_user;
RGFormsModel::add_note( $entry_id, $current_user->ID, $current_user->display_name, $not );
}, 10, 2 );
Worked it out. For anyone who may need it. Answer below :)
add_action( 'gform_after_update_entry', 'update_entry', 10, 3 );
function update_entry( $form, $id, $original ) {
$entry = GFAPI::get_entry( $id );
$status_from = $original[ID_OF_THE_FIELD];
$status_to = $entry[ID_OF_THE_FIELD];
if($status_from != $status_to) {
$current_user = wp_get_current_user();
$message = 'Status updated from ' . $status_from . ' to ' . $status_to . ' by ' . $current_user->display_name;
RGFormsModel::add_note( $entry_id, $current_user->ID, $current_user->display_name, $message );
}
}

Wordpress Shortcode Not Displaying Results

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.

Categories