You’re thinking about this entirely wrong. A meta tag isn’t something you add when a post gets saved, it’s something that gets added to the output when a post is viewed.
So instead of trying to hook the action inside save_post
, you hook it on every page load, and inside the hook you check if your custom field exists on the post being viewed. If it is, you output the tag.
function wpse_283352_add_meta_tag() {
if ( is_singular() {
$post_id = get_queried_object_id();
$meta = get_post_meta( $post_id, '_my_custom_field', true );
if ( $meta ) {
echo '<meta name="my_custom_field" content="' . esc_attr( $meta ) . '">';
}
}
}
add_action( 'wp_head', 'wpse_283352_add_meta_tag' );
That function just goes in your plugin file/functions file, not inside any other hook.
Related Posts:
- Why does save_post action fire when creating a new post?
- Unable to prevent function using save_post firing twice
- Exclude trash from save_post
- Write automatic title at save_post (infinite loop)
- Add meta field after post has been published
- Create post revision on slug change
- Can’t Get Metabox Data Saved Assistance Needed
- Run a function on all posts
- save_post not triggered when a post is updated
- WordPress sent multiple requests on update
- Add Tag to post after publishing
- save_post action not firing when save
- save post running twice and creating two database entries – how do I disable this?
- A way to see real changes of posts inside an action?
- remove_action or remove_filter with external classes?
- add_action reference a class
- what is correct way to hook when update post
- Remove parent theme action in child
- How to know what priority to use with add_action()?
- How to do_action and get a return value?
- Remove an action from an external Class
- Return $post_id when DOING_AUTOSAVE?
- Difference between do_action_ref_array() and do_action()
- Can my “add_action” function know the name of the hook calling it?
- Why is there both a save_post and wp_insert_post action?
- Does the event ‘wp_version_check’ even exist? What is it doing?
- When is admin_init Action ran?
- Can an action callback prevent the parent from continuing execution?
- remove_action in a theme
- add_action ‘manage_posts_custom_column’ in a class [closed]
- How do I enqueue a script to run inside the Gutenberg editor?
- trigger save_post event programmatically
- How can I find out what functions are assigned to actions?
- Difference between do_action(‘admin_enqueue_scripts’, $hook_suffix) and do_action(“admin_print_styles-$hook_suffix”) syntax
- Cron jobs in a class
- Add something to beginning of the content
- Disable WooCommerce action
- Custom post type save_post action not firing
- How can I remove the WP menu from the admin bar?
- When and Where to use wp_insert_post()
- Why, Where, and When to use reference pointers in filters/hooks?
- flush_rewrite_rules on save_post Does Not Work on First Post Save
- add_action customize_register not working
- A Post is saved twice or more during add_action(save_post)
- Passing arguments into ‘init’ function
- Add a Custom Field in Comment Box AFTER text area BUT BEFORE Send button
- How to use conditional add_action for WordPress
- Why can’t I hook into save_posts after admin_init?
- Action ‘save_post’ not working for quick edit
- Using wp_insert_post and post_update_meta but need to fire save_post afterward
- WP Schedule Event – Every Day When First Visitor Comes
- enqueuing React script and hooking its target div fails to load script
- wp_logout action not working
- How to remove an action that is added inside a class
- Having an add_action( ‘user_new_form’,) [closed]
- Using auth_redirect returns cannot modify header information
- add action which returns modified value
- What would cause the ‘wp’ action to fire twice per page (but only once per post) in Firefox only?
- Problem:Save Several Duplicate posts in The Database and then Error nesting level of ‘100’ reached
- How to remove an action added by a child theme of Genesis
- I don’t understand why I need a lower priority to remove an action with a higher priority to make it work
- prevent post submission
- WordPress admin WP_table_list show incorrectly
- wp_get_attachment_metadata returns false with add_action() ‘add_attachment’ hook
- How to add a checkbox inside the “Publish post” widget?
- Manually add admin bar
- Is there a way to expose additional fields to the Bulk Action > Edit functionality?
- What’s the usage of action do_meta_boxes?
- How can I invoke an action after a CiviCRM triggering event?
- Custom bulk actions in WP 3.7.1
- Remove action from a plugin class
- do_action pass array argument as reference not copy [duplicate]
- Get the post_id of a new post
- how can I use add_action with external class which the function contain 2 argument?
- remove_action not working with procedural function
- Why Should We Use wp_clear_scheduled_hook and What it Does?
- Woocommerce Change placement with wrapper
- Modify query in pre_get_posts action is messing up my nav menu
- How to check if an action is already fired?
- add_action and wp_head not display content in head section
- Scheduled event won’t fire
- Get updated post meta on save_post action?
- Which action fire in front-end single post only
- When should I call add_settings_section and add_settings_field?
- update_post_meta() whenever custom post type is updated
- use add_action(‘wp_head’) in a widget
- Unable to show error message using wp_login action
- Usage of do_action and add_adction
- array of objects to do_action, callback gets singular object?
- Preventing frontpage to be deleted/moved to trash
- WP action is not triggering a do_action() call within?
- Selectively applying action based on role
- Mustn’t do_action be accompanied with a function?
- where is the documentation for add_action() parameters?
- How to set a different value in the generator meta tag?
- How to remove a custom action on child theme?
- Action hook on only publishing post not on editing or updating
- how can I use add_action run another function when publishing new post?
- How to remove function?
- Performance : Duplicating `add_action`