Skip to content

Instantly share code, notes, and snippets.

@pmgllc
Last active December 16, 2015 15:09
Show Gist options
  • Save pmgllc/5453550 to your computer and use it in GitHub Desktop.
Save pmgllc/5453550 to your computer and use it in GitHub Desktop.
Trying to add a "settings" link to my plugin. This is after looking at 5-10 plugins with a simple method that didn't work to now using what is in YOURLS and bbPress-Members Only: http://plugins.svn.wordpress.org/bbpress-members-only/trunk/init.php Still no link appears and die('foo'); at any point along here does show the function is being read.
<?php
// Start up the engine
final class SLPCreator {
static $instance;
function __construct() {
self::$instance =& $this;
// Actions
add_action( 'init', 'slp_load_plugin_translations', 1 );
// Filters
add_filter( 'plugin_action_links', array( $this, 'admin_settings_link' ), 10, 2 );
}
public function admin_settings_link( $links, $file ) {
if (!$this_plugin) {
$this_plugin = plugin_basename(__FILE__);
}
if ($file == $this_plugin) {
$settings_link = '<a href="' . admin_url( 'options-general.php?page=stealth-login-page' ) . __( 'Settings', 'stealth-login-page' ) . '</a>';
array_unshift( $links, $settings_link );
}
return $links;
}
}
@jaredatch
Copy link

What Line 16-18 for? Where would $this_plugin even be set? Why not just delete those lines and change L20 to plugin_basename(__FILE__) == $file?

@jaredatch
Copy link

Few issues.

1 - You are never calling the class, so the code isn't executing. You're missing new SLPCreator; :)
2 - The stuff checking for $this_plugin can be nuked
3 - There is an error when you are compiling the $settings_link. The href tag isn't getting closed.
4. For something that is this simple, I wouldn't use a class. The rest of your plugin isn't classed base so I wouldn't do it for this either.

Fixed:

/**
 * Add settings link on plugin page
 *
 * @since 1.x.x
 * @param array $links
 * @param string $file
 * @return array
 */
function slp_admin_settings_link( $links, $file ) {

  if ( plugin_basename(__FILE__) == $file ) {
    $settings_link = '<a href="' . admin_url( 'options-general.php?page=stealth-login-page' ) . '">' . __( 'Settings', 'stealth-login-page' ) . '</a>';
    array_unshift( $links, $settings_link );
  }

  return $links;

}
add_filter( 'plugin_action_links', 'slp_admin_settings_link', 10, 2  );

/**
 * Load translations for this plugin
 *
 * @since 1.x.x
 */
function slp_load_plugin_translations() {

  load_plugin_textdomain( 'stealth-login-page', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );

}
add_action( 'init', 'slp_load_plugin_translations', 1 );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment