Greetings. In the subject there is a file, header.php . Under some condition, you need to create a meta description tag yourself. To output meta tags, the header.php function is wp_head() . Tell me how can I change or delete a description before calling wp_head() ?

There is a plug-in All in One SEO Pack .

    1 answer 1

    Here is a ready-made example for replacing title, by which you can understand the principle of replacement.

     add_action('template_redirect', 'before_header', 0); add_action('wp_head', 'after_header', 900); function before_header (){ ob_start('change_title_tag'); } function change_title_tag($head) { $title = 'My SEO Title'; if (!$title) return $head; return eregi_replace('<title>[^<]*</title>', '<title>'.$title.'</title>', $head); } function after_header() { ob_end_flush(); } 

    The code must be placed in the functions.php file. It’s easy to change a regular expression to replace the meta description.

    • Tell me how you can pass category_description (get_the_category () -> get_ID) in function change_title_tag ($ head)? I understand that category_description (get_the_category () -> get_ID) can be called in the theme file, but not in functions.php. - IgorPr
    • If you use get_the_category () -> get_ID outside of a loop, then you need to pass a value to get_the_category ($ id). Also, get_the_category () returns an array, so using get_the_category () -> get_ID is not correct. Try instead of category_description (get_the_category () -> get_ID) to use the function term_description ($ term_id, $ taxonomy). In your case like this: term_description ($ cat, 'category'); - LCGreyAngel