Trying to Remove Peculiar Query String - wordpress

I have a wordpress site using the W3 cache plugin with MaxCDN and Cloudlfare. I ran a few different speed tests and one common suggestion is to "Remove query strings from static resources". I download a plugin and cleared my cache everywhere but still got this message.
function vmf_remove_script_version( $src ) {
if ( strpos( $src, 'ver=' ) ) {
$src = remove_query_arg( 'ver', $src );
}
return $src;
}
add_filter( 'script_loader_src', 'vmf_remove_script_version', 15, 1 );
add_filter( 'style_loader_src', 'vmf_remove_script_version', 15, 1 );
After some closer inspection, turns out all the files the speedtests refer to end in ?x81224 (of which there's 50+ files). So I modified the code to the below but still no fix!
function remove_cssjs_ver( $src ) {
if( strpos( $src, '?ver=' ) ) {
$src = remove_query_arg( 'ver', $src );
}elseif( strpos( $src, '?x' ) ) {
$src = str_replace('?x81224','',$src);
//remove_query_arg( 'x', $src ); <- thought this wouldn't work because there's no = sign
}
return $src;
}
add_filter( 'style_loader_src', 'remove_cssjs_ver', 10, 2 );
add_filter( 'script_loader_src', 'remove_cssjs_ver', 10, 2 );
Does anyone have any suggestions on how I can fix this?

First of all it is NOT a good idea to remove the ?ver=... query argument. This version is usually static and should only be updated if you change a script.
Your clients save scripts in their local browser history as well as caching Plugins may save this scripts server side. If the version is updated properly the new scripts are delivered. If you remove the ver query argument outdated scripts will be delivered to your clients (maybe for a long time) which causes a lot of troubles and client complaints. Don't do it.
Furthermore there are two problems I recognize with your script:
1) The remove filter priority should be latest so that it is not overwritten by other Plugins therefore set argument 4 to 100 (https://developer.wordpress.org/reference/functions/add_filter/)
function remove_cssjs_ver( $src ) {
if( strpos( $src, '?x' ) ) {
$src = str_replace('?x81224','',$src);
}
return $src;
}
add_filter( 'style_loader_src', 'remove_cssjs_ver', 100, 2 );
add_filter( 'script_loader_src', 'remove_cssjs_ver', 100, 2 );
2) Where does ?x81224 come from? Find the Plugin/Function which creates this strange appendix and find out why. It looks like a custom made idea as replacement for the "ver" query argument.
In general I would say it is not a good idea to remove the query strings from static resources as usually there is a good reason for those. Do not know why they recommend it. If the file is cached with query string it will not slow down the site (or at least I can not think of a reason why).

Related

WPML Automatically duplicate secondary language to another secondary language (or set different fallback per language)

WPML provides an option to set each domain as a separate language. As a result, we have a German AT (for the .at) domain and a German DE (for the .de domain), for example. English is the default language and it needs to remain that way because we have many other domains for other languages and each of them includes an English (and WPML falls back to the default language if a language doesn't have a translation, which works best as English).
WPML does not provide an option to set a different Fallback language per secondary language. They do provide a way to manually duplicate from one secondary language to another, but that is time consuming each time we want to update something.
I therefore wrote the following code:
function duplicate_wpml_language_on_update( $post_id, $post ) {
global $sitepress;
if ( defined( 'ICL_LANGUAGE_CODE' ) && $sitepress->is_translated_post_type( $post->post_type ) ) {
switch (ICL_LANGUAGE_CODE) {
case 'fr':
wp_schedule_single_event( time(), 'duplicate_post', array( $post_id, $post, 'frbe' ) );
break;
case 'de':
wp_schedule_single_event( time(), 'duplicate_post', array( $post_id, $post, 'deat' ) );
}
}
}
add_action( 'save_post', 'duplicate_wpml_language_on_update', 10, 2 );
add_action( 'woocommerce_update_product', 'duplicate_wpml_language_on_update', 10, 2 );
add_action( 'wpml_pro_translation_completed', 'duplicate_wpml_language_on_update', 10, 2 );
add_action( 'icl_pro_translation_completed', 'duplicate_wpml_language_on_update', 10, 2 );
add_action( 'wpml_save_translation_data', 'duplicate_wpml_language_on_update', 10, 2 );
function duplicate_post( $post_id, $post, $lang ) {
global $sitepress;
$trid = $sitepress->get_element_trid( $post_id, 'post_' . $post->post_type );
$translations = $sitepress->get_element_translations( $trid, 'post_' . $post->post_type );
if ( isset( $translations[$lang] ) ) {
$target_post_id = $translations[$lang]->element_id;
$duplicate_of = get_post_meta( $target_post_id, '_icl_lang_duplicate_of', true );
if ( $duplicate_of ) {
// Duplicate to $lang
$target_post_id = $sitepress->make_duplicate( $post_id, $lang );
}
}
}
add_action( 'duplicate_post', 'duplicate_post', 10, 3 );
The code works when using the default wordpress editor to edit a post and duplicates it to the secondary language. When using the classic translation editor though, it doesn't work. I'm assuming that the hook is not firing. As you can see, I tried multiple hooks and it's still not working. If it is firing, I don't see any reason for it not to work. I believe the trid would be the same for all languages of a particular post, and I believe _icl_lang_duplicate_of should function the same. Could anything be incorrect about the code? If not, is there another way to do this or get it to fire when saving a post after using the WPML Classic Editor?

woocommerce - include customer-completed-order email template in plugin

I try to override the woocommerce customer-completed-order.php template via a custom plugin I build.
The most handy is to include the custom email template in the plugin, so various customers dont need to copy the template to a child theme.
So far i have (based upon internet research):
function intercept_wc_template( $template, $template_name, $template_path ) {
if ( strpos($template_name,'customer-completed-order.php' ) ) {
$template = trailingslashit( plugin_dir_path( __FILE__ ) ) . 'template/woocommerce/customer-completed-order.php';
}
return $template;
}
add_filter('woocommerce_locate_template', 'intercept_wc_template', 20, 3);
I tried to change the priority, by changing the priority to 9 instead of 20. However, that doesnt seem to help either.
Any help would be appriciated,
Kind regards, Kees
You should try this one:
add_filter('woocommerce_locate_template', 'woo_customer_completed_order_template', 10, 4);
function woo_customer_completed_order_template($template, $template_name, $template_path)
{
if ('customer-completed-order.php' === basename($template)){
$template = trailingslashit(plugin_dir_path( __FILE__ )) . 'templates/woocommerce/customer-completed-order.php';
}
return $template;
}
Note: You can change/update if ('customer-completed-order.php' === basename($template)) , cundition accordingly.
If now working above function, you can echo $template, $template_name, $template_path and update if() condition accordingly.
it's working 100% on my side.

How to set homepage based on device with Wordpress?

I need to set a default homepage based on the visitor device (mobile/desktop), I tried the following code in plugin, but did not work.
if ( wp_is_mobile() ) {
$homepage = get_page_by_title( 'mobile' );
}else{
$homepage = get_page_by_title( 'home1' );
}
if ( $homepage ){
update_option( 'page_on_front', $homepage->ID );
update_option( 'show_on_front', 'page' );
}
it keeps loading the home1, which is selected from theme options.
Thanks,
Your current functionality wouldn't work, even if it "worked". You're attempting to set a site-wide option in the database based on the most recent visitor's device.
Using update_option() isn't in your best interest here. What you should be doing is programmatically changing the template that's loaded at run-time based on the user's device, using the template_include filter - that way you're not storing a (semi) permanent change in your database, which would get constantly overwritten countless times by any user and affect all other users.
This would end up looking something like this:
add_filter( 'template_include', 'so_52745088_homepage_template', 99 );
function so_52745088_homepage_template( $template ){
// Only execute on the front page, not pages/posts/cpts
if( is_front_page() ){
// Determine if mobile
if( wp_is_mobile() ){
// Make sure mobile homepage template is found
if( $home_template = locate_template( array( 'homepage-mobile.php' ) ) ){
return $new_template;
}
}
}
return $template;
}
If you don't have a separate page template for mobile, and it's just a regular ol' separate page, then you can look at using wp_safe_redirect() on any number of hooks, a common one being template_redirect, which would end up looking like this:
add_action( 'template_redirect', 'so_52745088_homepage_redirect' );
function so_52745088_homepage_redirect( $template ){
// Only execute on the front page, not pages/posts/cpts
if( is_front_page() ){
// Determine if mobile
if( wp_is_mobile() ){
wp_safe_redirect( 'mobile' );
exit;
}
}
}

Wordpress Not Updating Stylesheet

My wordpress website is not updating the stylesheet its using instead of style.css it is showing "style.css?ver=3.8.3"
I want to know why is this happening and where does this version thingie come from since I've checked both via admin panel and the ftp and have reset cache but still its getting its styles from some other version of the styelsheet.
It would be really helpful if someone points out the logic behing this. Many Thanks!
You have to set the '$ver' parameter to 'null' in the call to style-sheet in your theme files
wp_enqueue_style( $handle, $src, $deps, $ver, $media );
Add this code to the functions.php file of your WordPress theme
function remove_cssjs_query_string( $src ) {
if( strpos( $src, '?ver=' ) )
$src = remove_query_arg( 'ver', $src );
return $src;
}
add_filter( 'style_loader_src', 'remove_cssjs_query_string', 10, 2 );
add_filter( 'script_loader_src', 'remove_cssjs_query_string', 10, 2 );
This code will remove query strings (?ver=) parameter from all the CSS and JS files on your site.
add_filter( 'style_loader_src', 'remove_cssjs_query_string', 10, 2 );
Above line removes query strings from all CSS files
add_filter( 'script_loader_src', 'remove_cssjs_query_string', 10, 2 );
Above line removes query strings from all JS files
Hope it helps :)
Source : http://belnad.com/remove-ver-query-string-wordpress-css-js-files/

Buddypress - Groups navigation / redirect to forum (this again)

If you've customized a BP site before, you'll know that the groups nav bp_get_options_nav(); can be a real barrier in getting a site exactly the way you want it.
There was an older solution to involving a permanent redirect (not ideal for SEM purposes) from home/ to forum/....
`
function redirect_to_forum() {
global $bp;
$path = clean_url( $_SERVER['REQUEST_URI'] );
$path = apply_filters( 'bp_uri', $path );
if ( bp_is_group_home() && strpos( $path, $bp->bp_options_nav['groups']['home']['slug'] ) === false )
bp_core_redirect( $path . $bp->bp_options_nav['groups']['forum']['slug'] . '/' );
}
add_action( 'wp', 'redirect_to_forum' );
`
and this one works in BP 1.5
`
function redirect_to_forum() {
global $bp;
$path = clean_url( $_SERVER['REQUEST_URI'] );
$path = apply_filters( 'bp_uri', $path );
if ( bp_is_group_home() && strpos( $path, $bp->bp_options_nav['groups']['home']['slug'] ) === false )
bp_core_redirect( $path . $bp->bp_options_nav['groups']['forum']['slug'] . 'forum/' );
}
add_action( 'bp_init', 'redirect_to_forum' );
`
Is there no other way of moving functionality around in Buddypress Groups without creating explosions? It would be really nice just to be able to change the include file references in /groups/single/home.php to pull the functionality you wanted. For example...
`
elseif ( bp_group_is_visible() ) :
locate_template( array( 'groups/single/** change this to any file within /single/ **' ), true );
`
If you change the home.php include file reference to forumn's, the forum's display just fine, however the add new topic functions and support do not seem to be dialed in... create a new topic and nothing happens... so in order to tap into forum functionality you actually need to be at the "forum" slug i.e. /forum/... is there any way of getting around this?
To summarize... I'm trying to get the forum's functionality working at the group root i.e. "sitename.com/groups/group-name/" WITHOUT a redirect to "sitename.com/groups/group-name/forumn/"
Any thoughts? suggestions? similar experiences?

Resources