Wait for the action plugins_loaded
before you create the class instance. The pluggable functions are loaded at this time. From wp-settings.php
:
/**
* Fires once activated plugins have loaded.
*
* Pluggable functions are also available at this point in the loading order.
*
* @since 1.5.0
*/
do_action( 'plugins_loaded' );
I would even wait for wp_loaded
in most cases. Then the global WP_Roles
object has been set up, you know the theme and the locale – you will very likely not run again into a problem because of missing information.
Never just create class instances when your plugin’s main file is loaded. This always too early. Usually you want to check the request first to exclude your code from slowing down other plugins AJAX requests or WP’s comment/XML RPC/feed processing.
So your plugin’s main file could look like this:
add_action( 'wp_loaded', [ new Something, 'setup' ] );
Related Posts:
- Get plugin_dir_url() from one level deep within plugin
- Where the Nickname is being used in WordPress
- Namespaces in WordPress – How do I initiate the main class?
- Editor access to plugin settings
- How to include and use custom class files in plugin?
- Getting user roles in plugin files
- How to keep users unique id stored in session in addition to IP in WordPress plugin?
- How to get current post user id
- New User Form – Custom Menu Page
- How can I change the meta_key for a particular piece of information?
- Why is my custom post type not being activated on plug-in activation?
- How to get all posts belongs to a user between a certain date
- Add User Role: Pre-saved in User-Meta [SOLVED]
- get_option / wp_localize_script Not Working in OOP Plug In
- Built a second plugin but it overwrote the first one
- Override plugin class which has namespace
- WordPress Object Oriented plugin development [closed]
- How to access OOP plugin function inside themes or other plugin
- How to add custom function to pluggable.php
- Disable Author Archive just for certain roles in bulk
- WordPress plugin blog creation
- Get post content inside plugin class method
- How to access classes in theme of a plugin?
- Confusing problem with displaying message recipient metadata
- Autoloading a PHP class – working outside, but not inside WordPress plugin
- Why function hooked using object are executing at all time?
- Plugin: Custom menu item problem
- Function not being called on form submit, only blank admin-post.php page
- is_plugin_active function doesn’t exist
- Disable email notification after change of password
- How to call a plugin function from index.php
- Where can I find a schema of wordpress plugin core architecture?
- How Does WordPress Uninclude/Deactivate A Plugin?
- Why allow overriding crucial pluggable functions wp_verify_nonce and wp_create_nonce?
- Trying to get logged-in user data inside php class
- How can I add a custom meta value on file upload?
- Check if a class exists within a method
- Admin-ajax.php is dying “0” without processing function
- How do I register a stylesheet inside a WordPress widget?
- Add a Custom Permalink option in the Permalink Admin Screen?
- How to remove an Ajax action
- Has anyone used require.js for handling plugin scripts?
- publish_post plugin hook doesn’t always pass $post->post_content
- Multiple files in a plugin
- Auto add content such as pages upon plugin activation?
- Escape when echoed
- How to remove the woocommerce_checkout_process action hook in woocommerce if particular project in cart [closed]
- WP_Query ordering numbers as letters
- WordPress Automatic Plugin Update Renames Plugin Directory
- How to change commenter links to /user/user_id?
- Execute a plugin only on post pages
- Redirecting to home page after login as custom role WordPress
- Create dynamic page content from custom WordPress plugin page
- WordPress Gutenberg react make import of __experimentalUseInnerBlocksProps which is no more experimetal
- How to stop activating a plugin and show admin notice when dependent plugins minimum version is not met
- Fixing WordPress’s Bug (failed to send buffer of zlib output compression) results in “White Screen of Death”
- How can I make my metabox appear?
- Consolidate plugin functionality into a theme
- Custom User meta field display
- Redirect to another page using contact form 7? [closed]
- edit_user_profile and show_user_profile are not firing inside a class
- Separate database for a wordpress plugin
- WordPress Multisite: Programmatically Activate / Deactivate a Plugin of a Specific Blog
- remove all submenus from plugin
- Specific way to allow WordPress users to view their current password? And edit it?
- How to prevent plugins from sniffing/stealing other plugins’ options?
- Hook for page Request?
- composer not working on my plugin when i upload it to my website
- How to render a template file using shortcode inside a plugin
- Does WordPress check for updates of a plugin via plugins root folder name?
- Delete data from custom table when deleting a post
- Need to add/remove group of options and display them as rows
- How to deactivate my plugin upon deactivation of NextGen
- Paypal Framework
- Widgets are not displaying in the admin panel
- Content-Security-Policy implementation with WordPress W3Total Cache plugin installed
- Fatal error “Call to undefined function is_plugin_active” each time the plugin is activated
- Custom Plugin: How to Include Install Buttons of other 3rd Party Plugins?
- Clean old users
- How to make WooCommerce multiple filter with custom meta fields
- How to write different events from different functions in my file to a log file in WordPress
- Create a form and have custom menu display based on user answers?
- How to make a plugin api route have permission?
- Simulate Update User button click
- Modify Plugin PHP Class in Child Theme – Correct Method
- Problem with conditional woocommerce custom checkout field
- Plugin to Delete All Posts Monthly
- mailjet plugin – extend functionnalities and reuse api
- Change wp_mail SMTP settings for each E-Mail
- WordPress Thumbnail add action if no thumbnail
- Creating fields in the database
- overwrite a plugin function in functions.php
- Pass strings to plugin function [closed]
- contact form 7 captcha support anyother captcha plugin? [closed]
- how to embed a crop feature for cropping images uploaded by the user
- How to make content as required in custom post type?
- How to create a “Most Popular” & “Latest” TAB in WordPress
- is_user_logged_in() undefined at shutdown in plugin context
- What are Seeds? [closed]
- Shortcode cannot parse attributes within double quotes. ” is becoming ” breaking my shortcode