Home > database >  Use Yoast Filter to modify value of wp_head output?
Use Yoast Filter to modify value of wp_head output?

Time:06-22

I want to modify my wp_head. I am using the Yoast plugin I want to add a new custom meta tag after the description meta tag. I try this code for add keyword tag but it's not shown after the description tag its shown in a lower position this code

    /*Display custom meta keywords or the post excerpt */
function add_custom_meta_key(){

#Single Page Meta Description
if( is_single() ){
    $key = get_post_meta( get_the_id(), 'keywords', true);
    if( ! empty( $key )  ){
        $meta_key = esc_html($key);
        echo '<meta name="keywords" content="' . $meta_key . '" />';
    }
}}
add_action( 'wpseo_head', 'add_custom_meta_key', 2 );

CodePudding user response:

hope you are having a wonderful day. As far as I understand from your query and code, you are trying to add meta descriptions and meta keyword tag just one after another. I think you should change the hook from wpseo_head to wpseo_metadesc It will render the meta tags one after another.

I have added the code example below.

// Define the add_custom_meta_key callback 
function add_custom_meta_key($wpseo_replace_vars)
{
    if (is_single()) {
        $key = get_post_meta(get_the_id(), 'keywords', true);
        if (!empty($key)) {
            $meta_key = esc_html($key);
            echo '<meta name="keywords" content="' . $meta_key . '" />';
        }
    }
    return $wpseo_replace_vars;
};
add_filter('wpseo_metadesc', 'add_custom_meta_key', 10, 1);

This code will provide the output shown in this image. Meta keyword output

  • Related