WordPress – Add links to plugin description in plugin list

In the plugin list ‘/wp-admin/plugin.php’ it is nice to add links to go to settings or management pages for people who just installed the plugin. They can find the pages under the plugin description ¬†without searching through the (long) list of menu links on the admin page.

The benefit is that users see the options, domcumentation or other links from the moment they installed the plugin.

information under plugin description

Two places for links

There are two places for links in the plugin list.

  1. Right under the name – this is where the (de)activate, edit and delete links are.
  2. Under the description – this is where the version number, author and plugin site link are displayed.

Right under the plugin name

The links right under the plugin name should be direct plugin actions. Clear the cache, run the scan etc. The filter for these links is: ‘plugin_action_links{$your_plugin_name}’. WordPress codex link

<?php

add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), 'add_action_links' );

function add_action_links ( $links ) {
 $mylinks = array(
 '<a href="' . admin_url( 'options-general.php?page=myplugin' ) . '">Settings</a>',
 );
return array_merge( $links, $mylinks );
}

Under the plugin description

The links under the description can be more generic and point to external sources or plugin links in the page. The standard links are made by a good header requirements, but you can append custom links to the list. WordPress codex link

<?php

add_filter( 'plugin_row_meta', 'custom_plugin_row_meta', 10, 2 );

function custom_plugin_row_meta( $links, $file ) {

	if ( strpos( $file, 'plugin-file-name.php' ) !== false ) {
		$new_links = array(
			'<a href="donation_url" target="_blank">Donate</a>'
			);
		
		$links = array_merge( $links, $new_links );
	}
	
	return $links;
}

Object orientated notation

You can use these codes in object orientated programming. Make the plugin public and use an array to point to the method.

<?php
class your_plugin_object 
{

    public function __construct()
    {
        // Initialise actions
        add_action('init', [$this, 'init']);
    }

    public function init()
    {
        add_filter( 'plugin_row_meta', [$this, 'custom_plugin_row_meta'], 10, 2 );
    }

    public function custom_plugin_row_meta( $links, $file ) 
    {
        if ( strpos( $file, 'plugin-file-name.php' ) !== false ) {
	    $new_links = array(
               '<a href="donation_url" target="_blank">Donate</a>'
			);
		
		$links = array_merge( $links, $new_links );
	}
	
	return $links;
    }
}
    // Do not forget to initiate the object
    if( class_exists( 'your_plugin_object' ) ) {
        $your_plugin = new your_plugin_object;
    }

 

 

One Comment

  • Leave a Reply

    Your email address will not be published. Required fields are marked *