Skip to main content

Custom WordPress Plugin Tips & Tricks (Settings Link, Default Settings, Redirect To Settings, Admin Notice)

By July 25, 2017Blog, Plugin, Wordpress
WordPress Tutorials by Allure Web Solutions

Here are several WordPress tips and tricks for when you are developing your own custom plugins. All of these make for a pretty good user experience. This page will be updated as I get new ideas!

  1. Add Settings Link
  2. Set Default Settings on Plugin Activation
  3. Redirect to Settings Page on Activation
  4. Create Admin Notice
/**
 * Add settings link to plugin page
 *
 * @param $links
 * @return mixed
 */
public function add_settings_link($links)
{
    $settings_link = '<a href="options-general.php?page=wp-post-modal">' . __('Settings') . '</a>';
    array_push($links, $settings_link);
    return $links;
}

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

If you are using the WordPress Plugin Boilerplate, your add_filter() function would look like: $this->loader->add_filter('plugin_action_links_' . plugin_basename(plugin_dir_path(__DIR__) . $this->plugin_name . '.php'), $plugin_admin, 'add_settings_link');

Set Default Settings on Plugin Activation

This is an example for setting a checkbox to be checked by default.

Assuming code for rendering checkbox looks something like this: <input type="checkbox" name="checkbox" id="checkbox" value="1" <?php echo checked($styling, '1'); ?> />

register_activation_hook( __FILE__, 'myplugin_plugin_activate' );
function myplugin_plugin_activate() {
    if (get_option('wp_post_modal_styling') === false || get_option('wp_post_modal_styling') === '')
        update_option('wp_post_modal_styling', '1');
}

Replace OPTION_NAME with appropriate name of your option and on update_option()  update true  to your content for the option as needed.

Redirect to Settings Page on Activation

register_activation_hook( __FILE__, 'myplugin_plugin_activate' );
function myplugin_plugin_activate() {
    // redirect to settings page after plugin activation
    global $pagenow;
    if (isset($_GET['activated']) && $pagenow == 'plugins.php') {
        wp_redirect(admin_url('options-general.php?page=PLUGIN_NAME'));
        exit;
    }
}

Replace PLUGIN_NAME or the whole URL options-general.php?page=PLUGIN_NAME with the appropriate URL of your settings page.

Create an Admin Notice

add_filter('admin_notices', 'admin_notice');
add_filter('network_admin_notices', 'admin_notice'); // also show message on multisite
public function admin_notice()
{
    $message = '<h4>Thanks for installing WP Post Popup!</h4>';
    ?>
    <div class="notice notice-success is-dismissible">
        <?php echo $message; ?>
        <button type="button" class="notice-dismiss">
            <span class="screen-reader-text">Dismiss this notice.</span>
        </button>
    </div>
    <?php
}
allure

Author allure

More posts by allure

Leave a Reply

Designed by

best down free | web phu nu so | toc dep 2017