I am looking to create a plugin which will allow me to update content within the wp_head
function. I have the below code which is fine in allowing me to add new content during the wp_head
load event;
<?php
function manipulate_head() {
?>
<title>This is a second title</title>
<?php
}
add_action('wp_head', 'manipulate_head');
However it does not allow me to remove or replace something that is already within the <head>
tags on the page.
An example would be to update the <meta name="description">
tag in the section of the website. The website could have a <meta name="description">
in which case we need to update the content, otherwise we need to add a new one.
wp_head
is an action, so anything output there is printed immediately during load, so there isn't a value to manipulate. To do this you'd need to do something very hacky to capture the output, which would prevent it being output while you do string manipulation to change the markup, therefore slowing down the site.