How do you protect certain php files from overwriting by updates in Wordpress? - wordpress

Let's say I have made some changes on a php file from a certain plug-in. How do I prevent their lost when someone else decide to update this plug-in?

You should try to alter the plugin's behavior with a plugin of your own. But that depends on the nature of the changes you've done.
You can disable its update capabilities with the following filter:
add_filter( 'site_transient_update_plugins', 'filter_plugin_updates' );
function filter_plugin_updates( $value ) {
unset( $value->response['akismet/akismet.php'] );
return $value;
}
There are a couple of other methods to deal with this in the Answer from where I copied this code:
If I rename a plugin (in its main php file) do I still get update notifications?

Related

How to attach files to an email sent by a WordPress booking plugin?

I'm using a WordPress plugin for accepting online bookings (Appointment Hour Booking) and I need to attach a file to the emails sent after submitting the booking request (a PDF file with the general booking terms). I already applied a solution by editing the calls to the wp_mail() function in this way:
wp_mail(trim($payer_email), $subject, $message,
"From: ".$from."\r\n".
$content_type. "X-Mailer: PHP/" . phpversion(),
array(WP_CONTENT_DIR . '/uploads/agreement.pdf'));
The above works but everytime the plugin updates the file is overwritten and I've to reapply the code modification again. There is a better way to do that without being affected by the plugin updates or there is a way to prevent partially or completely a plugin update in WordPress?
Thank you in advance for any help.
Disabling the plugin update isn't a good idea, you may lost important compatibility or security updates. The way the call to the wp_mail() was modified also causes other attachment-related features stop working. The plugin you mention has a filter that can be used to modify the list of attached files, you can put the following code for example into your theme’s functions.php file:
add_filter( 'cpappb_email_attachments', 'my_attach_function', 10, 3 );
function my_attach_function( $attachments, $params, $form_id )
{
$attachments[] = WP_CONTENT_DIR . '/uploads/agreement.pdf';
return $attachments;
}
With the above code located out of the plugin files your file is added to the list of attachments without removing other attachments and locating the code out of the plugin files will prevent being overwritten by the plugin updates.
Your options are:
Fork the plugin and customize it to your needs.
Ask the team behind the plugin to implement a filter hook to allow customizing the headers passed to the wp_mail() function (so you can then attach files to e-mails).
Keep doing what you have been doing until now.
I like option two the best because:
It allows you to customize the behavior of the plugin from the outside, and,
Your changes will survive plugin updates.

How to stop W3 Total Cache from globally replacing URLs in 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.

Finding hooks Wordpress

I know there are lists of hooks for WordPress like --> http://adambrown.info/p/wp_hooks/hook
But if I want to find hooks for a plugin like WC Vendors there is a much shorter list of hooks on their website.
Are 'do_action' and 'apply filter' functions the only thing we can modify?
If given a class like --> https://github.com/wcvendors/wcvendors/blob/master/classes/admin/class-product-meta.php#L10, is there any way to modify it?
Are we limited to the do_action hooks or is there a way to modify other areas as well? Can we use the WordPress hooks to hook into the WC Vendors plugin as well?
Mostly you should try to accomplish any task with hooks, but some tasks are just not possible without actually modifying the actual code. But we all know its not good to modify core code, as all changes disappear after an update. So instead of modifying a class, you can extend it. You can override the current features and also add new ones. Extending a class is as easy as using a relavant hook in functions.php and then extending it in the same file or requiring it from another file. Here is an official tutorial on how to add a new shipping method to the woocommerce shipping class.
Sometimes you dont even need all the hooks, you just need to find the ones that are running on a specific page. For this you can use the code below to list all the current hooks.
$debug_tags = array();
add_action( 'all', function ( $tag ) {
global $debug_tags;
if ( in_array( $tag, $debug_tags ) ) {
return;
}
echo "<pre>" . $tag . "</pre>";
$debug_tags[] = $tag;
} );
Or you can use this plugin "simply show hooks"which is really helpful while development as it gives you an idea of where each hook is being triggered on the page.

how to display "There are updates available for your Custom Plugin" in wordpress

I have developed a custom wordpress plugin, many users have started using it, but now I have updates available for the plugin and want to display a message to the users who have older versions of the plugin on there site.
How can I modify the code of my plugin so that once I make updates to it, it should trigger a message to the users on the plugin dashboard that there are updates to available to your plugin.
Here is a scenario:
Say a user has version 1.0 of my plugin and the place where I host the plugin has version 1.2, how can I notify the user on his plugins page that my plugin has an updated version??
Although user3042036 answer is great, and very comprehensive, I thought I would entend his / her answer with a open source solution.
This is what you are looking for: WordPress Plugin Update Notifier
First, good practice is to create a constant for your current plugin version, and create an activation and deactivation hook for your plugin. This allows you to check things like version numbers, and do some general initialization.
define ( 'MY_PLUGIN_VERSION', '2.0.0');
register_activation_hook(__FILE__, 'my_plugin_activation'));
register_deactivation_hook(__FILE__, 'my_plugin_deactivation'));
function my_plugin_activation() {
// Initialize some stuff for my_plugin
}
function my_plugin_deactivation() {
// Welp, I've been deactivated - are there some things I should clean up?
}
Here is an example of a typical update function:
function my_plugin_activation() {
$version = get_option( 'my_plugin_version' );
if( version_compare($version, '2.0.0', '<')) {
// Do some special things when we update to 2.0.0.
}
update_option( 'my_plugin_version', MY_PLUGIN_VERSION );
return MY_PLUGIN_VERSION;
}
There is no hook for when your plugin is updated. You, as a plugin
author, have to manually check the plugin version. First, you want to
create a simple function which will tell you if your plugin is up to
date:
function my_plugin_is_current_version(){
$version = get_option( 'my_plugin_version' );
return version_compare($version, MY_PLUGIN_VERSION, '=') ? true : false;
}
Then, test if your plugin is up to date, and call your update function (or in this case we call the same function as we would if the plugin was updated!):
if ( !my_plugin_is_current_version() ) my_plugin_activation();
Testing the update process from one version to the next is not all that complicated, though it is kinda cumbersome. Maybe someone has a better way, if so please tell me!
You can’t really see any errors when you activate a plugin, so the first step is to create a very simple hook to store plugin activation errors. In this case, we store these errors in error_activation.html in the plugin folder
add_action('activated_plugin', 'my_plugin_activation_error');
my_plugin_activation_error() {
file_put_contents( plugin_dir_path(__FILE__) . '/error_activation.html', ob_get_contents());
}

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

Resources