Here’s reference from Codex
To use the add_action hook when your plugin or theme is built up using
classes, add $this to your add_action call together with the function
name within that class, like so:
class MyPluginClass {
public function __construct() {
//add your actions to the constructor!
add_action( 'save_post', array( $this, 'save_posts' ) );
}
public function save_posts() {
//do stuff here...
}
}
Related Posts:
- Where is the best place to use add_filter
- When can you get current page ID and initialize hooks right after?
- Using the ‘draft_to_publish’ hook (post status transition)
- Enqueue style inside shortcode but its loaded at the bottom of page (before footer scripts)
- How can I limit functionality in one version of a plugin?
- Shortcode display outside the div
- Good tools for locating hooks in a wordpress page/admin interface/blog post?
- Can a plugin add to header/footer/body content?
- Is it possible to add an action to the currently running action?
- Action on post publish
- How to add inline css/js inside a shortcode
- How to prevent action in ajax request, when in admin side?
- template_redirect not being called when using ajax
- add action wp_head not working
- What are the benefit in adding hook in the init() hook?
- add_action in functions.php, do_action in plugin?
- add action for displaying posts using a shortcode
- ‘wp_login’ action hook not working with wp-login.php file
- Any hook for pre-plugin-update -either bulk or single plugin update
- wordpress plugin is not activating from widget
- When to load auto-login code?
- How to get all of the activate_plugin action parameters?
- How to check current user before all actions and filters?
- Get Time Taken By Each Action Hook in WordPress
- Are functions in main plugin file called before function bound to register_activation_hook runs?
- Custom plugin init action causing general slowness
- Inject HTML meta tag inside wordpress tag using add_shortcode
- Add action to fire when a published post is updated
- How to pass variables to a function argument using add_action [duplicate]
- Submit Form data to another page via Ajax (WordPress Way)
- Does using `add_action( ‘init’…` cause performance issues?
- Hook for page Request?
- Custom CSS not being added by plugin
- How to use add_action for multiple instances of the same class
- How to append new form elements in “Add New” form of Users in WordPress admin panel?
- I cannot include a file in my plugin settings page
- Why do actions with class and public method don’t fire __construct()
- The function called on the wp head hook becomes null
- Nested Actions and Filters
- Passing function into add_action always returns the first argument
- Get post content inside plugin class method
- Remove action added in plugin class from theme
- echo plugin results on pages
- Plugin Hook: Get posts
- Is it possible to cancel a post status transition?
- How wordpress plugin hooks works? [duplicate]
- add_action() not working for admin
- dbDelta only creates the last table
- How can I make it so the Add New Post page has Visibility set to Private by default?
- How does WordPress handle MySQL row lock errors?
- Sync my svn repositories
- Override plugin localization
- When do I need to use esc_attr when using WordPress internal functions
- Where should my plugin POST to?
- Get all options saved by another plugin
- How to bulk send emails
- Has anyone used require.js for handling plugin scripts?
- Multiple files in a plugin
- adding a new value to a serialized array in WP_option table
- Write to / remove from default .htaccess file from plugin?
- What is the best way to mimic a search query within a plugin
- Remove rewrite rules generated by plugin during deactivation
- Programatically download a plugin
- How to Use Parameters with a Do_Action Function Within PHP Tags
- Call to a member function have_posts() on a non-object on normal loop
- Creating plugin using simple_html_dom parser?
- Not able to add option in Sub-Menu under page
- wp.media javascript issue with on select
- jQuery for custom plugin not working with Divi theme
- Creating shortcodes in plugin
- register_activation_hook in oop approach
- Why is the ‘Gutenberg’ Plugin generating an ‘Inconsistent File Permissions’ error when other Plugins, with the same permissions, do not?
- Basic function call on init failing
- WordPress front end AJAX. Return 0 :?
- breadcrumb need to show all pages and subpages
- Widget’s container?
- WordPress 5.4 – How to prevent to enter only certain values in custom field
- DB Query not working in Plugin
- sending different email notification while registration based on user role
- WordPress Widget Not Saving Instance
- Settings options not showing up on Sub Menu page in WordPress plugin
- My plugin does not install correctly if a previous version is still installed
- unregister a sidebar widget
- Could add_query_arg() redirect user to external site?
- Hook from plugin doesn’t fire up from external PHP script
- wp_insert_post not inserting post from XML
- Access “wp site health info” data from plugin
- How to prevent redoing get_posts queries and make results available to other scripts?
- Prefix WordPress Taxonomy Tags With Hashtag Symbol Like Twitter
- Change Woo Custom Endpoint Titles from a Plugin
- Get input form data posted by users
- WP internationalization not loaded
- Adding custom cron_schedule prior to wp_schedule_event
- Create plugin with form in post and submit it to specific form
- Storing values in Post Meta vs new tables
- Capturing POST data
- manage_{taxonomy}_custom_column not working
- How to be Variables and options must be escaped when echo’d?
- Mixed results with is_page() WordPress function, when using $this, or self
- I have created a custom plugin. After activation it creating page but i am not getting how to add another php file at page content?