1

I am writing a WP plugin. I have created a button for the user to click in thw WP dashboard. I need to run a function when a user clicks the button, shown with a red arrow.Button in WP dashboard The code snippet for the button is:<p><button class="button button-primary">Update Media Titles and ALT Text</button></p>. I have already created the function in my class like so:

public function kh_update_media_seo() {

    //update media files title and alt tags here
}

I can handle the code that goes in the function alone, I only need help making the button in the WP dashboard fire off this specific function when clicked.

Pardon me if this sounds dump or straight forward. It's my first time doing this.

My plugin is a one file plugin if that helps.

1

Create form or link with action="my_media_update"

<form action="<?php echo admin_url('admin-post.php'); ?>" method="post">
  <input type="hidden" name="action" value="my_media_update">
  <input type="submit" value="Update Media Titles and ALT Text">
</form>

Add this function and hook in your plugin file:

public function kh_update_media_seo() {
    //update media files title and alt tags here
    //
    // at the end redirect to target page
}
add_action( 'admin_post_my_media_update', 'kh_update_media_seo' );

When form will be sent and field "action" will have value "my_media_update", then your function will be executed. Wordpress Codex

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.