• I’m glad I decided to look at the code. First off, it doesn’t support custom post types. Second, it directly alters (i.e. ALTER TABLE) the wp_posts table in the database, rather that using the wp_postmeta table to store the user’s selection. It also stored the selection on wp_insert_post, rather than save_post, so it would only run on new posts, and not when a post is updated. In other words, you could never change the setting.

    I give it 2 stars because it will work for most people, but only 2 stars because of how it works and that it doesn’t meet my needs.

    I’m not sure the best way to make these modifications available..? I can put the zip file up somewhere. They are posted below. Should be easy to see where I made changes.

    <?php
    /*
    Plugin Name: WordPress Meta Robots
    Plugin URI: https://www.typomedia.org/wordpress/plugins/wordpress-meta-robots/
    Description: This plugin will give you full control of the <code>meta robots</code> tag for each post or page.
    Author: Typomedia Foundation
    Version: 2.0
    Author URI: https://www.typomedia.org/
    */
    
    if ( !class_exists ('wp_meta_robots_plugin')) {
    	class wp_meta_robots_plugin {
    
    	/* mod BL 2013.07.17: shouldn't alter built-in WordPress tables */
    	/*function meta_robots_addcolumn() {
    		global $wpdb;
    		if (false === $wpdb->query("SELECT meta_robots FROM $wpdb->posts LIMIT 0")) {
    			$wpdb->query("ALTER TABLE $wpdb->posts ADD COLUMN meta_robots varchar(20)");
    		}
    	}*/
    	/* mod-end */
    
    	/* mod BL 2013.07.17: Adding to wp_postmeta instead, and in response to save_post, also sanitizing inputs and checking nonce */
    	/*function meta_robots_insert_post($pID) {
    		global $wpdb;
    		extract($_POST);
    		$wpdb->query("UPDATE $wpdb->posts SET meta_robots = '$meta_robots' WHERE ID = $pID");
    	}*/
    	function meta_robots_save_post($post_id) {
    		global $wpdb;
    
    		// verify this came from the our screen and with proper authorization,
    		// because save_post can be triggered at other times
    		if ( !isset($_POST['meta_robots_metabox_noncename'])
    				|| !wp_verify_nonce($_POST['meta_robots_metabox_noncename'], plugin_basename(__FILE__)) ) {
    			return $post_id;
    		}
    
    		// verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
    		// to do anything
    		if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
    			return $post_id;
    
    		// Check permissions
    		if ('page' == $_POST['post_type'] ) {
    			if ( !current_user_can('edit_page', $post_id) )
    				return $post_id;
    		} else {
    			if ( !current_user_can('edit_post', $post_id) )
    				return $post_id;
    		}
    
    		// if this is a revision, get the original post_id
    		if ( ($temp = wp_is_post_revision($post_id)) !== false )
    			$post_id = $temp;
    
    		// validate & save fields
    		$acceptable_values = array('index, follow', 'index, nofollow', 'noindex, follow', 'noindex, nofollow');
    		$meta_robots = $acceptable_values[0];
    		if ( isset($_POST['meta_robots']) ) {
    			$meta_robots = filter_var($_POST['meta_robots'], FILTER_SANITIZE_STRING);
    			if ( !in_array($meta_robots, $acceptable_values) )
    				$meta_robots = $acceptable_values[0];
    		}
    
    		// if there are no selections, attempt to delete the meta info
    		if ( empty($meta_robots) )
    			delete_post_meta($post_id, 'meta_robots');
    		else if ( !add_post_meta($post_id, 'meta_robots', $meta_robots, true) )
    			update_post_meta($post_id, 'meta_robots', $meta_robots);
    
    		// done
    		return $post_id;
    
    	}
    	/* mod-end */
    
    	function meta_robots_options_box() {
    		add_meta_box('meta_robots', 'Meta Robots', array('wp_meta_robots_plugin','meta_robots_dropdown_box'), 'page', 'side', 'low');
    		add_meta_box('meta_robots', 'Meta Robots', array('wp_meta_robots_plugin','meta_robots_dropdown_box'), 'post', 'side', 'low');
    		/* mod BL 2013.07.17: adding support for custom post types */
    		$post_types = get_post_types(array('public' => true,'_builtin' => false), 'names');
    		foreach ( $post_types as $post_type ) {
    			add_meta_box('meta_robots', 'Meta Robots', array('wp_meta_robots_plugin','meta_robots_dropdown_box'), $post_type, 'side', 'low');
    		}
    		/* mod-end */
    	}
    
    	function meta_robots_dropdown_box() {
    		global $post;
    		/* mod BL 2013.07.17: switching to post meta, and adding a nonce */
    		//$meta_robots = $post->meta_robots;
    		$meta_robots = get_post_meta($post->ID, 'meta_robots', true);
    		wp_nonce_field(plugin_basename(__FILE__), 'meta_robots_metabox_noncename');
    		/* mod-end */
    	?>
    		<fieldset id="mycustom-div">
    		<div>
    		<p>
    		<label for="meta_robots" ></label>
    		<select name="meta_robots" id="meta_robots">
    		<option <?php if ($meta_robots == "index, follow") echo 'selected="selected"';?>>index, follow</option>
    		<option <?php if ($meta_robots == "index, nofollow") echo 'selected="selected"';?>>index, nofollow</option>
    		<option <?php if ($meta_robots == "noindex, follow") echo 'selected="selected"';?>>noindex, follow</option>
    		<option <?php if ($meta_robots == "noindex, nofollow") echo 'selected="selected"';?>>noindex, nofollow</option>
    		</select>
    		</p>
    		</div>
    		</fieldset>
    	<?php
    	}
    
    	function add_meta_robots_tag() {
    		global $post;
    		/* mod BL 2013.07.17: switching to post meta, and cleaning up code for my own benefit */
    		$meta_robots = get_post_meta($post->ID, 'meta_robots', true);
    		if ( is_home() || is_single() || is_page() ) {
    			echo '<meta name="robots" content="'.(empty($meta_robots) ? 'index, follow' : $meta_robots).'" />'."\n";
    		} elseif ( is_category() || is_tag() || is_archive() ) {
    			echo '<meta name="robots" content="noindex, follow" />'."\n";
    		} else {
    			echo '<meta name="robots" content="noindex, nofollow" />'."\n";
    		}
    		/* mod-end */
    	}
    
    	} // class meta_robots_plugin
    }
    
    /* mod BL 2013.07.17: shouldn't alter built-in WordPress tables */
    //add_action('init', array('wp_meta_robots_plugin','meta_robots_addcolumn'));
    /* mod-end */
    add_action('admin_menu', array('wp_meta_robots_plugin','meta_robots_options_box'));
    /* mod BL 2013.07.17: this should be called on save_post so the user can change it. */
    //add_action('wp_insert_post', array('wp_meta_robots_plugin','meta_robots_insert_post'));
    add_action('save_post', array('wp_meta_robots_plugin','meta_robots_save_post'));
    /* mod-end */
    add_action('wp_head', array('wp_meta_robots_plugin','add_meta_robots_tag'));
    ?>

  • The topic ‘Can't recommend’ is closed to new replies.