override a function

Is there any way to override a function?

Let's say I want to customize a function in template-functions.php to fit my needs, but since the file is updated frequently, I'd have to be extra careful when updating. Is it possible to have a custom-functions.php that overrides these specific functions?

jordi

Comments

  • PHP does not allow overriding a function. The only option is to have alternate PHP files and load the one with the version of the function you want to use. If the function you want is in the core, your only choice is to clone it with a different name.
  • I see, thanks anyway sbillard!
  • WOW, a 3 year old post that is relevant.... is this still the case as the answer above is provided?

    If so when you say clone, where does the clone go and what do I edit so the system uses the clone?

    Thanks

    GW
  • acrylian Administrator, Developer
    Clone means take the function, rename it and move elsewhere. For example in a functions.php file (see the theming tutorial) in your theme or a custom plugin. Then you have of course to update your theme to use your custom function.
  • How do you replace a template_function.php function correctly?

    I know to over-ride a function in functions.php you just clone it and place it in your themes functions.php file, but what about template-functions.php?

    For example I want to remove the added styles used for front end editing, as I do not use this and having `zp_uneditable zp_uneditable_` on each text element is unnecessary. But that function is called in many places (82) so it is not just a case of clone that function and it will replace that one.

    For the moment I just edit the function in template_functions.php, but it would be good if an over-ride feature could be added.

    By that I mean if any file exists in the themes directory use that in place of the original, I am sure it could be just done by using something like the following at the top of the file,

    `
    <?php $filename = $_zp_themeroot.'/template_functions.php';
    if (file_exists($filename)){
    require_once($filename);
    } else {
    /* use the following original file */
    }
    ?>
    `
    This way if you want to make edits or hack a core file, you simply clone that file and place a copy in your theme folder. ZP then checks for that file in your theme file and if it exists it uses that in place of the original file, if not it uses the original files contents. Now the clone file will need to be a full copy of the original. Of course when you update your ZP install you will also need to check to update your themes file as well.

    Probably not the best solution but it is an option.

    Another option would be for the theme functions.php file to be used as a file that holds all edited functions that are a copy by exact name of the original function, the system then checks for that function in the themes function file before it uses the function from the original file, but I would think that would be slower than having a clone of the whole file in the theme directory as suggested above.

    Your thoughts?

    GW
  • You cannot replace a function in PHP. Instead you need to create a new function with a different name. If your really want to remove the classes you could impelent a filter to strip them. But what problem are they causing?
  • None, I am just one for wanting tidy code and to strip any unnecessary code were I can :)

    GW
  • acrylian Administrator, Developer
    Well, an attached CSS class is hardly really "code". You can always use the object model directly as well, then you get the "pure" stuff.
  • What one defines as 'code' is not needed to be debated here, I merely made a request, you gave your reply and asked a question of which I replied to.

    Thanks

    GW
  • acrylian Administrator, Developer
    Just to make clear: No offence taken or meant actually (it was sbillard's question actually..;-))
  • No problem :)

    I'm actually having a little bit of fun playing with the nightly builds and seeing what is new and of course what needs to be fixed.

    Slightly off topic, but what is the line in the sand between making a post here about an issue versus making a ticket on bugtracker - the two seem to get inter-mixed if you know what I mean.

    Cheers

    GW
  • acrylian Administrator, Developer
    Tickets are the best way so that issues are not forgotten if they are not worked on initially. Forum posts are easily buried a few days later.

    Probably we should discuss the tinyZenpage issue on the ticket from now on..;-)
Sign In or Register to comment.