Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress replace meta description

I need to replace the existing <meta name="description"...> generated by wp_head() function in header.php with a custom meta description. The information in the page is not a regular wordpress post, is taken from an external DB. I was able to add my custom meta but the old one is also there

function add_meta_tags() 
{
    global $data;
    if(!is_null($data['metas']['page_meta_description']) )
    {
        echo '<meta name="description" content="'.$data['metas']['page_meta_description'].'">';
    }
}
add_action('wp_head', 'add_meta_tags');

Is there any way to: - delete the default meta description with an action or filter in function.php? or, - replace the value of meta description somehow before is rendered?

like image 271
Scarpelius Avatar asked Dec 04 '25 23:12

Scarpelius


1 Answers

function remove_meta_descriptions($html) {
    $pattern = '/<meta name(.*)=(.*)"description"(.*)>/i';
    $html = preg_replace($pattern, '', $html);
    return $html;
}
function clean_meta_descriptions($html) {
    ob_start('remove_meta_descriptions');
}
add_action('get_header', 'clean_meta_descriptions', 100);
add_action('wp_footer', function(){ ob_end_flush(); }, 100);
like image 200
Павел К. Avatar answered Dec 06 '25 14:12

Павел К.