Half-Elf on Tech

Thoughts From a Professional Lesbian

Category: How To

  • Customize Network Toolbars

    Customize Network Toolbars

    This came up in the WordPress Support Forums. If you use Multisite, the WordPress toolbar (at the top of your site when logged in) has a special item called “My Sites” which shows all the sites of which you are an administrator. This is great and works as a quick jump to get to a different site really fast. It has problems, though, in that if you make a network with a bunch of sites named the same thing, it’s hard to tell which site you’re on.

    Now I know what you’re thinking! “Mika!” You say. “Mika, come on, no one has 100 sites with the same name unless they’re doing what you say is a terrible idea, and duplicating sites!!”

    Au contraire, mon frère. There are a few totally understandable reasons why this might happen. Fairly recently I was helping a school sort out Multisite, and they wanted a site for each classroom (easy) and the names of the sites would all be the same: Super Cool School – Class Frog

    And their ‘My Sites’ list was all the same.

    An example of a site list where the names aren't really readable because they're too long

    As you can see, WordPress wisely puts a practical limit on the title length, which makes sense. Now when I was faced with this problem, I remembered something that had come up in the forums, where someone wanted the ‘language’ of the site to show up in the site list, so his sites would be showing as “SiteName (en)” and so on. Since he was using the site path (en, de, etc) as the site’s slug, it was easy for him to come up with this, once I (accidentally) pointed him the right way:

    <?php
    /*
    Plugin Name: Show Site Path
    Description: Show Site Path in My Sites Menu
    */
    
    function helf_customize_my_sites( $wp_admin_bar ) {
    
        $mysites = $wp_admin_bar->user->{'blogs'};
    
        foreach($mysites as $site) {
          $site->blogname .= ' (' . $site->path . ')';
        }
    }
    
    add_action('admin_bar_menu', 'helf_customize_my_sites');
    

    I say it was an accident because I did read the question wrong, but it actually gave me the answer to my schools. They too used the classroom name as the site path, so for them I changed one line:

        foreach($mysites as $site) {
          $site->blogname = '$site->path';
        }
    

    That was it. Now the sites showed up the way they wanted.

  • Mailbag: One Analytics to Bind Them

    Mailbag: One Analytics to Bind Them

    Mailbag on Monday because Angie Meeker asks:

    Do you have an article about getting Google Analytics right on Multisite, so SA can see indiv stats for each site, but also parent. Where the SAdmin owns the entire GA account (site owners don’t need their own GA account)

    There are a few ways to do this, and they’re all pretty easy.

    Google Mod_PageSpeed

    This is the ‘easiest’ way if you already have PageSpeed installed. You can put in your GA filter in the .htaccess and be done with it:

    ModPagespeedEnableFilters insert_ga
    ModPagespeedAnalyticsID <Analytics ID>
    

    That’s actually what I do here, because I’m incredibly lazy and I have Pagespeed set up on my server. I can even make this a little more special by using if statements in Apache 2.4:

    <If "$req{Host} == 'www.domain.com'">
        ModPagespeedEnableFilters insert_ga
        ModPagespeedAnalyticsID <Analytics ID>
    </If>
    

    Graph Background

    MU Plugin

    But if you’re still on Apache 2.2 or don’t want to mess with .htaccess for whatever reason, then you should try an mu-plugin, my favorite things in the world. And all you have to do is this:


    // Paste your Google Analytics code here

  • Mailbag: I Don’t Woo, But I Do CPT

    Mailbag: I Don’t Woo, But I Do CPT

    A preface to this, I don’t actually use WooCommerce so I can’t give anyone a specific answer to that, but Geovanni asks:

    Im in a pinch. I have a woocommercesite and i want to make a part where i can have users who already registered can have their your own URL and page where they can make post. I read a post u made on http://wordpress.org/support/topic/allow-users-to-post-events-on-their-own-page?replies=6 ,but u didnt say anything about how u got it to work or if u have. Can you help me?

    This was a post from two years ago, where someone asked the following:

    I’m working on a WordPress site that will allow artists to post their own events. I can’t find a plugin that will allow these posts to link up to their own pages. Each user that registers has their own URL and this page has all of their information. I’m trying to make sure that the tour dates posted aren’t ending up on every users page!!

    Any help would be greatly appreciated!

    I suggested that one could accomplish this with Custom Post Types, which was a theoretical remark, since I didn’t have the details, but when the OP later said “I need everyone to be able to create an event and post it on their own page…” I agreed it would probably be a better fit for Multisite, since they may have more than one of their own events.

    Lightbulb on a table

    So what did I mean about Custom Post Types? Well sometimes the answer is to think about the problem in different ways. Obviously the easiest thing in the world is to tell people ‘their’ page is http://example.com/author/name, and then have them post in a specific category (say… events). Thus all events are in an event group, and everyone has their own page. But I also know users can be a little confused by WordPress and categories, so you have some options here, when it comes to management.

    First of all, there are plugins like Restrict Categories, which let you restrict users to a category. That works, but if you don’t want the URLs to have

    /category/

    in the slug for just that one thing, you really do need to look at Custom Post Types. That leads us down the road of things like AAM – Advanced Access Manager which will let you make a custom role for ‘Event Manager’ who can perhaps manage all events, and ‘Event Poster’ who can only post.

    All this does highlight a flaw/annoyance in WordPress, and that is complex roles. WordPress’s role system is, at once, stupid simple and crazy complex. It’s a total headache to restrict people to specific areas, and in general, I hate having to do it because I find I spend more time messing with that than I do working on the site. At the same time, I dislike giving people more ‘power’ than they need. I can’t make a person a ‘comment moderator’ without giving them access to write/edit posts, for example, which is not a far-fetched wish. You can use plugins, like Disqus, to do that, but that means you’ve offloaded comments, and I don’t like that.

    Of course… for the question posited by Geovanni the answer is “Use WordPress Multisite.” Install WordPress, activate Multisite, give the user a site, let them go to town.

    That, of course, may not answer all his questions.

  • All Comments By Email

    All Comments By Email

    By default, when you look at the list of comments on your WP Admin dashboard, you get a list like this:

    The Edit Comments page

    On that list, if you click on the IP address of the commenter, you go to all comments by that IP, but if you click on the email you get a mailto link, to let you email the person. That’s great, but as my friend, and fellow fansite runner, Liv pointed out, a lot of people post from multiple IP addresses these days, but only one email. What she wanted was for the icon that gave you the number of approved comments to link to that person’s approved comments.

    Me, being the sort to poke around, decided to see if that could be done. I already knew how to filter columns and tables, after all. What I learned was that there actually isn’t a filter for those columns, and the only way around it was to replace it. This means I was going to have to rebuild everything, and in doing so, I wanted that email address to be a link to the search. While annoying, it was pretty easy:

    &lt;?php
    /*
    	Plugin Name: Easy Comment Search By Email
    	Description: Changes the default link for emails in the comment lists from mailto links to search results for that address.
    	Author: Mika A Epstein (ipstenu)
    	Author URI: https://halfelf.org
    
    Credit: https://wordpress.stackexchange.com/questions/83769/hook-to-edit-an-column-on-comments-screen
     */
    
    class ecsbePlugin {
    
    	public function __construct() {
    		add_action( 'admin_head' , array( &amp;$this, 'column_style') );
    
    	add_filter( 'manage_edit-comments_columns', function($columns) {
    		unset($columns[&quot;author&quot;]);
    		$columns_one = array_slice($columns,0,1);
    		$columns_two = array_slice($columns,1);
    		$columns_one[&quot;author-new&quot;] = &quot;Author&quot;;
    		$columns = $columns_one + $columns_two;
    		return $columns;
    	});
    
    	add_filter( 'manage_comments_custom_column', function($column, $column_id) {
    		
    		global $comment_status;
    				$author_url = get_comment_author_url();
    					if ( 'http://' == $author_url )
    							$author_url = '';
    					$author_url_display = preg_replace( '|http://(www\.)?|i', '', $author_url );
    					if ( strlen( $author_url_display ) &gt; 50 )
    							$author_url_display = substr( $author_url_display, 0, 49 ) . '&amp;hellip;';
    	
    					echo &quot;&lt;strong&gt;&quot;; comment_author(); echo '&lt;/strong&gt;&lt;br /&gt;';
    					if ( !empty( $author_url ) )
    							echo &quot;&lt;a title='$author_url' href='$author_url'&gt;$author_url_display&lt;/a&gt;&lt;br /&gt;&quot;;
    	
    					if ( current_user_can( 'edit_posts' ) ) {
    						$author_email = get_comment_author_email();
    					
    							if ( !empty( $author_email ) ) {
    									echo '&lt;a href=&quot;edit-comments.php?s=';
    									echo $author_email;
    									echo '&amp;amp;mode=detail';
    						   		if ( 'spam' == $comment_status )
    									echo '&amp;amp;comment_status=spam';
    								echo '&quot;&gt;';
    								echo $author_email;
    								echo '&lt;/a&gt;&lt;br /&gt;';
    							}
    							
    							echo '&lt;a href=&quot;edit-comments.php?s=';
    							comment_author_IP();
    							echo '&amp;amp;mode=detail';
    							if ( 'spam' == $comment_status )
    									echo '&amp;amp;comment_status=spam';
    							echo '&quot;&gt;';
    							comment_author_IP();
    							echo '&lt;/a&gt;';
    					}
    		
    		}, 10, 2 );
    
    	}
    
     	public function column_style() {
    		echo '&lt;style type=&quot;text/css&quot;&gt;
    			#comments-form .fixed .column-author-new {
    				width: 20%;
    			}
    		 &lt;/style&gt;';
    	}
    
    }
    
    new ecsbePlugin();
    

    The plugin needs a way better name, though, because this is just … bad. The array slice in the beginning was to remove the first item and replace it, without having to do a lot of overly wrought arguing with possible columns.

    That said, this is the sort of thing I may submit a patch for in core, since IPs change a heckuvalot more now, and while that’s a great way to find some serial-accounts and sockpuppets, sorting by email helps you find people being trolls. Both would be good, and I don’t think a lot of us email people. If anything, I’d change the author NAME to be a mailto link.

    Food for thought.

  • Mailbag: Pinging Pingbacks

    Mailbag: Pinging Pingbacks

    I run a fan site, and so does a friend of mine. Liv and I were chatting about wishlists in WordPress for fansites, and she mentioned this:

    I also like seeing who has linked to my site from other WP blogs because that helps me create fandom connections with other bloggers. I wish there was a quick button I could hit that would allow me to email those bloggers with a quick note of thanks for the connection

    When you’re running a fan website, communicating and connecting with those other sites is a killer feature. We network and that’s how we make our communities bloom, after all, since most of us can’t afford a budget for ‘real’ advertising, and it’s probably not entirely legal for us to do that anyway. So outside of spending days tracking everyone down, what about using the power of ping-backs for ourselves?

    Table Tennis

    I’m sure Liv has an unshakable confidence in my ability to code her things (and I love the requests she makes, they stretch my brain) but this one kicked my patootie a lot. Getting a list of pingbacks isn’t all that hard. There’s a plugin called Commenter Emails by Scott, which nicely lists all the email addresses used to make comments. Using that logic, it’s pretty easy to list all the pingbacks. I mean, hey, we can already do that!

    If you go to /wp-admin/edit-comments.php?s&comment_status=all&comment_type=pings you’ll see all your pings:

    All pings listed

    Just looking at that, however, made me notice a horrible problem. There are no emails listed in pingbacks. This makes perfect sense. The emails aren’t (generally) listed on a page that links to your site. That means without doing some serious site-scraping, there’s no way to get that email.

    Putting that aside, the other option is to, perhaps, list the ‘parent’ domain that pinged you. So I went back to Scott’s plugin and forked it into this:

    &lt;?php
    
    /*
    Plugin Name: Pingers List
    License: GPLv2 or later
    License URI: http://www.gnu.org/licenses/gpl-2.0.html
    Description: List all pingbacks with links to their main domain.
    
    	Quasi fork of http://wordpress.org/plugins/commenter-pings/
    
    	Copyright (c) 2007-2014 by Scott Reilly (aka coffee2code)
    	Copyright (c) 2014 by Mika Epstein (aka ipstenu)
    */
    
    defined( 'ABSPATH' ) or die();
    
    if ( is_admin() &amp;&amp; ! class_exists( 'PingersList' ) ) :
    
    class PingersList {
    
    	private static $plugin_basename = '';
    	private static $plugin_page     = '';
    
    	/**
    	 * Returns version of the plugin.
    	 *
    	 * @since 2.1
    	 */
    	public static function version() {
    		return '2.2.1';
    	}
    
    	/**
    	 * Constructor
    	 */
    	public static function init() {
    		self::$plugin_basename = plugin_basename( __FILE__ );
    
    		// Register hooks
    		add_action( 'admin_menu', array( __CLASS__, 'admin_menu' ) );
    		add_action( 'admin_menu', array( __CLASS__, 'do_init' ), 11 );
    	}
    
    	/**
    	 * Initialize hooks and data
    	 */
    	public static function do_init() {
    		// Currently empty
    	}
    
    	/**
    	 * Query database to obtain the list of commenter email addresses.
    	 * Only checks comments that are approved, have a author email, and are
    	 * of the comment_type 'comment' (or '').
    	 *
    	 * Only one entry is returned per email address.  If a given email address
    	 * has multiple instances in the database, each with different names, then
    	 * the most recent comment will be used to obtain any additional field data
    	 * such as comment_author, etc.
    	 *
    	 * @param array $fields  The fields to obtain from each comment
    	 * @param string $output (optional) Any of ARRAY_A | ARRAY_N | OBJECT | OBJECT_K constants. See WP docs for wpdb::get_results() for more info
    	 * @return mixed List of email addresses
    	 */
    	public static function get_pings( $fields = array(  'comment_post_ID', 'comment_author', 'comment_author_url' ), $output = ARRAY_N ) {
    		global $wpdb;
    
    		// comment_author_url must be one of the fields
    		if ( ! in_array( 'comment_author_url', $fields ) )
    			array_unshift( $fields,  'comment_author_url' );
    
    		$fields = implode( ', ', $fields );
    		$sql = &quot;SELECT $fields
    				FROM {$wpdb-&gt;comments} t1
    				INNER JOIN ( SELECT MAX(comment_ID) AS id FROM {$wpdb-&gt;comments} GROUP BY comment_author_url ) t2 ON t1.comment_ID = t2.id
    				WHERE
    					comment_approved = '1' AND
    					comment_type = 'pingback'
    				GROUP BY comment_author_url
    				ORDER BY comment_author_url ASC&quot;;
    		$pings = $wpdb-&gt;get_results( $sql, $output );
    		return $pings;
    	}
    
    
    	/**
    	 * Creates the admin menu.
    	 *
    	 * @return void
    	 */
    	public static function admin_menu() {
    		add_filter( 'plugin_action_links_' . self::$plugin_basename, array( __CLASS__, 'plugin_action_links' ) );
    		// Add menu under Comments
    		self::$plugin_page = add_comments_page( __( 'Pinger List', 'pinger-list' ), __( 'Pinger List', 'pinger-list' ),
    			apply_filters( 'manage_commenter_pings_options', 'manage_options' ), self::$plugin_basename, array( __CLASS__, 'admin_page' ) );
    	}
    
    	/**
    	 * Adds a 'Settings' link to the plugin action links.
    	 *
    	 * @param array $action_links The current action links
    	 * @return array The action links
    	 */
    	public static function plugin_action_links( $action_links ) {
    		$settings_link = '&lt;a href=&quot;edit-comments.php?page=' . self::$plugin_basename.'&quot; title=&quot;&quot;&gt;' . __( 'Listing', 'pinger-list' ) . '&lt;/a&gt;';
    		array_unshift( $action_links, $settings_link );
    		return $action_links;
    	}
    
    	/**
    	 * Outputs the contents of the plugin's admin page.
    	 *
    	 * @return void
    	 */
    	public static function admin_page() {
    		$pings = self::get_pings();
    		$pings_count = count( $pings );
    
    		echo '&lt;div class=&quot;wrap&quot;&gt;';
    		echo '&lt;h2&gt;' . __( 'Ping List', 'pinger-list' ) . '&lt;/h2&gt;';
    		echo '&lt;p&gt;' . sprintf( __( 'There are %s unique ping locations for this site.', 'pinger-list' ), $pings_count ) . '&lt;/p&gt;';
    		echo '&lt;/div&gt;';
    
    			echo '&lt;div class=&quot;wrap&quot;&gt;';
    			echo '&lt;h2&gt;' . __( 'All Pings', 'pinger-list' ) . '&lt;/h2&gt;';
    			echo '&lt;table padding=2&gt;';
    			echo '&lt;tr&gt;&lt;th&gt;' . __( 'Post', 'pinger-list' ) . '&lt;/th&gt;&lt;th&gt;' . __( 'Source', 'pinger-list' ) . '&lt;/th&gt;&lt;th&gt;' . __( 'Direct Link', 'pinger-list' ) . '&lt;/th&gt;&lt;/tr&gt;';
    
    			foreach ( $pings as $item ) {
    			
    				$pings_url = parse_url(esc_html( $item[2] ));
    				$ping_url = $pings_url[scheme].'://'. $pings_url[host];
    			
    				echo '&lt;tr width=&quot;20%&quot;&gt;&lt;td&gt;&lt;a href=&quot;' . get_permalink( $item[0] ) . '&quot;&gt;'. get_the_title($item[0]) .'&lt;/a&gt;&lt;/td&gt;';
    				echo '&lt;td width=&quot;20%&quot;&gt;' . make_clickable($ping_url).'&lt;/td&gt;';
    				echo '&lt;td&gt;&lt;a href=&quot;'.esc_html( $item[2] ).'&quot;&gt;'. esc_html( $item[1] ) . '&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;';
    			}
    
    			echo '&lt;/table&gt;';
    			echo '&lt;p&gt;' . sprintf( __( '%s pings listed.', 'pinger-list' ), $pings_count ) . '&lt;/p&gt;';
    			echo '&lt;/div&gt;';
    
    
    	}
    } // end PingersList
    
    PingersList::init();
    
    endif; // end if ! class_exists()
    

    The plugin’s crazy basic. It simply checks for unique ping sources and lists them. So if the same ‘main’ site links to you 10 times from 10 separate posts, it lists that. Probably a nice tweak would be to order them by domain, list the posts they link to and from where, and have a group by sort of list, but I didn’t get that far into it. Forks welcome, as are full blown plugins!

  • DreamUp Security

    DreamUp Security

    Not that long ago, I did a ‘DreamUp’ for my company, where we held a Google Hangout and I talked about WordPress security and how to be smarter about things. You can catch the video here:

    http://www.youtube.com/watch?v=Uu-3-o80rEE

    One thing I tried not to do was to list too many plugins and too much code because a lot of security talks are about how we’re all dooooomed, learn all this code. The concept of security is to be smarter about things, so to simplify it, I wanted to talk about the silly things we all do that make us LESS secure, and how to start thinking about what we do to know it’s smarter.

    Here are some of the links from my talk: