Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I always do changes on the Wordpress plugins for clients. But these changes are always in danger to be lost if the plugin is updated.

Is there is a way to make a plugin for another plugin in Wordpress? Is there is a way to preserve the changes or re-apply it after each plugin update.

share|improve this question
    
What you need to do? DB update? File change? plz tell me more.. –  Yakir Sitbon Mar 12 '12 at 11:40
1  
i think it depends on how the plugin is built. if it is a class you could extend it.... i've done that before. if it has hooks/filters you can override certain things. –  helgatheviking Mar 12 '12 at 11:45
    
See below for my answer. I would use Wordpress hooks, but define your own in your plugin that you can hook into using other plugins. –  DigitalSea Mar 12 '12 at 12:37
    
DB is not a problem, files is the big problem. and it is not my plugins to add actions and hooks to it but they have classes. I think i can extend it but i am not that good with OOP yet. –  alhoseany Mar 12 '12 at 12:56
add comment

3 Answers

up vote 3 down vote accepted

I think best way to do this is via actions and filters, like we extend WordPress core itself.

Other options is like @helgatheviking pointed, if plugin is class you can extend it.

Unfortunately not all plugin developers provide useful filters and actions with their code, most often plugin isn't written in OOP manner. Only way to save your modifications on plugin update, is to create copy of original plugin, change plugin name. I usually prefix original name e.g. Mamaduka Twitter Connect, but with this solution you'll need to manually update original plugin's code.

If you think that plugin needs more filters/action, you can contact the author and ask him to include those hooks into the core.

share|improve this answer
    
i just got another insane idea, is it possible to know when a plugin is finished updating and then rewrite the old files that i changed back into the plugin folder? also, any example on extending a plugin class in another plugin. thanks –  alhoseany Mar 12 '12 at 12:59
    
If you rewrite all those files back, then updating loses it meaning, also it can break plugin if author did some changes in that files and they're used some other places. Can't remember any of them right now, but in general it can be done. –  Mamaduka Mar 12 '12 at 13:08
    
i can only rewrite the files that i add to the plugins, like templates for example –  alhoseany Mar 13 '12 at 9:46
add comment

One simple way would be to define custom hooks within your plugin that you can hook onto. The in-built hooks system allows for you to create your own hooks and then bind onto them like normal Wordpress hooks. The Wordpress Codex has great examples and explanations of the do_action function and how you can use it to create custom hooks. A seriously overlooked feature in Wordpress by plugin and theme developers.

I am a firm believer that the hooks system is all you need to develop plugins that can be extended by other plugins, but as I said seriously overlooked by 90% of all Wordpress developers.

See below for an example (taken from the Wordpress Codex link provided):

<?php 
# ======= Somewhere in a (mu-)plugin, theme or the core ======= #

/**
 * You can have as many arguments as you want,
 * but your callback function and the add_action call need to agree in number of arguments.
 * Note: `add_action` above has 2 and 'i_am_hook' accepts 2. 
 * You will find action hooks like these in a lot of themes & plugins and in many place @core
 * @see: http://codex.wordpress.org/Plugin_API/Action_Reference
 */

// Define the arguments for the action hook
$a = array(
     'eye patch' => 'yes'
    ,'parrot' => true
    ,'wooden leg' => (int) 1
);
$b = 'And hook said: "I ate ice cream with peter pan."'; 

// Defines the action hook named 'i_am_hook'
do_action( 'i_am_hook', $a, $b );

# ======= inside for eg. your functions.php file ======= #

/**
 * Define callback function
 * Inside this function you can do whatever you can imagine
 * with the variables that are loaded in the do_action() call above.
 */
function who_is_hook( $a, $b )
{
    echo '<code>';
        print_r( $a ); // `print_r` the array data inside the 1st argument
    echo '</code>';

    echo '<br />'.$b; // echo linebreak and value of 2nd argument
} 
// then add it to the action hook, matching the defined number (2) of arguments in do_action
// see [http://codex.wordpress.org/Function_Reference/add_action] in the Codex 

// add_action( $tag, $function_to_add, $priority, $accepted_args );
add_action( 'i_am_hook', 'who_is_hook', 10, 2 );  

# ======= output that you see in the browser ======= #

Array ( 
    ['eye patch'] => 'yes'
    ['parrot'] => true
    ['wooden leg'] => 1
) 
And hook said: "I ate ice cream with peter pan."
share|improve this answer
1  
+1 for taking the Codex do_action example :) –  kaiser Mar 12 '12 at 14:37
add comment

FWIW, you can increase the plugin version number so that it doesn't auto update. while its not the best solution, it'll solve the immediate problem. You could also change the naming and filenames so that they aren't the "same" plugin anymore.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.