Skip to content

Instantly share code, notes, and snippets.

@mavieth
Created December 30, 2015 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mavieth/e49347b5b90212d546b7 to your computer and use it in GitHub Desktop.
Save mavieth/e49347b5b90212d546b7 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Clock Spring
Plugin URI:
Description:
Author:
Version: 1.0
Author URI:
*/
add_action('admin_menu', 'clockspring_functions_menu');
function clockspring_functions_menu(){
// add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );
$parent_page_title = 'Clock Spring Buttons Page';
$parent_menu_title = 'Clock Spring Buttons';
$parent_capability = 'manage_options';
$parent_menu_slug = 'cs';
$parent_function = 'cs_admin_page';
add_menu_page($parent_page_title,$parent_menu_title,$parent_capability,$parent_menu_slug,$parent_function);
$submenu_page_title = "CS Submenu Page Title";
$submenu_menu_title = "CS Submenu Title";
$submenu_capability = 'manage_options';
$submenu_slug = 'cs_sub_menu';
$submenu_function = 'cs_admin_submenu_page';
// add_submenu_page ( string $parent_slug, string $page_title, string $menu_title, string $capability, string $menu_slug, callable $function = '' )
add_submenu_page(
$parent_menu_slug,
$submenu_page_title,
$submenu_menu_title,
$submenu_capability,
$submenu_slug,
$submenu_function
);
}
function cs_admin_submenu_page() {
if (!current_user_can('manage_options')) { wp_die( __('You do not have sufficient pilchards to access this page.') ); } ?>
<div class="wrap">
<h2>Submenu Page</h2>
<?php if (isset($_POST['test_button']) && check_admin_referer('test_button_clicked')) button_clicked_function(); ?>
<form action="admin.php?page=cs_sub_menu" method="post">
<?php wp_nonce_field('test_button_clicked'); ?>
<input type="hidden" value="true" name="test_button" />
<?php submit_button('Call Function'); ?>
</form>
</div>
<?php
$users = get_general_users();
print_r($users);
echo explode("</li><li>", $users);
?>
<?php
}
function get_general_users(){
// WP_User_Query arguments
$args = array (
'orderby' => 'id',
);
// The User Query
$user_query = new WP_User_Query( $args );
$ret = array();
// The User Loop
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
// echo "<pre>" . print_r($user) . "</pre>";
array_push($ret, $user);
$nice_name = $user->user_nicename;
// echo "<br>" . $nice_name;
}
} else {
// no users found
}
return $ret;
}
// Parent Menu
function cs_admin_page() {
// This function creates the output for the admin page.
// It also checks the value of the $_POST variable to see whether
// there has been a form submission.
// The check_admin_referer is a WordPress function that does some security
// checking and is recommended good practice.
if (!current_user_can('manage_options')) { wp_die( __('You do not have sufficient pilchards to access this page.') ); } ?>
<div class="wrap">
<h2>Main Menu Page</h2>
<?php if (isset($_POST['test_button']) && check_admin_referer('test_button_clicked')) button_clicked_function(); ?>
<form action="admin.php?page=cs" method="post">
<?php wp_nonce_field('test_button_clicked'); ?>
<input type="hidden" value="true" name="test_button" />
<?php submit_button('Call Function'); ?>
</form>
</div>
<?php
}
function button_clicked_function()
{
echo '<div id="message" class="updated fade"><p>'
.'Success. ' . '</p></div>';
$pluginPath = WP_PLUGIN_DIR . '/clockspring/';
$folderName = 'exports/' . date('m-d-Y') . '/';
$filename = "Export-". date('m-d-Y_h:i:s').".txt";
$exportThis = $pluginPath . $folderName;
// create if doesnt exist
if (!file_exists($exportThis)) {
mkdir($exportThis, 0777, true);
}
// append filename
$exportThis .= $filename;
$handle = fopen($exportThis,"w");
if ($handle == false) {
echo '<p>Could not write the log file to the temporary directory: ' . $exportThis . '</p>';
}
else {
echo '<p>Log of button click written to: ' . $exportThis . '</p>';
fwrite ($handle , "Call Function button clicked on: " . date("m-d-Y_h:i:s", time()));
fclose ($handle);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment