How to stop W3 Total Cache from globally replacing URLs in Wordpress - wordpress

I'm trying to create a custom wp_head implementation in a Wordpress theme to work alongside the original method.
I've setup my code like this in functions.php:
function wp_head_r()
{
echo '<script src="http://sample-url.com/js/file.js"></script>';
}
Then in header.php, I have this:
wp_head(); //original
wp_head_r();
The problem I have is that the Wordpress install I'm working with has W3 Total Cache installed. So what is happening is that any file that has sample-url.com that references a JavaScript or CSS file is being replaced to sample-url-cdn.com before it is output to the page.
This was happening with enqueued scripts and stylesheets, and I was thinking that setting up a custom wp_head method would prevent this, but that doesn't seem to be the case.
Is it possible to create some kind of filter to prevent W3 Total Cache from globally replacing all the urls?

I managed to figure this out. The trick is to use a filter with a high priority, effectively overriding [I think] the one that W3 Total Cache is leveraging.
Here's the code [for functions.php]:
function my_filter_w3tc_cdn_url( $new_url, $url, $is_engine_mirror )
{
if(preg_match('/\/(my_special_dir_pattern)/i', $new_url))
{
$new_url = $url;
}
return $new_url;
};
add_filter( 'w3tc_cdn_url', 'my_filter_w3tc_cdn_url', 100, 3 );
I used preg_match to target all urls that fit a specific pattern, and then exclude them from being updated to the CDN url. Also, notice that I used a priority of 100, which seems high enough and worked in my specific use case.
Hope it helps.

Related

Wordpress plugin URL rewrite

I have a wordpress plugin which enables me to add events to my website.
However the url structure is e.g.
domain.com/events/cat/event-name
I was looking into the plugin, however I was not able to locate the way how the plugin reacts to that request.
Thus comes my questions:
What different ways are there for a plugin to be called via a certain url structure (like above e.g. all urls which contain "/events/cat" belong to the plugin) ?
I was looking at my .htaccess file but it was unchanged, also i was looking if the plugin uses a
add_action('parse_request', 'handler_action');
But I couldnt find anything. Are there any other ways the plugin could be using?
Thanks a lot!
Its probably using a custom post type using register_post_type function,
this will then build a new URL rewrite rule
The other possible method is by using add_rewrite_rule
something like
add_action('init', 'your_plugin_rules');
function your_plugin_rules() {
add_rewrite_rule( "events/cat/(.+)/?$", 'index.php?events=$matches[1]', "top");
}
if you check and print the value of $wp_rewrite you can see all the rewrite rules
e.g.
add_action('wp_head', function() {
global $wp_rewrite;
echo '</pre>', print_r( $wp_rewrite, 1 ), '</pre>';
});

Enable WP-postratings

I've created my own wordpress theme and installed WP-Postratings plugin, but it doesn't work. I add only
<?php if(function_exists('the_ratings')) { the_ratings(); } ?>
It show rating image, but i can't rate anything.
Should I add something to the functions, maybe the reason is ajax, should I add some function?
I'm using the same plugin on my theme
Rating Buttons
if(function_exists('the_ratings')) { the_ratings(); }
Rating Avarage
if(function_exists('the_ratings')) { echo ''.expand_ratings_template('<span class="rating-images">%RATINGS_IMAGES%</span>', get_the_ID()); }
If it doesnt work you may have any javascript conflict , try to disable all plugins and try it again.You must be sure that admin-ajax.php is not blocked by server or anything else.
You should add something to the functions. That code is enough.
It usually happened because the plugin is crashed with another plugin. Disable another plugins one by one until you can rate. Then change the plugin that make the issue with the similar one.

WordPress using different CSS - is this possible?

Bit is a basic question here but can someone confirm that this statement be confirmed: WordPress Pages (certain templates created within) can pull different CSS and JS?
Or - does WordPress only permit universal CSS + JS to be pulled across the entire site?
Thanks for clearing this up.
Depends on what plugin and themes you use. The WordPress/PHP functions wp_enqueue_style() and wp_enqueue_script() can be used literally by everyone (core, themes, plugins, you) to request WordPress to load styles or JavaSctript. You can combine this with WordPress functions to check whether the current page is something you want to filter for (post type, post, front-page, category archive, template, etc.). Here is an example to load a custom style if on front page :
if (is_front_page()) {
wp_enqueue_style('custom-frontpage', 'my/path/to/frontpage.css');
}
You will have to hook this piece of code to the wp_enqueue_script action so that WordPress executes it at the appropriate time. Here is an example using an anonymous function:
add_action('wp_enqueue_scripts', function() {
if (is_front_page())
wp_enqueue_style('custom-frontpage', 'my/path/to/frontpage.css');
});
You can also register your code as a "normal" function and pass the functions name to add_action() instead.
Edit: Enabling and disabling plugins is a bit more difficult, since you can never know how they implement their features without examining the source code. Here are my thoughts on this:
The plugin likely uses the above method (wp_enqueue_styles, wp_enqueue_scripts) to register it's styles and scripts. The plugin, since it assumes to be needed on all pages and posts, does this on every page without the conditional checking described earlier.
You could do one of the following to stop the plugin from doing this:
Identify the place where the plugin loads the styles and scripts and add the if-statement to only do so if the post-ID matches your desired post-ID. This method is bad since your changes are lost every time the plugin is updated.
Write a "counter plugin" (you could just add it to your theme or find a plugin that allowes you to add PHP to your page) that "dequeues" the style and script added by the plugin with inversed conditional tag
The counter-plugin approach would look as follows:
function custom_unregister_plugin() {
if (not the desired blog post) {
wp_dequeue_style('my-plugin-stylesheet-handle');
wp_dequeue_script('my-plugin-script-handle');
}
}
Make sure this function is executed after the enqueuing-code of your plugin by giving it a low priority in the same hook (999 is just an example, test it yourself):
add_action('wp_enqueue_scripts', 'custom_unregister_plugin', 999);
With wp_enqueue_style() you can add stylesheet (https://developer.wordpress.org/reference/functions/wp_enqueue_style/)
You can use it after detecting which template is used
function enqueue_custom_stylesheet() {
if(get_page_template() == 'contact.php')
wp_enqueue_style( 'contact-style', get_template_directory_uri().'/contact.css' );
}
add_action( 'wp_enqueue_scripts', 'enqueue_custom_stylesheet' );
You can use wp_enqueue_style for your CSS, wp_enqueue_script for your JS, wp_localize_script to pass variables from PHP to JS.
You can call these with hooks like:
funtion enqueue_my_stuff()
{
// your enqueue function calls
}
add_action('wp_enqueue_scripts','enqueue_my_stuff'); //front end
add_action('admin_enqueue_scripts','enqueue_my_stuff'); //admin panel
add_action('login_enqueue_scripts','enqueue_my_stuff'); //login screen

Calling wp_enqueue_media() in a custom theme widget on WordPress 3.5.x cause js error

I am writing a custom widget for my own WordPress theme.
From WordPress 3.5 there is a new Media Uploader instead of the old ThickBox.
My widget used to work fine on WordPress versions older than 3.5, but now the new media uploader prevent the old working behavior.
I added a check in the costructor for the presence of wp_enqueue_media function:
if( function_exists( 'wp_enqueue_media' ) ) {
wp_enqueue_media();
}
but when this part of cose is executed javascript throw an error in the console stopping Js engine:
Uncaught TypeError: Cannot read property 'id' of undefined load-scripts.php:69
I removed all the widget code and reduced it to bare bones... the error is caused by wp_enqueue_media() calls, but I cannot get my head around why and how to fix it.
I also read Wordpress 3.5 custom media upload for your theme options, but there is no mention to this issue
Can anyone point me in the right direction? Is there any documentation available for the the WordPress 3.5 Media Uploader?
It's too late for you now, but might be helpful for other people. I managed to make it work using
add_action( 'admin_enqueue_scripts', 'wp_enqueue_media' );
Hope it helps!
The problem you are experiencing is because you probably put your custom jquery in the header and you didn't registered wordpress jquery. If multiple jquery are defined you will get that error.
My sugestion is you should either remove your jquery script or remove the one from wordpress
function remove_jquery() {
wp_deregister_script('jquery');
//wp_register_script('jquery', ("//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"), false);
}
if(!is_admin()){add_action('init', 'remove_jquery');}
I suggest you use the jquery wordpress provides you, if not, the proper way to enqueue it is to deregister the default one an register your jquery. Just remove the comments from the remove_jquery function.
Also, the above code should go in functions.php
Cheers.
From codex [1], the function wp_enqueue_media( $args ) should be called from 'admin_equeue_scripts' action hook. or later.
Example:
function enqueue_media() {
if( function_exists( 'wp_enqueue_media' ) ) {
wp_enqueue_media();
}
}
add_action('admin_enqueue_scripts', 'enqueue_media');
Hope it helped.
[1]. https://codex.wordpress.org/Function_Reference/wp_enqueue_media
To debug, you need to get the non-minified versions of the js sent to the browser. See the docs:
SCRIPT_DEBUG
SCRIPT_DEBUG is a related constant that will force WordPress to use the "dev" versions of core CSS and Javascript files rather than the minified versions that are normally loaded. This is useful when you are testing modifications to any built-in .js or .css files. Default is false.
define('SCRIPT_DEBUG', true);

Wordpress plugin functions: check if function exists

I'm developing a Wordpress site that relies on a plugin to be activated for the site to function properly.
The plugin has a few useful functions that I'm using in the site's template files. When the plugin is active, everything works perfectly. If the plugin is deactivated, the content doesn't load.
Wrapping these functions in if(function_exists(...) obviously fixes that, but I'm wondering if there's a cleaner way of doing that in Wordpress. Is there a function that can be placed in the theme's functions.php file that can check if these functions are available every time I call them, and if not provide a safe fallback without me having to wrap them in the function_exists()?
Thanks.
If you're only using it sparingly (1-2 times), use if( function_exists() ). If you're calling the function several times through in different template files, I'd suggest using something like
In your functions.php
function mytheme_related_posts( $someparams = nil ) {
if( function_exists( 'related_posts' ) ) {
related_posts( $someparams );
} else {
echo 'Please enable related posts plugin';
}
}
Then use mytheme_related_posts() in your template.
I think this is the most clear way. It prevents all problems. I think you can write a function instead which can check if these functions are available every time you call them, but I'm almost sure it can cause you more trouble and it burns more memory then simply using if(function_exist()). Don't forget the else branch and it will work fine.
If you want to check if a plugin is active then you should be using the is_plugin_active() function - you can find the docs at: http://codex.wordpress.org/Function_Reference/is_plugin_active
You can then also use if(function_exists()) as well just to doubly make sure :)

Resources