Ask your WordPress questions! Pay money and get answers fast! (more info)

Add Button to Users Page (jQuery) WordPress

  • SOLVED

I need to insert a code snippet onto the Users.php page (i.e. /wp-admin/users.php)

I am trying to insert the code directly after the 'Add New' button. If you view the users.php file in wp-admin directory this is around line 434.

However, I of course can't just leave it in the file because it will be constantly overwritten by updates so I am hoping for some jquery that will insert my code snippet (or to insert with a hook if anyone knows of a way to do that). I am not even 100% sure this is possible...

Code snippet:

<?php if(isset($_POST['spue_action'])) {
if($_POST['spue_action'] == 'export_now')
{ global $spUserExport;
$message = $spUserExport->spue_daily_cronjob();
echo $message;
} } ?>
<form method="post" action="">
<p class=""><input type="submit" name="submit" value="<?php _e("Email Aging Report Now", 'spue'); ?>" class="button" /></p>
<input type="hidden" name="spue_action" value="export_now" />
</form>


Thanks for your time

Answers (4)

2013-02-27

Dan | gteh answers:

Place this inside functions.php, or something like that should do it, or put you on the right path.



add_action('user_admin_menu', 'custom_menu_item');

function custom_menu_item() {
if(isset($_POST['spue_action'])) {

if($_POST['spue_action'] == 'export_now')

{ global $spUserExport;

$message = $spUserExport->spue_daily_cronjob();

echo $message;

} } ?>

<form method="post" action="">

<p class=""><input type="submit" name="submit" value="<?php _e("Email Aging Report Now", 'spue'); ?>" class="button" /></p>

<input type="hidden" name="spue_action" value="export_now" />

</form>
<?php }






Kyle comments:

I believe that is the admin panel on the left sidebar isn't it.

Image of desired results (this is from hardcoding): [[LINK href="http://imgur.com/peN0vHm"]]http://imgur.com/peN0vHm[[/LINK]]


Dan | gteh comments:

It is. I am not aware of any hooks to insert directly into the page. I'm sure it's possible.

Mine is an alternative solution if you can't figure it out.

The only other thing I can think of is using a hook to the users table maybe, inserting the button beside each user? I know it's not ideal, but may work.


add_filter('manage_users_columns', 'pippin_add_user_id_column');
function aging_report($columns) {
$columns['aging'] = 'Aging';
return $columns;
}

add_action('manage_users_custom_column', 'aging_reports_content', 10, 3);
function aging_reports_content() {
if(isset($_POST['spue_action'])) {

if($_POST['spue_action'] == 'export_now')

{ global $spUserExport;

$message = $spUserExport->spue_daily_cronjob();

echo $message;

} } ?>

<form method="post" action="">

<p class=""><input type="submit" name="submit" value="<?php _e("Email Aging Report Now", 'spue'); ?>" class="button" /></p>

<input type="hidden" name="spue_action" value="export_now" />

</form>
<? }


Dan | gteh comments:

replace first line

add_filter('manage_users_columns', 'aging_reports');


Kyle comments:

Thank you for the tips, yeah inside the table is less desirable but I will keep it in mind.

2013-02-27

Dbranes answers:

You can try the <strong>in_admin_header</strong> or <strong>the all_admin_notices</strong> hooks:

add_action('in_admin_header','my_admin_header');
function my_admin_header(){
if($_SERVER['SCRIPT_NAME'] == "/wp-admin/users.php"){
echo "<div>hello world</div>";
}
}


but it will be above the wrap class, so this will be displayed above the h2 title.


Kyle comments:

Thanks this is closer, still not ideal but getting warmer

2013-02-27

John Cotton answers:

Hi Kyle

Have you looked at the "restrict_manage_users" action?

It's not what it was intended for, but it allows you to put a button (or whatever) next to the Change button on the bottom left of your grab. Not quite the same place as you want, but not far and - with a bit of neat CSS - you could easily space it away from the Change button and thus make it it's own man.


function my_report_button(){
echo 'My button';
}
add_action('restrict_manage_users','my_report_button');


JC


Kyle comments:

Hi John, thanks this is almost perfect!

2013-02-27

Christianto answers:

For jQuery solution:

add_action('admin_head', 'spue_user_export');
function spue_user_export(){
if($_POST['spue_action'] == 'export_now'){
global $spUserExport;
$message = $spUserExport->spue_daily_cronjob();
}?>
<script type="text/javascript">
jQuery(document).ready(function($){
if($('.users-php').length > 0){
var message = '<?php echo $message ?>',
button = $('<form method="post" action=""><p class=""><input type="submit" name="submit" value="<?php _e("Email Aging Report Now", 'spue'); ?>" class="button" /></p><input type="hidden" name="spue_action" value="export_now" /></form>');
if(message != '')
$('.wrap .subsubsub').before($('<div class="updated"><p>'+message+'</p></div>'));
$('.wrap .subsubsub').before(button);
}
});
</script>
<?php
}


Kyle comments:

Perfect, thank you :)