The reason the code posted is not working is that $post
is not referencing the global $post
variable, which is the goal here.
Using get_the_ID()
is a good way of accessing the ID associated with the current post. That’s how I’d suggest doing it, but there are other ways too:
add_action ( 'wp_head', 'hook_inHeader' );
function hook_inHeader() {
if ( is_single() ) {
// Get the post id using the get_the_ID(); function:
echo get_post_meta( get_the_ID(), 'meta-head', true );
/* Or, globalize $post so that we're accessing the global $post variable: */
//global $post;
//echo get_post_meta( $post->ID, 'meta-head', true );
/* Or, access the global $post variable directly: */
// echo get_post_meta( $GLOBALS['post']->ID, 'meta-head', true );
}
}
Related Posts:
- How to display before H1 Title
- WooCommerce: change display order of product short description and price [closed]
- Use wp init hook to call other hooks?
- Hook that fires when admin setting is saved
- add_action(‘wp_ajax_[action name]’, myfunction) problem
- What is the difference between update_post_meta and update_postmeta hooks?
- Hook on trash post
- How to remove action hook done in a plugin from functions.php in my theme?
- Hooks for trashing, deleting, saving, restoring custom post type
- Implementing advanced add_* function wrappers
- My add_action (wp_footer, ‘method’) is not calling?
- Hook into wp_head(); in a plugin
- How to customize the WP admin default help contents
- Hide gutenberg option blocks
- Use $query->set multiple times with pre_get_posts?
- admin_post hook not called
- How can I hook into the post editor title field in order to change the HTML?
- WordPress v5.0.3 Gutenberg & JS error “Uncaught SyntaxError: missing ) after argument list”
- Can’t get ID of post that relates to the comment
- Searching hook to set cookies before and avoid “headers already sent”
- Hook before inserting user into database [duplicate]
- Bulk action hook for admin pages which uses WP_List_Table
- How to hook into user registration process Before user registers
- Searching a hook which triggers when deleting a post to get all comments
- wp_redirect() not working on form submission with init hook
- @wordpress/hooks use to add actions/filters within multiple plugin
- WooCommerce – Overwrite action hook [closed]
- Admin Hook at the Login Page
- Which Hook? Hide attachments from specific post types in media library
- Custom action on login and “remember me”
- What’s hook to use immediately after a user is authentcated [duplicate]
- Hook before posting via JSON REST API
- How to hook into Add New Member in BuddyPress
- Using wp_editor tinyMCE in metabox cause form alert on leaving page
- How to exclude a list of specific category in category widget in new post admin screen?
- Method to find a hook
- Hook when new CPT published AND postmeta inserted
- Need to check is_archive during init
- When WP_CLI runs WordPress from the command line, are hooks called?
- Query categories that have a description
- Update a costume wp_usermeta key back to 0 every 24hours (time can be specified as needed)
- Trying to run a compile command while saving post
- WooCommerce single product page quantity description [closed]
- Execute JavaScript in WordPress Hook
- Action hook for custom tax edit
- wp cron job fires at every second or so, if callback is wrapped with DOING_CRON check it never fires at all
- What is the difference between add_action and add_filter [duplicate]
- Is it possible to add the_content filter upon login?
- I don’t understand how add_action and do_action work in tandem. The former executes the code already…what is do_action for?
- Insert HTML in post, below nav bar but above content?
- Convert Gravity Form Entry Date on Export
- Hook Adding content after title
- Fatal error: Call to a member function get_queried_object_id()
- “admin_enqueue_scripts” hook or $_GET[‘page’]?
- wp_logout hook never triggered
- add short description under price
- How do I use pre_option_{option_name} correctly?
- post.php AJAX request not being called when publishing post
- How to add content to suscriptor dashboard frontend?
- Add PHP code to header section from plugin
- How to change a WordPress term’s slug before saving
- dynamic add_action according to child pages (for homepage control)
- Remove action in a parent theme from the child theme
- Remove action within a class in a parent theme’s includes folder from the child theme
- WordPress media library allow uploading fake file
- Action hook save_post for newly created posts – $post object data is all empty
- add_filter to the_content from plugin function
- What hook do I use if I want to update a user profile field when a new user is created?
- What hook should I use that will fire whenever I open a post for editing in the WP back-end?
- Can not using get_header() cause trouble (later on)?
- Is there a author_update action?
- How to set Media Box form elements default value?
- How to get product information using hooks?
- Print additional fields in wp_new_user_notification_email_admin
- Can we change the hook firing sequence?
- Intercept loading of a certain page
- Add content in cpt archive page
- remove_action not working for a function
- Is there no concise way, a library maybe, to help with unhooking class functions and so on?
- Output Redirect Headers on Admin Dashboard Page
- Debug a WP install: how to find which functions write post updates during a process (a woocommerce checkout in my case)
- What hook should I use to add post meta data with on update?
- Problem with call_user_func_array front end
- How to I prevent WordPress from switching external HTTP links to HTTPS?
- Stylesheet is getting removed/deregistered automatically
- How to override get_avatar() function?
- Change user role if it’s orders count more than
- Hook triggered twice in a Woocommerce single product page
- Using actions, hooks and filters in a non-WordPress page
- Hook into create_category
- Trouble adding data to options table
- Getting all post/page IDs related to a soon-to-be-deleted tag/cat
- Hooking get_pages()
- How to hook into publish_posttype?
- Fire a hook programmatically
- How do I override the user’s input when updating a custom post type’s permalink?
- $GLOBALS & global doesn’t work [closed]
- Hooks not working on live server
- Callback API for getting response from fygaro payment gateway
- Changing login url