Last active
May 4, 2016 13:11
-
-
Save glueckpress/26b791ab52d5c197ef0c to your computer and use it in GitHub Desktop.
[WordPress] Mini plugin to display a random admin notice in the back-end. 📢 Use URL parameters to control count and type. Handy test tool for https://wordpress.org/plugins/wp-notification-center/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Notifications FFS! | |
* Description: Sample admin notices. Use URL parameters <a href="/wp-admin/plugins.php?nffs_count=2&nffs_type=random">(sample)</a> to control count and type: <code>nffs_count=1(,2…)</code>, <code>nffs_type=random(|success|error|warning|info)</code> | |
* Version: 0.2 | |
* Author: Caspar Hübinger | |
* Author URI: https://profiles.wordpress.org/glueckpress | |
* License: GNU General Public License v3 | |
* License URI: http://www.gnu.org/licenses/gpl-3.0.html | |
* Requires PHP: 5.3.0 | |
* Requires WP: 4.3 | |
*/ | |
if( ! defined( 'ABSPATH' ) ) | |
exit; | |
/** | |
* Displays a random notification in the backend. | |
* @return void | |
*/ | |
function notifications_ffs() { | |
$i = 1; | |
$count = notifications_ffs__get( 'nffs_count' ); | |
while ( $count >= $i ) { | |
add_action( 'admin_notices', function () { | |
$type = notifications_ffs__get( 'nffs_type' ); | |
$hell_ya = (array) notifications_ffs__gimme_notifications( $type ); | |
printf( '<div class="%2$s notice"><p>%1$s</p></div>', $hell_ya[ 'msg' ], $hell_ya[ 'class' ] ); | |
} ); | |
$i++; | |
} | |
} | |
add_action( 'plugins_loaded', 'notifications_ffs' ); | |
/** | |
* Random notification message. | |
* @return array Class name and text for message | |
*/ | |
function notifications_ffs__gimme_notifications( $type = 'random' ) { | |
$messages = array( | |
'success' => array( | |
'class' => 'notice-success', | |
'msg' => 'Something worked well. <a href="https://www.youtube.com/watch?v=vnDTSq3HmNo" target="_blank">Treat yourself</a>' | |
), | |
'error' => array( | |
'class' => 'notice-error', | |
'msg' => 'Something is BROKEN! <a href="http://glueckpress.com/?show_me=💩" target="_blank">Show me</a>' | |
), | |
'warning' => array( | |
'class' => 'notice-warning', | |
'msg' => 'Something <em>may</em> break. <a href="http://i.giphy.com/yEX1HlKiuIJxe.gif" target="_blank">Watch out</a>' | |
), | |
'info' => array( | |
'class' => 'notice-info', | |
'msg' => 'You might want to know this. <a href="https://www.google.de/#q=random+facts" target="_blank">Learn more</a>' | |
), | |
); | |
if ( 'random' === $type ) { | |
shuffle( $messages ); | |
$pointer = mt_rand( 0, count( $messages ) - 1 ); | |
return $messages[ $pointer ]; | |
} | |
return $messages[ $type ]; | |
} | |
/** | |
* Handle GET URL paramters. | |
* @param string $param Parameter key | |
* @return mixed Paramter value | |
*/ | |
function notifications_ffs__get( $param ) { | |
if ( 'nffs_count' === $param ) | |
return isset( $_GET[ $param ] ) ? absint( $_GET[ $param ] ) : 1; | |
if ( 'nffs_type' === $param ) | |
return isset( $_GET[ $param ] ) ? esc_attr( $_GET[ $param ] ) : 'random'; | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment