action hook wp_head higher priority as all other plugins/hooks

I work a bit with the wordpress API and I want to try to include a <meta>-tag into my head area of my website.

This is my code (works everything fine!):

class dmd_noindex_options_page{
    function __construct() {
        add_action( 'wp_head', array( $this, 'set_meta') );
    }
    function set_meta(){
       $pages = get_option('dmd_noindex_pages');
       $pages_id = explode(',', $pages);           
       for($i = 0; count($pages_id) > $i; $i++){
           if(is_page($pages_id[$i])){
              echo '<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">';
           }
        }
    }
}
new dmd_noindex_options_page;

My problem:

I use the WP PlugIn Yoast SEO and if I take a look into my code of the website I see this above of my <meta>-tag.

<!-- This site is optimized with the Yoast SEO plugin v2.3.5 - https://yoast.com/wordpress/plugins/seo/ -->
<meta name="robots" content="noindex,follow"/>
<meta name="description" content="Impressum von EK Immobilien und Kontaktaufnahme"/>
<link rel="canonical" href="http://ek.dimadirekt.com/de/impressum/" />
<meta property="og:locale" content="de_DE" />
<meta property="og:type" content="article" />
<meta property="og:title" content="Impressum -" />
<meta property="og:description" content="Impressum von EK Immobilien und Kontaktaufnahme" />
<meta property="og:url" content="http://ek.dimadirekt.com/de/impressum/" />
<meta name="twitter:card" content="summary"/>
<meta name="twitter:description" content="Impressum von EK Immobilien und Kontaktaufnahme"/>
<meta name="twitter:title" content="Impressum -"/>
<!-- / Yoast SEO plugin. -->

I don’t want deactivate this Yoast SEO PlugIn! I tried to set a higher priority of my add_action but that doesn’t work.

E.g.:

add_action( 'wp_head', array( $this, 'set_meta', 2,1) );

My question is:

How can I include my <meta>-tag above the <meta>-tags of the Yoast SEO Plugin?
Can I include my code first of all plugins?

Solutions Collecting From Web of "action hook wp_head higher priority as all other plugins/hooks"

I just skimmed through the WPSEO_Frontend class and found the wpseo_robots filter for the meta robots content string.

You might try something like:

add_filter( 'wpseo_robots', function( $content )
{
        // your logic here ...
        return $content;    
} );

to override the robots meta content from that plugin.

ps: You should consider moving the add_action out of the class constructor.

Including it higher is probably not the right thing that you want as you are most likely to enter an “undefined functionality” on google side when encountering conflicting tags.

If you don’t want the yoast SEO to output its tags you will need to find in its code how to disable or override the specific functionality.

But probably the most robust thing you can do is disallow those pages in robots.txt instead of “fighting” with all the SEO plugins. You can use the do_robotstxt action for that. something like (taken from here https://vip.wordpress.com/documentation/vip-development-tips-tricks/robots-txt/)

function my_disallow_directory() {
    echo "User-agent: *" . PHP_EOL;
    echo "Disallow: /path/to/your/directory/" . PHP_EOL;
}
add_action( 'do_robotstxt', 'my_disallow_directory' );