Take a look at the Plugin API/Action Reference, particularly the section Actions Run During a Typical Request.
In WordPress, you’re going to want to hook your code to various specific points of execution, otherwise it will be executed immediately, which is often not the desired result.
The code you posted is going to be executed as soon as the plugin is loaded, which takes place on the plugins_loaded
hook. That’s way too early in the flow of loading a page (for example) to be getting a post/page ID.
Below, I’ve wrapped your code in a function that is hooked to the template_redirect
hook and now it generates the expected results.
add_action( 'template_redirect', 'wpse_inspect_page_id' );
function wpse_inspect_page_id() {
$page_object = get_queried_object();
var_dump($page_object);
$page_id = get_queried_object_id();
echo $page_id;
}
Related Posts:
- How to create an API for my plugin?
- Plugins in symlinked directories?
- Is there a way for a plug-in to get it’s own version number?
- How to add tab which is visible only in admin side of product in woocommerce? [closed]
- Best practices for using the transients API
- Hook the Keydown Event in the TinyMCE Post Editor
- Elementor Fatal error: Class ‘Elementor\Widget_Base’ not found
- How Can I Add a Filter to Class Instance Only?
- Headers already sent error with CSV export plugin
- How to add HTML / Form to an Admin Bar Menu
- Unable to write test cases for a WordPress plugin using PHPUnit and WP-CLI
- Modify how gallery.js builds the shortcode [gallery ...] in tinyMCE?
- How to use is_multisite() in a must-use-plugin?
- why creating tables using $wpdb is not being executed while installing plugins?
- How to update the language used by wordpress from a plugin
- Is there any background process that I can run from plugin without depending on page hits on a website without affecting page-load speed?
- Woocommerce how to update variable product price programitically?
- Using $wpdb object in a widget
- Unified Approach for Placing Option Pages
- Define a function outside a class and call the function using action or filter hook
- Modifying WordPress plugins
- WordPress Ajax callback function from plugin – OOP
- Is there any way to have Featured Text, as opposed to Featured Image?
- Can I use namespaces in my plugin?
- Will cron job run if page loaded is being served from cache?
- Sharing common functionality (functions, template parts) between plugins and themes?
- Custom url for a plugin page add_rewrite_rule WordPress
- Access first name and last name from user_register
- Woocommerce custom emails without header and footer
- Custom attribute type not displaying terms in edit product – WooCommerce
- How do I add a prefix when a user registers
- Check and load class of theme from a plugin
- Short code not working in boostrap modal dont no why?
- What is the proper way to include Bootstrap when executing a shortcode
- How can I turn a custom wordpress page into a product page?
- how to get link of added document with a post
- Doubts about the use of metadata and how this can affect performance on WordPress
- How can I replace content in the WP Admin area before an admin page is rendered?
- update_post_meta not working in a loop
- When is the proper time to minify css and js with git workflow?
- Get draggable widgets on Edit Post page
- Plugin that saves form data
- Auto-Populate Image After Media Upload
- Warning: include(): https:// wrapper is disabled in the server configuration by allow_url_include=0
- update your existing plugin’s WordPress compatibility
- How dynamic action login_form_{action} is working
- WP Multisite login not working on one subsite. Possibly cookies/ history issue?
- dbDelta not installing database
- Cannot get jQuery UI to enqueue in my plugin!
- Can I use %category% like Templates in my Plugin?
- How to get the url of logo image?
- Plugin path comes wrongly
- Catching Form Submission in WordPress Admin Panel
- Replace first occurence of a word with link [closed]
- Converting core modification to a plugin
- How to replace settings in WordPress plugin from a theme
- Adding filter to the title without affecting the menu title
- How to validate inputs with filter in register_setting callback
- How can the_excerpt (or equivalent) be called on a category description?
- Plugin Creation: Overriding upload_max_size and post_max_size
- Problems with wierd characters and my plugin
- How to call a function from a shortcode function in an oop plugin
- How to access index file in Block Themes?
- wp_new_user_notifications to notify user and admin about new user registration
- Is it possible to use the MySQL’s ORDER BY FIELD() clause as part of the pre_get_posts hook?
- Multiple TinyMCE on button click is initialized and appended but why only last one is writeable?
- How to create a custom post-new.php page for plugin , no wp menu
- How to create a custom WordPress page with my plugin?
- How to re-render inspector controls?
- Why User_login key doesn’t work with wp_update_user()
- How to limit number of category term display in home page grids?
- A question on init and activation hooks
- Shortcode content is not showing. Only the [shortcode-tag] is showing
- Always hide a page from the menu
- How to remove the WooCommerce Product->Category thumbnail from admin [closed]
- Is it possible to replace MySQL with JSON files for WordPress
- Add custom html classes to gutenberg wrapper
- Plugin Development – Call to undefined function comment_exists()
- Replace “content-area” of themes 404 page with plugin?
- Autoloading & Namespaces in WordPress Plugins & Themes: Can it Work?
- WordPress doesn’t create table on plugin activation
- Create Customization panel for Plugins not for theme
- Update database record in plugin
- How is construct function working even when variable is assigned null value?
- Custom signature appears twice on page
- Declaring a new woo commerce product type i get this error
- Can I use a hook other than ‘init’ to handle form submissions?
- Managing Custom Designed Content
- Stop theme displaying comments
- List Available Templates for Current Theme in a Plugin
- Do a summary based on sections in a post
- Plugin as custom page
- Adding Third Post Box Column: postbox-container-3
- Plugin capabilities
- WooCommerce custom payment gateway
- Deactivate plugin on registration
- How to get the value entered in the input field in wordpres
- Sanitize and Save metabox values
- How can I add a custom button to the post editor?
- How to use register_setting()