1

I have a plugin whose that has a class method i want to add on to. Since this is a WordPress plugin I can't really edit their code since future updates would break my changes.

How do I add this:

alert('bar');

to the edit method below:

var builder_blvd = {
    // Lots of stuff here
    edit : function ( name, page )  {
        alert('foo');
    }
    // Lots of other stuff here
};

The result should be 2 alert messages, one for 'foo' and another for 'bar'? Is this doable or do i have to make a new copy of the entire builder_blvd class?

2 Answers 2

5
(function() {
    var original = builder_blvd.edit
    builder_blvd.edit = function(name, page) {
        original.call(builder_blvd, name, page);
        alert('bar');
    }
}());

builder_blvd.edit();

Demo: http://jsfiddle.net/xdrh5/

Sign up to request clarification or add additional context in comments.

Comments

1

The other posters have proposed some good solutions. But spyke01, you don't ever know if in the next version of the plugin, if the author might decide to do something as simple as this:

edit : function ( name, page )  {

gets changed to

edit : function ( page, name )  {

That said, all of these proposed solutions would break. You're really going to have to figure out how to manage change to others' code -- it might mean not changing it in the first place if you're not gonna be able to maintain it. Because otherwise, if you update the plug in later, you'll have to remember exactly what you did, examine code changes, and try to implement again.

Best of luck.

1 Comment

Thanks, this is a great point and I will definitely keep it in mind.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.