Force date of item in RSS feed

Hi!

I would like to force the tag value as the current date in my custom RSS feed (to trigger every day a random image posting).

I have found the getItemGallery() function in class rss_options. If I hack the function in rss.php, it works:

//date
if ($this->mode != "albums") {
//$feeditem['pubdate'] = date("r", strtotime($item->getDateTime()));
$feeditem['pubdate'] = date("r");
} else {
//$feeditem['pubdate'] = date("r", strtotime($albumobj->getDateTime()));
$feeditem['pubdate'] = date("r");
}
return $feeditem;

To modify the function only in my theme and keep the core clean, I have copied the getItemGallery() in my functions.php. But it does not work. What am I missing? I have tried to declare it in the class

class rss_options {
function getItemGallery( $item ) {
...}
}

Thanks for help!

Tags:

Comments

  • acrylian Administrator, Developer

    I don't understand why you want to do this but that's not important ;-)

    It does not work because you use the wrong class and also you did it wrong. You redeclared the rss_options class which is the wrong class and also should have caused a fatal error).

    'getItemGallery()is a method of theRSSclass. So the correct way to create your custom one is to create a child class (myRSS` is an example class name):

     class myRSS extends RSS {
    
          function getItemGallery( $item ) {
          …
          }
    
     } 
    

    But that also will not exactly do the trick since the old class will be used to setup the feed.

    You have to also create a child class method executeRSS() and then attach it to the filter load_theme_script. See the bottom of the rss.php. Before doing so you need to remove the original function from the filter so not both are used and casue conflicts.
    I recommend to do all this in a custom plugin which you need to make sure is loaded after the orignal one (see the number within $plugin_is_filter at the beginning) as both need to be active since the original RSS class must also be loaded.

  • Thanks for the great help, appreciated. I will start my own plugin then. I have never developed one, it is a good opportunity ;-)

    I can see that a demo plugin exists in the release https://www.zenphoto.org/news/demo-plugin/ to kickstart but I can't find it. Do you know where it is today?
    Or is there a similar plugin that replaces a function somewhere I could be inspired?

    You wrote:
    "Before doing so you need to remove the original function from the filter so not both are used and cause conflicts."

    How can I do that specifically in the RSS class?
    Would it be something like that:
    zp_remove_filter('load_theme_script','RSS::getItemGallery');

    Cheers

  • Btw the objective is to trigger email sending (photo of the day) every day using mailchimp as the pubDate will always be the date of the current day for any random item...

  • acrylian Administrator, Developer
    edited November 2020

    Thanks about the note regarding the demo plugin. It is not included actually but apparenlty there is a bug in our site theme not showing the link:
    https://github.com/zenphoto/demo-plugin

    There is no actualy guide to replace a function ( In this case we are speaking of methods as they are part of a class) as this is PHP basics of object inheritance and nothing Zenphoto specific.

    zp_remove_filter('load_theme_script','RSS::getItemGallery');

    Yes, that would be the correct way to remove the filter. It needs to be done right at the beginning of your plugin. Right afterwards you have to register your replacement so it is used.

    Alternative would be to copy the RSS plugin itself, rename it and modify itself. Of course then you have to see that you get all possible updates later yourself.

  • OK, great!

    Is the number of $plugin_is_filter in my custom plugin must higher or lower than the one of RSS plugin (to be loaded after the original class) ?

    I have setup this at the beginning and copy the executeRSS method:
    zp_remove_filter('load_theme_script','RSS::getItemGallery');
    zp_register_filter('load_theme_script', 'RSSDate::executeRSS', 1);

    Is the integer of zp_register_filter important too?

    Thanks

  • acrylian Administrator, Developer

    Is the number of $plugin_is_filter in my custom plugin must higher or lower than the one of RSS plugin (to be loaded after the original class) ?

    The higher he number the more priority, so lower than the original.

    Is the integer of zp_register_filter important too?

    That priortiy parameter should not matter in this case as the plugin itself here already sets the priority order.

  • Thanks @acrylian ! It works ;-) I have just have to wait tomorrow if mailchimp will be triggered via RSS to send emails with latest dates of random photo every day.

    I have found what I was missing: to instantiate my object in my class in executeRSS():

    static function executeRSS() {
    if (!$_GET['rss']) {
    $_GET['rss'] = 'gallery';
    }
    $_zp_gallery_page = 'rssDate.php';
    $rss = new RSSDate(sanitize($_GET));
    $rss->printFeed();
    exitZP();
    }

    I definitely improved my ZP xp ;-)

  • acrylian Administrator, Developer

    Great it worked out. And yes, of course executeRSS() has to create the object of your child class. Sorry if I forgot to mention that as that was just "obvious" to me.

  • Hi,

    It wasn't for me ;-), thanks.

    Now, to be clean, I would like to extend/redefine my 2 customized functions "printRSSLink" and "getRSSLink" in my plugin instead of copy/modified and renamed them in functions.php of my theme. But I cannot succeed to integrate them in my plugin. I have tried to apply same logic: Remove them at the beginning and redefine under my new class but it does not work for these 2.

    It seems that these 2 functions are not part of the RSS class as it is for getItemGallery. Do you know if it is possible to override these 2 too in same plugin?

  • acrylian Administrator, Developer
    edited December 2020

    These two are procedural functions as a lot in Zenphoto is procedural. You cannot override procedural functions as that leads to fatal errors because of re-declaring.

    You have to create new unctions with a new name within your plugin and use them instead.

Sign In or Register to comment.