Page 1 of 14 12311 ... LastLast
Results 1 to 10 of 134

Thread: Plugin to Add DONATE Link to Plugin Description

  1. #1
    chipbennett's Avatar
    chipbennett is offline WordPress Legend
    Join Date
    Feb 2009
    Location
    St. Louis, MO
    Posts
    1,992

    Default Plugin to Add DONATE Link to Plugin Description

    So, I would like to write a plugin that will pull the donate link out of readme.txt, and then display it in on the Manage Plugins page, in the list of plugin meta information (Version, Author, Plugin URL) as follows:

    The lorem ipsum plugin Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam ac urna leo. Quisque ligula neque, vulputate ac suscipit adipiscing, lacinia non nisl. Morbi facilisis porta eros, ac ornare diam pretium quis.

    Version 1.0 | By Chip Bennett | Visit plugin site | Donate
    Here's the current plugin_meta array code from plugins.php:

    Code:
      $plugin_meta = array();
      if ( !empty($plugin_data['Version']) )
       $plugin_meta[] = sprintf(__('Version %s'), $plugin_data['Version']);
      if ( !empty($plugin_data['Author']) ) {
       $author = $plugin_data['Author'];
       if ( !empty($plugin_data['AuthorURI']) )
        $author = '<a href="' . $plugin_data['AuthorURI'] . '" title="' . __( 'Visit author homepage' ) . '">' . $plugin_data['Author'] . '</a>';
       $plugin_meta[] = sprintf( __('By %s'), $author );
      }
      if ( ! empty($plugin_data['PluginURI']) )
       $plugin_meta[] = '<a href="' . $plugin_data['PluginURI'] . '" title="' . __( 'Visit plugin site' ) . '">' . __('Visit plugin site') . '</a>';
      $plugin_meta = apply_filters('plugin_row_meta', $plugin_meta, $plugin_file, $plugin_data, $context);
      echo implode(' | ', $plugin_meta);
    Two initial questions:

    1) Does information in readme.txt (specifically, "Donate URI") get added to the $plugin array, such that the Donate URI would be $plugin['DonateURI'], like Author URI is $plugin['AuthorURI'] (but that comes from the plugin's php file rather than the readme.txt)?

    2) How do I modify the above function, to add in the code to output the Donate link? I assume I have to use a function callback, but I've never done so, and the only time I tried, it failed miserably. :)
    WP TurnKey - Turn-Key WordPress installation and maintenance services
    WordPress user since 2005 | @chip_bennett | chipbennett.net | cbnet Plugins

  2. #2
    andreasnrb's Avatar
    andreasnrb is offline Kegger
    Join Date
    Jun 2009
    Posts
    594

    Default

    Perhaps like this?
    Code:
    add_filter('plugin_row_meta','your_function')
    function your_function($plugin_meta,$plugin_file,$plugin_data, $context){
    $donatelink = getDonateLink(strtolower(dirname($plugin_file)));
    if($donateLink)
    $plugin_meta[]='donatelink';
    }
    return $plugin_meta;
    }
    function getDonateLink(pluginslug){
    use http://dd32.id.au/projects/wordpress...tion-api-docs/
    }
    Last edited by andreasnrb; 02-02-2010 at 11:50 AM.

  3. #3
    chipbennett's Avatar
    chipbennett is offline WordPress Legend
    Join Date
    Feb 2009
    Location
    St. Louis, MO
    Posts
    1,992

    Default

    Quote Originally Posted by andreasnrb View Post
    Perhaps like this?
    Code:
    add_filter('plugin_row_meta','your_function')
    function your_function($plugin_meta,$plugin_file,$plugin_data, $context){
    $donatelink = getDonateLink(strtolower(dirname($plugin_file)));
    if($donateLink)
    $plugin_meta[]='donatelink';
    }
    return $plugin_meta;
    }
    function getDonateLink(pluginslug){
    use http://dd32.id.au/projects/wordpress...tion-api-docs/
    }
    Hmm... okay, can you explain what this does? Speifically, how it modifies $plugin_meta?

    Also, looking at the API reference you linked, I can't find any mention of DonateURI... maybe it's not exposed?
    WP TurnKey - Turn-Key WordPress installation and maintenance services
    WordPress user since 2005 | @chip_bennett | chipbennett.net | cbnet Plugins

  4. #4
    Otto's Avatar
    Otto is offline On The Rocks
    Join Date
    Apr 2009
    Location
    Memphis, TN
    Posts
    862

    Default

    Regarding number #2:

    PHP Code:
    add_filter('plugin_action_links_'.plugin_basename(__FILE__), 'myfunction'101);

    function 
    myfunction($links) {
      
    $mylink '<a href="whatever.php?something=something_else">Test link</a>';
      
    $links[] = $mylink;
      return 
    $links;

    Edit: My mistake, plugin_row_meta is the correct filter, like explained above. I got confused as to which link set you were talking about. action_links makes more sense for a settings link.

    Example of using plugin_row_meta correctly:
    PHP Code:
    add_filter('plugin_row_meta''donate_link'102);
    function 
    donate_link($links$file) {
        if (
    $file == plugin_basename(__FILE__)) {
            
    $donate_link '<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=otto%40ottodestruct%2ecom">Donate</a>';
            
    $links[] = $donate_link;
        }
        return 
    $links;

    Last edited by Otto; 02-02-2010 at 12:45 PM.

  5. #5
    Otto's Avatar
    Otto is offline On The Rocks
    Join Date
    Apr 2009
    Location
    Memphis, TN
    Posts
    862

    Default

    Regarding #1: The info in readme.txt is not exposed via that means. It can be retrieved via a call to plugins_api, but it's a bit crazy to go that far to do it when you can simply hardcode the link...

    Still, examine the "Changelogger" plugin if you want to see how this works.

  6. #6
    chipbennett's Avatar
    chipbennett is offline WordPress Legend
    Join Date
    Feb 2009
    Location
    St. Louis, MO
    Posts
    1,992

    Default

    Quote Originally Posted by Otto View Post
    Regarding #1: The info in readme.txt is not exposed via that means. It can be retrieved via a call to plugins_api, but it's a bit crazy to go that far to do it when you can simply hardcode the link...
    You lost me on "hardcode the link". I want to add the link to ALL installed plugins, not just one of my own.

    Still, examine the "Changelogger" plugin if you want to see how this works.
    Hmm... now that looks promising.

    *runs off to look at how Changelogger works*
    WP TurnKey - Turn-Key WordPress installation and maintenance services
    WordPress user since 2005 | @chip_bennett | chipbennett.net | cbnet Plugins

  7. #7
    andreasnrb's Avatar
    andreasnrb is offline Kegger
    Join Date
    Jun 2009
    Posts
    594

    Default

    Otto He wants to make a plugin that shows the donate link for every plugin that has written one in the readme.txt.

    Chip
    $plugin_meta is an array. $plugin_meta[]=something appends a value to the end of the list. You can check out I Make Plugins plugin for information on how you work with the API. Forgot about the easy solution below =).
    You can also just check each plugin folder for readme.txt and parse it. Then you check using file_exists if the readme.txt exists if it does you get its content using file which loads the file into an array with one line per row. The key is the linenumber the value is the read line.
    I suggest you save the pluginslug & donatelink data in an option in the database. Then you don't have to parse the readme.txt file eachtime.

    Code:
    //Havent tested it
    function getDonateLink($plugin_file){
    $donate_link=false;
    $readmeFile=WP_PLUGIN_DIR.'/'.dirname($plugin_file).'/readme.txt';
     if(file_exists($readmeFile){
        $readme=file($readmeFile,FILE_SKIP_EMPTY_LINES);
        foreach($file as $nbr => $line){
             if(strpos('link',$line)>0){
                 $dl=explode(':',$line);
                 $donate_link=$dl[1];
              }
         }
    }
    return $donate_link;
    }

  8. #8
    chipbennett's Avatar
    chipbennett is offline WordPress Legend
    Join Date
    Feb 2009
    Location
    St. Louis, MO
    Posts
    1,992

    Default

    Example of using plugin_row_meta correctly:
    PHP Code:
    add_filter('plugin_row_meta''donate_link'102);
    function 
    donate_link($links$file) {
        if (
    $file == plugin_basename(__FILE__)) {
            
    $donate_link '<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=otto%40ottodestruct%2ecom">Donate</a>';
            
    $links[] = $donate_link;
        }
        return 
    $links;

    Okay, so this much I get. Makes sense, with the plugin_row_meta filter.

    What I'm still not getting, however, is how to get DonateURI out of the plugins API. Is there a more recent reference than this one?

    I can follow what changelogger is doing, but it's using $api->sections['changelog'], which I can't emulate, unless I can figure out if/how DonateURI is exposed in the API.
    WP TurnKey - Turn-Key WordPress installation and maintenance services
    WordPress user since 2005 | @chip_bennett | chipbennett.net | cbnet Plugins

  9. #9
    chipbennett's Avatar
    chipbennett is offline WordPress Legend
    Join Date
    Feb 2009
    Location
    St. Louis, MO
    Posts
    1,992

    Default

    Quote Originally Posted by andreasnrb View Post
    Otto He wants to make a plugin that shows the donate link for every plugin that has written one in the readme.txt.

    Chip $plugin_meta is an array. $plugin_meta[]=something appends a value to the end of the list. You can check out I Make Plugins plugin for information on how you work with the API. Forgot about the easy solution below =).
    You can also just check each plugin folder for readme.txt and parse it. Then you check using file_exists if the readme.txt exists if it does you get its content using file which loads the file into an array with one line per row. The key is the linenumber the value is the read line.
    I suggest you save the pluginslug & donatelink data in an option in the database. Then you don't have to parse the readme.txt file eachtime.

    Code:
    //Havent tested it
    function getDonateLink($plugin_file){
    $donate_link=false;
    $readmeFile=WP_PLUGIN_DIR.'/'.dirname($plugin_file).'/readme.txt';
     if(file_exists($readmeFile){
        $readme=file($readmeFile,FILE_SKIP_EMPTY_LINES);
        foreach($file as $nbr => $line){
             if(strpos('link',$line)>0){
                 $dl=explode(':',$line);
                 $donate_link=$dl[1];
              }
         }
    }
    return $donate_link;
    }
    Okay, I think I follow this. It looks for the first line with the word "link", then pulls out everything in that line after the first colon (which should be the URL defined as "Donate link").

    I assume I would only want to go this route if I can't get at Donate Link through the plugins API?

    If I put the Donate Link in a database option, would I just define an array with plugin_slug and plugin_donate_uri, and then look for that first?

    And if I did so, would I need to check to see if the plugin was updated, so that I would know to check to update the saved value for plugin_donate_uri?

    Do-able, but definitely more complex than being able to use the API!

    In any case, sounds like a fun problem to tackle. :)
    WP TurnKey - Turn-Key WordPress installation and maintenance services
    WordPress user since 2005 | @chip_bennett | chipbennett.net | cbnet Plugins

  10. #10
    andreasnrb's Avatar
    andreasnrb is offline Kegger
    Join Date
    Jun 2009
    Posts
    594

    Default

    plugins API only retrieves the readme.txt and splits it into sections == Something == http://wordpress.org/extend/plugins/about/readme.txt
    There is no special donate row.
    Just add a var_dump($api->sections) in the changelogger plugin to see all it retrieves.
    I assume I would only want to go this route if I can't get at Donate Link through the plugins API?
    You would still need to parse the section that the plugin api retrieves. Its faster to first look locally for the readme.txt file than to make an request to api.wp.org. Parsing needs to be done anyway.

    If I put the Donate Link in a database option, would I just define an array with plugin_slug and plugin_donate_uri, and then look for that first?
    Yup. Store all the data in an option = one array for all plugins with the $plugin_file as key and donate url as value.

    Then you also have to make sure so you don't remove a preexisting donate link in the plugins_meta array.

Page 1 of 14 12311 ... LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •