WordPress.org

Ready to get started?Download WordPress

Forums

WP-Members
[resolved] Noindex meta tag for blocked pages (4 posts)

  1. Energ
    Member
    Posted 1 year ago #

    Hello!

    With such a little PHP knowledge I cannot get this simple code to work. The idea is to get a search engine robot block for all the pages that are blocked by wp-members plugin.

    I am sure some of you will sort this out very quickly. So far I have this:

    //Try to block search engines from blocked pages
    add_filter( 'wpmem_block', 'my_block_function' );
    function my_block_function( $block ) {
    
    	if ($block == true) {
    		add_action('wp_head', 'my_function');
    		function my_function() {
    			echo '<meta name="robots" content="noindex, nofollow">';
    		}
    	}
    
    	return $block;
    }

    Thanks!

    http://wordpress.org/extend/plugins/wp-members/

  2. Chad Butler
    Member
    Plugin Author

    Posted 1 year ago #

    That probably won't work because the wp_head will be completed before the wpmem_block filter. wpmem_block is part of the wpmem_securify process and thus comes during the hook the_content.

    What you would need to do is write a function hooked to wp_head. You need a process to get the post ID so you could use get_post_custom_values to get the block/unblock value.

  3. Energ
    Member
    Posted 1 year ago #

    Thanks. I noticed that getting custom value didn't work on posts, so I made it to check if it is one.

    Here it is, if someone needs it too:

    //Add noindex meta tag to blocked pages, posts, blog page and archive page
    add_action('wp_head', 'add_meta_function');
    function add_meta_function() {
    	$values = get_post_custom_values('block');
    	if ($values[0] == true || is_single() || is_home() || is_archive()) {
    		echo '<meta name="robots" content="noindex, nofollow">';
    	}
    }
  4. Chad Butler
    Member
    Plugin Author

    Posted 1 year ago #

    Actually, get_post_custom_value does work on posts, but you have to pass the
    $post->ID.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic