Half-Elf on Tech

Thoughts From a Professional Lesbian

Tag: wordpress

  • Sidebar Login Widget

    Sidebar Login Widget

    This comes up a lot.

    The basic concept is you want to allow users to log in and out via a sidebar, and never see the admin-end of WP. There’s an awesome plugin called Sidebar Login that already does this, but I decided to play around and make a dead simple widget. There’s no ajax going on here, and very little code since it all calls the built in functions and filters.

    It lets you make a few choices, as to what verbiage you want to use, and if you want to show login/registration info or not. The registration link won’t work if you have registration turned off, natch.

    All the code is here at Hack: Sidebar Login Widget

    Really those hacks are more ‘mu-plugins’ that I don’t want to support, but still wrote, but you get the idea.

    I’ve taken to writing up the code, when I can, for people in the forums a little more, since it helps me as a developer get better with WordPress. I’m still new at wrangling widgets, so this was a new, and interesting, experience for me. That brought up questions for me, as to where people like the plugin ‘settings’ to be.

    [polldaddy poll=6676462]

  • I’ll be in Tybee!

    There’s an upcoming WordPress Community Summit, and I’ll be attending! I was invited before I was employed here at DreamHost (who is providing lunch, yay!), so now I’m serving two purposes! The first is as a community support leader, and in that auspice, I plan to represent the common user. The second is as DreamHosts’s WordPress Support Manager, and there I will represent the support my company provides you DH users.

    Do you have any topics you’d like to see me address at the meetup? Any annoying WordPress/DreamHost issues taht you’re not sure is WP or DH? Leave a comment and I’ll do my best!

  • WordPress Cancel Post Button

    WordPress Cancel Post Button

    Someone asked about this in the forums. I can see why folks would find it useful, so here’s a simple addition to add a ‘Cancel Post’ button to your publishing metabox.

    This plugin checks what post-type you’re on and redirects you correctly for pages and CPTs.

    To Install

    Make a file called cancel-button.php and put it in your mu-plugins folder (if you don’t have one, just make it in /wp-content/). In that file, paste the following (yes there’s no ending PHP tag, it’s okay, you don’t need it):

    <?php
    
    /* 
    Plugin Name: Cancel
    Plugin URI: https://halfelf.org/
    Description: Adds a 'cancel' button to posts, next to Publish.
    Version: 1.0
    Author: Ipstenu
    License: GPL2
    
    */
    
    add_action( 'post_submitbox_misc_actions', 'author_in_publish' );
    
    function author_in_publish() {
        $screen = get_current_screen();
        $cancel = get_admin_url();
        if ( $screen->id == 'post') {$cancel = "edit.php"; }
        else { $cancel = "edit.php?post_type=$screen->id";}
        
        echo "<div class=\"misc-pub-section\"><a class=\"button\" href=\"".$cancel."\" id=\"post-preview\">".__('Cancel Post')."</a><input type=\"hidden\" name=\"wp-cancelpost\" id=\"wp-cancelpost\" value=\"\"></div>";
    }
    

    This will add the button for all your sites on a network, if you happen to use Multisite.

  • Auto Multisite Registration

    Auto Multisite Registration

    There are plugins for this, and I’m rather fond of them. If you just want all new users to be added to some (or all) sites, then I suggest you use Multisite User Management. It’s a great plugin, and lets you pick and chose. But similar to how sometimes you want users to register per site, you may have a situation where you want users to be added to any and all sites when they visit.

    So how do we do this? It’s really not that painful, just make a add-users.php file in your mu-plugins folder with this:

    <?php
    
    /*
    Plugin Name: Add Users
    Description: Add users to blogs when they visit.
    */
    
    function helf_add_users( ) {
        global $current_user, $blog_id;
    
        if(!is_user_logged_in())
        return false;
     
        if( !is_user_member_of_blog() ) {
            add_user_to_blog($blog_id, $current_user->ID, "subscriber");
        }
    }
    
    add_action( 'wp' , 'helf_add_users' , 10);
    

    This code will automatically detect “John Doe is logged in.” and “John is not a member of this site.” If both of those are true, then it says “We will add him as a subscriber.” You can change subscriber as you see fit.

    Would it be more efficient to just do this check on registration? Sure. But there are moments you don’t want this. If you wanted to get extra sneaky, you could set this to only run on a specific page, and make that page password protected.

    If we want to add in a couple extra layers, that’s also not terrible. Let’s say we want people to press a button ‘Join This Site’? I have to admit, this actually took me longer to hash out than I wanted it to, mostly because I decided if I was going to do this, I should do it all the way.

    That’s why I came up with Join My Multisite, which is a handy plugin to give you some more options. It’s per-site configurable, so your site-admins will get to decide if they want everyone logged in user to be added to their site, or if they want a widget, or if they want nothing at all. I put some time into thinking about if it would be a good idea to have the network admin be able to pick, but when I got down to brass tacks, I realized there was no way to easily force a widget (i.e. the button to join the site) for all sites and make it always look good. If you need that, you should fork the plugin and hard-code in the widget.

    Join or Die

    In a way, this is an extension of the old ‘Add Users Sidebar’ plugin, which drifted off into the wayward lands of not-supported. I never looked at that code, though, and instead wrote all this from scratch.

    In addition to all that, Join My Multisite gives each site the ability, if registration is open, to easily make a per-site registration page. That one was Mason James’ suggestion.

  • Instagram oEmbed

    Instagram oEmbed

    This isn’t hard, so it’s just a nice example of how to handle instagram.

    wp_oembed_add_provider('#http://(www\.)?instagram\.com/.*#i', 'http://api.instagram.com/oembed?url=', true);
    

    Per usual, I have a file called oembeds.php in my mu-plugins folder, where I toss all these. This will work with and without www in the URL. I will warn you, Instagram’s embed is ugly! Here’s http://instagram.com/p/QU1OfbHBTe/

    http://instagram.com/p/QU1OfbHBTe/

    See? No formatting, nothing to tell you it’s bloody Instagram.

    Meanwhile, here’s the tweet it came from:

    Much nicer, eh? Of course, Instagram is weird, in that they don’t let you browse images.

  • WordPress Multisite Subdomains Without Wildcards

    The easiest way to run a Multisite network with subdomains is to use wildcard subdomains, because they’ll automagically map anything.yourdomain.com to yourdomain.com and WordPress will in turn translate that for you.

    When you’re on shared hosting, you can’t do that. But don’t worry! If you’re willing to do it manually, every time you have a new subdomain, you can still have subdomains! This is really easy, if weird.

    Go to manage domains on your Panel and click on ‘Add New Domain / Sub-Domain’

    Add your subdomain as ‘mysub.yourdomain.com’ and check the box to remove www (it’s a subdomain, you don’t need it, and it causes weird issues with some plugins).

    Set the Web Directory to where you have your main domain installed (normally this will be /home/username/yourdomain.com ) – yes, I really mean exactly the same. This is what wildcards do for you, but since you don’t have it, you need to do this. It should look like this:

    That’s it! Now you just scroll down and press Fully Host This Domain and wait till it’s provisioned. Once that’s done, you can go to mysub.yourdomain.com and it will take you to yourdomain.com! Add in a subdomain within WordPress called ‘mysub’ and everything will work exactly like it should.