0

I have a Custom Post Type that I'm doing some custom work for. I want to attach some custom functionality to the post type once it's created but an admin can only trigger the custom action from the dashboard.

For Example

You create a new Post of the custom type and after it's created you can go to edit the post and in there you see a button that says "Custom Function" which will run some javascript.

The Idea

I need the custom button to run a function in functions.php which will create X amount of entries in a custom table on the mysql database and then return the values. I'll use an input next to the button to decide X's value.

I'll be fine with the php + sql + ajax but I just don't know a non-hack way to get the custom inputs to show up on the edit page. Any ideas?

2

Add a meta box to your custom post type edit screen to contain your markup. Check the $pagenow global to only show it on post.php, not post-new.php, and check $current_user global for administrator role to exclude other user roles.

Also, if you aren't familiar, check out the $wpdb class for your queries, and read up on using AJAX in WordPress.

EDIT use get_current_screen and current_user_can rather than dirty, dirty globals, as @TheDeadMedic suggested in comment below.

  • 2
    I would advocate get_current_screen()->id and current_user_can( 'cap' ) over globals. – TheDeadMedic Apr 24 '15 at 18:11
  • @TheDeadMedic - you're right, I was thinking meta boxes were added on admin_init for some reason, which is too early for get_current_screen, but I'm wrong there. – Milo Apr 24 '15 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.