admin管理员组

文章数量:1023738

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. 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.

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. 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.

Share Improve this question asked Jul 24, 2018 at 19:55 Kha KaliKha Kali 411 gold badge1 silver badge2 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 5

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

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. 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.

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. 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.

Share Improve this question asked Jul 24, 2018 at 19:55 Kha KaliKha Kali 411 gold badge1 silver badge2 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 5

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

本文标签: WordPress plugin how to run function when button is clicked