How to update plugin without overwrite custom code

I am using horizontal-scrolling-announcement plugin to announce a news bellow the header.I have added some custom code to hide scrolling news announcement at one event if the admin change the status display no and its working .Before the customize the plugin if i set the status display no it was scrolling news with text “No announcement available.”. Now the problem is that if the plugin updated automatically my custom will be overwrite with new code i just wanted know that how can i update the plugin without overwrite the my previous custom code.

Bellow is the function which i have added some custom code to hide scrolling.

<?php
function HSA_shortcode( $atts ) 
{
if ( ! empty($data) ) 
    {
// Scroll new announcement 

}
else   //Hide scrolling bar

    { 
    ?> <style>

.breaking-news{
visibility:hidden;
height:0;
width:0;
opacity:0; 

}

</style>
        /*$what_marquee = $what_marquee . "No announcement available.";
        if($group <> "")
        {
            $what_marquee =  $what_marquee . " Please check this group " . $group;
        }  */
    <?php } 

    return $what_marquee;
}

1 Answer
1

i just wanted know that how can i update the plugin without overwrite
the my previous custom code.

You don’t.

Well, you do the same thing over and over and over again every time you want to update.

  1. Download the plugin.
  2. Hack your changes into it.
  3. Upload to your site.
  4. Repeat.

Which is why you don’t edit plugins that you didn’t write and don’t maintain.

The plugin might contain an action or a filter that you can use, but I don’t know about this particular plugin.

There is another option in your case, I am pretty sure. Your function name is HSA_shortcode so I am assuming this is a shortcode callback. Shortcodes can be hijacked. Try:

function my_first_shortcode($atts) {
  return 'howdy';
}
add_shortcode('hijack','my_first_shortcode');
echo do_shortcode('[hijack]');

function my_second_shortcode($atts) {
  return 'hijacked !';
}
add_shortcode('hijack','my_second_shortcode');
echo do_shortcode('[hijack]');

If you can find the original shortcode slug, you can put your function (probably with a different name) in your theme’s functions.php or another plugin and hijack the orginal shortcode.

function my_HSA_shortcode( $atts ) {
  // ...
}
do_shortcode('origslug','my_HSA_shortcode');

This, of course, will work only until the plugin author changes the shortcode “slug”, but even then the maintenance hassle is greatly reduced.

Leave a Comment