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 it possible to use Classes between WordPress plugins with separate namespaces?
- Any hook for pre-plugin-update -either bulk or single plugin update
- Get plugin option in another PHP file
- How to create user groups and allow custom posts and plugin modify/access to specific group?
- wp_create_nonce function doesn’t work inside a plugin?
- Test files for plugin development
- Input with spaces in Shortcode attributes overwritten by defaults
- Unable to override pluggable function wp_mail
- Avoid class name collision when using third party libraries in plugins?
- How to create custom tables in WordPress using my own plugin?
- Own plugin corrups plugin-activation
- wordpress plugin is not activating from widget
- All Users > User List > Update User Meta Field Inline
- Checking instances of scripts in wp_head
- How to save post change url youtube link?
- plugin class inheritance. cannot change variable
- Best approach to fetch data from wp options to js file or php file
- WordPress Central User Database
- Correct way to perform non-cacheable DB query
- Allow users of my plugin to define their own shortcode rather than use mine?
- Is it possible to create Custom Post plug-in?
- uninstall.php file in Plugin to clean DB
- Archive – same title for the first two posts
- Creating a plugin to sanitize comment and the url field before display only
- Edit Yoast SEO breadcrumbs output [closed]
- Creating plugin using simple_html_dom parser?
- Object Oriented Plugin not working
- How to read individual user’s directory and display content to that specific user?
- Does WP identify plugin by plugin name or plugin_basename?
- switched from query_posts to WP_query, not working now?
- Making a plugin only available on the front-end for the logged in super admin
- Create entire wordpress as a github repositery?
- Help With MySQL to WPDB Query Conversion
- When to load auto-login code?
- AJAX search posts and pages
- How to find the origin of a file upload from within wp_handle_upload?
- How would I go about creating a user ranked post popularity page?
- Overriding functions in wordpress plugins
- CSV file generation failing
- Updating the Drag-To-Share eXtended share URLs?
- finding whether request is for post, and post id
- Building plugin with changeable custom post type values…advice needed
- How to get all of the activate_plugin action parameters?
- __callStatic method handler passed to add_action causes bug in PHP
- Enqueue WordPress plugin scripts below all other JS
- Display update notification messages like ‘What’s New’
- How to add Internationalization in WordPress using Javascript/React?
- CRUD and Frontend show from a custom table without shortcode
- How to get specific setting by settings_fields()?
- Save / Show multi line text in metabox
- Creating mySQL procedure with $wpdb
- how to disable blockrenderAppender inside all Innerblocks?
- why doesn’t this update part of this plugin work? it take me to nothing here page
- Redirection of users away from wp-admin (but not administrators)
- WordPress Gutenberg react make import of __experimentalUseInnerBlocksProps which is no more experimetal
- Using a custom plugin to capture input data via Ajax and PHP
- Python with wordpress plugin
- Display post lists in 2nd paragraph
- Map Custom Registration Fields to WordPress User Roles
- wp_set_auth_cookie causes 403 error in the wooCommerce checkout
- Not able to add option in Sub-Menu under page
- How to stop activating a plugin and show admin notice when dependent plugins minimum version is not met
- Using a post-signup hook to get user details
- How to apply a patch via plugin?
- Fixing WordPress’s Bug (failed to send buffer of zlib output compression) results in “White Screen of Death”
- Disable woocommerce cookies and delete cart data automatically
- Ajax contact form widget plugin data not insert in database
- link bbpress forum discussion to blogposts
- Install Plugin via Code
- code is working properly in Core PHP but writing coding in WordPress
- Combine scripts from all extensions of the plugin when an extension is activated
- Creating a custom post type, adding custom meta fields, preventing all future editability of posts of this type