WordPress Plugin: Need to fire off a function immediately after the plugin is activated - wordpress

I need my plugin to run a function immediately after the plugin has been installed. The reason I need to run the function after and not during the installation is because none of the hooks work until "after" the plugin is activated and I need to do some additional install synching with a thirdparty server and I need those hooks.
So far I've found nothing that does what I want. The crons functions, from what I can tell and from what the codex says, only fire after someone visits the site. This is a "no no". The plugin cannot wait some "random" period of time. It might even be a serious security risk.

Thirlan, I have the same problem. I haven't been able to come up with a great solution, so what I'm doing is on plugin activate I'm setting a update_option and then once the settings page is visited I'm checking for the get_option to check for my one-time setting and if it's there, I fire off the function and delete_option. Now this won't exactly work for you, but... you might be able to figure out how to apply this filter:
http://adambrown.info/p/wp_hooks/hook/install_plugin_complete_actions?version=3.0&file=wp-admin/includes/class-wp-upgrader.php
or you might be able to sort of use my method. Try this:
register_activation_hook(__FILE__, 'initialize_my_function');
function initialize_my_function() {
add_option('run_my_initialization',"1");
}
add_action('admin_init', 'launch_activation_script');
function launch_activation_script() {
if (get_option('run_my_initialization') == "1") {
//Do Your Init Stuff Here
delete_option('run_my_initialization');
}
}

Can you use register_activation_hook?

Related

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.

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

WooCommerce Show Payment Gateways for Logged In Customers Only

I am setting up an ecommerce site using Wordpress and WooCommerce. We are using the wordpress member accounts to track customer information, and we need a way for logged in members only to be able to choose to purchase their cart "on credit", meaning no payment is required to place the order. Basically what I have done is hi-jacked the "Check" option (since we don't need it anywhere else) and renamed it "Credit" since it allows for the functionality we need.
However, I need a way for the "Credit" (check) option to only display if the user is logged in. Is there any way I can just "unhook" this option if the user isn't logged in? This seems like something that would be easy to do, but I couldn't find anything about it. Any help is appreciated.
The original answer to this question (from BWDesign) no longer works due to changes in WooCommerce (at least from WooCommerce 2.1.7 onwards, possibly before). This does the trick now (tested on 2.1.7 and 2.1.8), add it to e.g. your functions.php file:
add_filter( "woocommerce_available_payment_gateways", "rp_filter_gateways", 9999 );
function rp_filter_gateways($args) {
if(!is_user_logged_in() && isset($args['cheque'])) {
unset($args['cheque']);
}
return $args;
}
I just found the solution. In the class-wc-cheque.php file, the check or "cheque" (crazy brits) option is hooked using add_filter('woocommerce_payment_gateways', 'add_cheque_gateway' );. So the solution was simply to add this code to my functions.php file:
if(!is_user_logged_in()){
remove_filter('woocommerce_payment_gateways', 'add_cheque_gateway' );
}
Hope this helps!

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

Extending Contact Form 7 Wordpress plugin by using hooks

I would like to create a plugin that uses the contact form 7 hook, wpcf7_admin_after_mail. I want to use the plugin to interface with a CRM system. What I have thus far is the following:
//plugin header here
function add_to_CRM( $cf7 )
{
if (isset($cf7->posted_data["your-message"]))
{
full_contact($cf7);
} else {
quick_quote($cf7);
}
return $cf7;
}
add_action('wpcf7_admin_after_mail', 'add_to_CRM');
//other functions here
I can't seem to get this working. I can't even get the hook to work and do something like mail me. Anybody have any idea what I'm doing wrong here. Since I have limited Wordpress experience I might me missing the boat completely with what I'm trying to do here. I've Googled for answers to no end.
EDIT: I ended up adding this to the theme's functions.php file and it works perfectly. Thing is, I want to get it working as a plugin. Any help will be appreciated.
Try delaying the add_action() call, something like;
add_action('init', create_function('',
'add_action("wpcf7_admin_after_mail", "add_to_CRM");'));
This actually registers your CF7 hook once WordPress is ready (which is nearer the time functions.php gets loaded in).

Resources