Allow Your Users to Login to Your WordPress Site With Their Email Addresses

By adding this class to your WordPress theme or plugin. You can allow your users to login with their email address


/**
 * Add support to login with email address
 *
 * Initialize the class
 *     $variable = new Your_Prefix_Login_With_Email();
 */
class Your_Prefix_Login_With_Email {

    /**
     * Initialize the class and set its properties.
     */
    public function __construct() {
        add_filter( 'authenticate', array( $this, 'add_email_login' ), 20, 3 );
    }

    /**
     * Add email login
     *
     * @param obj     $user     the users information
     * @param string  $username the username
     * @param string  $password the password
     */
    public function add_email_login( $user, $username, $password ) {
        if ( is_email( $username ) ) {
            $user = get_user_by( 'email', $username );
            if ( $user ) {
                $username = $user->user_login;
            }
        }
        return wp_authenticate_username_password( null, $username, $password );
    }
}

Auto Create Constants for Your WordPress Plugins and Themes

This class will create constants from your plugin’s or theme’s header information. With the added bonus to define custom constants.


/**
 * Define the plugin's core constants
 * and create custom constants
 *
 * Initialize the class
 *      $constants = new Your_Prefix_Constants();
 *
 * return the core constants
 *      TEXTDOMAIN_DIR;
 *      TEXTDOMAIN_URL;
 *      TEXTDOMAIN_NAME;
 *      TEXTDOMAIN_DESCRIPTION;
 *      TEXTDOMAIN_AUTHOR;
 *      TEXTDOMAIN_AUTHORURI;
 *      TEXTDOMAIN_TEXTDOMAIN;
 *      TEXTDOMAIN_DOMAINPATH;
 *      TEXTDOMAIN_PREFIX;
 *
 * Plugins Only
 *     TEXTDOMAIN_PLUGINURI;
 *     TEXTDOMAIN_NETWORK;
 *     TEXTDOMAIN_TITLE;
 *     TEXTDOMAIN_AUTHORNAME;
 *
 * Themes Only
 *     TEXTDOMAIN_THEMEURI
 *     TEXTDOMAIN_TEMPLATE
 *     TEXTDOMAIN_STATUS
 *     TEXTDOMAIN_TAGS
 *     TEXTDOMAIN_VERSION;
 *
 * example Create a new constant
 *     $custom_constant = $constants->define_new_constant( $name, $value );
 *
 * example return custom constants
 *     PLUGIN_NAME_CUSTOM_CONSTANT;
 */

class Your_Prefix_Constants {

    /**
     * the type of constants to use
     *
     * @var string
     */
    protected $constant_type;

    /**
     * Array of the file header information
     *
     * @var array
     */
    protected $file_header_array;

    /**
     * Backtrace of the class instance
     *
     * @var string
     */
    protected $backtrace;

    /**
     * The plugin directory name
     *
     * @var string
     */
    protected $plugin_dir;
    /**
     * The theme directory name
     *
     * @var string
     */
    protected $theme_dir;
    /**
     * The convert dashes in the text domain to underscores
     *
     * @var string
     */
    protected $formated_textdomain;
    /**
     * convert formated text domain to all uppercase
     *
     * @var string
     */
    protected $constant_prefix;
    /**
     * Suffix to use on the directory constant
     *
     * @var string
     */
    public $dir_suffix;
    /**
     * Suffix to use on the URL constant
     *
     * @var string
     */
    public $url_suffix;
    /**
     * Suffix to use for the prefix constant
     *
     * @var string
     */
    public $prefix_suffix;

    /**
     * The Header data
     * 
     * @var array
     */
    private $data;

    /**
     * Initialize the class and set its properties.
     *
     * @since  1.0.0
     * @see http://wordpress.aspcode.net/view/63538464303732726617528/how-to-get-main-plugintheme-file
     */
    public function __construct() {
        $this->backtrace = debug_backtrace();
        $this->dependencies();
        $this->constant_type = $this->constants_type();
        $this->file_header_array = $this->get_file_header();
        $this->get_textdomain();
        $this->applied_filters();
        $this->define_core_constants();
    }

    public function dependencies() {
        if ( !function_exists( 'get_plugins' ) ) {
            require_once ABSPATH . 'wp-admin/includes/plugin.php';
        }
    }

    function get_textdomain() {
        if ( $this->constant_type == 'plugins' ) {
            $this->formated_textdomain = str_replace( '-', '_', $this->file_header_array['TextDomain'] );
        } elseif ( $this->constant_type == 'themes' ) {
            $this->formated_textdomain = str_replace( '-', '_', $this->file_header_array->get( 'TextDomain' ) );
        }
        $this->constant_prefix = strtoupper( $this->formated_textdomain );
    }

    public function constants_type() {
        // the backtrace array
        $backtrace = $this->backtrace;
        // the file's absolute directory path in an array
        $path = explode( '/', $backtrace[0]['file'] );
        // the wp-contents absolute directory path in an array
        $content_path = explode( '/', WP_CONTENT_DIR );
        // the last item in the array. the wp-content directory
        $last = end( $content_path );
        $path_array = $path;
        // remove all array items before the wp-content directory
        foreach ( $path as $key => $value ) {
            if ( $value == $last ) {
                break;
            }
            unset( $path_array[$key] );
        }
        // reorder the array
        $new_array = array_values( $path_array );

        return $new_array[1];
    }

    public function get_file_header() {

        $this->data = '';
        
        if ( $this->constant_type == 'plugins' ) {
            // the backtrace array
            $backtrace = $this->backtrace;
            // the file's absolute directory path in an array
            $path = explode( '/', $backtrace[0]['file'] );
            // pop off the plugin file
            $removed_file = array_pop( $path );
            // pop off the plugin file's parent directory
            $removed_dir = array_pop( $path );
            $this->plugin_dir = $removed_dir;
            $this->data = get_plugin_data( WP_PLUGIN_DIR . '/' . $this->plugin_dir . '/' . $removed_file, false, false );
        } elseif ( $this->constant_type == 'themes' ) {
            $this->data = wp_get_theme();
        }

        return $this->data;
    }

    private function format_texdomain() {
        if ( $this->constant_type == 'plugins' ) {
            $this->formated_textdomain = str_replace( '-', '_', $this->$file_header_array['TextDomain'] );
            $this->constant_prefix = strtoupper( $this->formated_textdomain );
        } elseif ( $this->constant_type == 'themes' ) {
            $this->formated_textdomain = str_replace( '-', '_', $this->$file_header_array->get( 'TextDomain' ) );
            $this->constant_prefix = strtoupper( $this->formated_textdomain );
        }
    }
    /**
     * Filters for customizing the class
     *
     * @return mixed the filter's values
     */
    public function applied_filters() {
        /**
         * The plugins directory suffix
         *
         * @param string  the custom plugin directory constant suffix
         */
        $this->dir_suffix = apply_filters( $this->formated_textdomain . '_dir_suffix', 'DIR' );
        /**
         * The plugins URL suffix
         *
         * @param string  the custom plugin URL constant suffix
         */
        $this->url_suffix = apply_filters( $this->formated_textdomain . '_url_suffix', 'URL' );
        /**
         * The plugins prefix suffix
         *
         * @param string  the custom plugin prefix constant suffix
         */
        $this->prefix_suffix = apply_filters( $this->formated_textdomain . '_prefix_suffix', 'PREFIX' );
    }

    /**
     * Define the core constants
     *
     * Defined from the plugin header info,
     * the plugin directory and the plugin URL.
     *
     * The core constants will be prefixed with
     * the plugin name
     *
     * @return const the core constants
     */
    public function define_core_constants() {
        if ( $this->constant_type == 'plugins' ) {
            $this->define_plugin_constans();
        } elseif ( $this->constant_type == 'themes' ) {
            $this->define_theme_constants();
        }
        // Define the plugin prefix constant
        if ( !defined( $this->constant_prefix . '_' . $this->prefix_suffix ) ) {
            define( $this->constant_prefix . '_' . $this->prefix_suffix, $this->formated_textdomain . '_', true );
        }

    }

    public function define_plugin_constans() {
        // Define the plugin directory constant
        if ( !defined( $this->constant_prefix . '_' . $this->dir_suffix ) ) {
            define( $this->constant_prefix . '_' . $this->dir_suffix, WP_PLUGIN_DIR . '/' . $this->plugin_dir . '/', true );
        }
        // Define the plugin URL constant
        if ( !defined( $this->constant_prefix . '_' . $this->url_suffix ) ) {
            define( $this->constant_prefix . '_' . $this->url_suffix, plugins_url( WP_PLUGIN_DIR . '/' . $this->plugin_dir . '/' ), true );
        }
        // Define the plugin header data as constants
        foreach ( $this->file_header_array as $key => $value ) {
            if ( !defined( $this->constant_prefix . '_' . strtoupper( $key ) ) ) {
                define( $this->constant_prefix . '_' . strtoupper( $key ), $value, true );
            }
        }
    }

    public function define_theme_constants() {
        // Define the theme directory constant
        if ( !defined( $this->constant_prefix . '_' . $this->dir_suffix ) ) {
            define( $this->constant_prefix . '_' . $this->dir_suffix, get_template_directory() . '/', true );
        }
        // Define the theme URL constant
        if ( !defined( $this->constant_prefix . '_' . $this->url_suffix ) ) {
            define( $this->constant_prefix . '_' . $this->url_suffix, get_template_directory_uri() . '/', true );
        }
        // Define the theme Name constant
        if ( !defined( $this->constant_prefix . '_' . strtoupper( 'Name' ) ) ) {
            define( $this->constant_prefix . '_' . strtoupper( 'Name' ), $this->data->get( 'Name' ), true );
        }
        // Define the theme ThemeURI constant
        if ( !defined( $this->constant_prefix . '_' . strtoupper( 'ThemeURI' ) ) ) {
            define( $this->constant_prefix . '_' . strtoupper( 'ThemeURI' ), $this->data->get( 'ThemeURI' ), true );
        }
        // Define the theme Description constant
        if ( !defined( $this->constant_prefix . '_' . strtoupper( 'Description' ) ) ) {
            define( $this->constant_prefix . '_' . strtoupper( 'Description' ), $this->data->get( 'Description' ), true );
        }
        // Define the theme Author constant
        if ( !defined( $this->constant_prefix . '_' . strtoupper( 'Author' ) ) ) {
            define( $this->constant_prefix . '_' . strtoupper( 'Author' ), $this->data->get( 'Author' ), true );
        }
        // Define the theme AuthorURI constant
        if ( !defined( $this->constant_prefix . '_' . strtoupper( 'AuthorURI' ) ) ) {
            define( $this->constant_prefix . '_' . strtoupper( 'AuthorURI' ), $this->data->get( 'AuthorURI' ), true );
        }
        // Define the theme Version constant
        if ( !defined( $this->constant_prefix . '_' . strtoupper( 'Version' ) ) ) {
            define( $this->constant_prefix . '_' . strtoupper( 'Version' ), $this->data->get( 'Version' ), true );
        }
        // Define the theme Template constant
        if ( !defined( $this->constant_prefix . '_' . strtoupper( 'Template' ) ) ) {
            define( $this->constant_prefix . '_' . strtoupper( 'Template' ), $this->data->get( 'Template' ), true );
        }
        // Define the theme Status constant
        if ( !defined( $this->constant_prefix . '_' . strtoupper( 'Status' ) ) ) {
            define( $this->constant_prefix . '_' . strtoupper( 'Status' ), $this->data->get( 'Status' ), true );
        }
        // Define the theme Tags constant
        if ( !defined( $this->constant_prefix . '_' . strtoupper( 'Tags' ) ) ) {
            define( $this->constant_prefix . '_' . strtoupper( 'Tags' ), implode( ', ', $this->data->get( 'Tags' ) ), true );
        }
        // Define the theme TextDomain constant
        if ( !defined( $this->constant_prefix . '_' . strtoupper( 'TextDomain' ) ) ) {
            define( $this->constant_prefix . '_' . strtoupper( 'TextDomain' ), $this->data->get( 'TextDomain' ), true );
        }
        // Define the theme DomainPath constant
        if ( !defined( $this->constant_prefix . '_' . strtoupper( 'DomainPath' ) ) ) {
            define( $this->constant_prefix . '_' . strtoupper( 'DomainPath' ), $this->data->get( 'DomainPath' ), true );
        }
    }

    /**
     * Define a custom constant
     *
     * The custom constant will be prefixed
     * with the plugin name.
     *
     * @param string  $name  the constant name
     * @param string  $value the constant value
     * @return const  the custom constant
     */
    public function define_new_constant( $name, $value ) {
        $format = strtoupper( str_replace( array( '-', ' ' ), '_', $name ) );
        $name = $format;
        if ( !defined( $this->constant_prefix . '_' .  $name ) ) {
            define( $this->constant_prefix . '_' . $name, $value , true );
        }
    }
}
WordPress

My WordPress Wishes

Once I discovered WordPress, I never looked back. Yes, I’m a WordPress fanboy. I will sing it’s praises until my throat is sore. Still, with the recent barrage of WordPress bashing, it has me thinking. How could WordPress be even better? Well, in this post I’ll speak my mind on what I think are some shortcomings and areas that could use some significant improvement in WordPress.

Not Just a Blog

If you know anything about the history of WordPress you’ll know that it is a fork of a really old the blogging platform p2. Because of this WordPress was conceived as another blog platform. Well, 12 years later you, me , and anyone else that uses or develops for WordPress knows that it’s not just a blog anymore. It’s a CMS. I used to preach “It’s a blog platform that can be used as a CMS”, but I’ve taken a 360 view on that. It’s a CMS whether you like it or not.

This is the approach I’d like WordPress to take in the years to come. I’d like to see the core WordPress dev team to approach WordPress development as a CMS, not a blogging platform. For instance, the main index.php page is setup to automatically list the basic Posts.

I don’t think this really needed to be the default any more.  Right now, if you want to make a front page that isn’t a list of blog posts. You have to create a special “Home” page, and configure the settings to use this page for your front page.That’s kinda of a pain, not hard to do, just annoying.

WordPress powers over 20% of all the websites on the interwebs. How many of those site are just a blog? How many are using WordPress as a full on CMS? I feel like the sites that use WordPress as a CMS, not just a blog, are being ignored. If WordPress development was approached as a CMS and not just a blog, then we would get a more robust and well rounded platform to build great websites with.

Stop the Bloat, Stop the Bloat Baby

WordPress is getting pretty bloated as of late, and it shows no signs of trimming down anytime soon. On the contrary, there’s plans to add even more. Don’t get me wrong I love new features as much as the next geek, but where does it stop? Do all these features need to be in the WordPress core?

I say no! Right now there are 93 JavaScript libraries included in core. Yes, 93. Some of them are used by the admin, some are really useful for rounding out the WordPress functionality, but not all of them.

Take Image Cropper for instance. The very first one on the list. Right next to the name it says “Image cropper (not used in core, see jcrop)”. Then why is it there? Why would there be an other image cropping library when most users will just end up using the one that is used by core media functionality? Don’t get me wrong. I’m not bashing Image Cropper. I just don’t see the need for this to be included in the WordPress core. It’s just making WordPress fat.

Should I Plug This?

This part goes hand in hand with the previous section about bloat. I really feel that some of the newer features and soon to be features could be best served as plugins. Rather than sticking them in core.

Some that come to mind are the Distraction Free Writing, Front End Editing, and the Customizer. Yes, The customizer has been getting rave reviews and many devs have been championing it, but does this really need to be in core?

I mainly work for agencies that provide websites for business clients. Rule one for building a site for business that is trying to promote it’s brand is: Never let them have the ability to change the design. Ever!

It’s not nice to say, but most clients are pretty dumb. They make really bad choices when it comes to web design. If we allowed clients to do everything they wanted. The web would be full of websites like this (OMG there’s music now!).

In that respect, the customizer is all but useless. I’ve experimented in adding non-design settings to the customizer. Like Google Analytics ID or other arbitrary settings, but the clients got confused. They didn’t understand why there’s a preview of the site when nothing they do changes it. They also found it a bit hard to navigate the customizer with everything stacked all together  in one column. It worked out a lot better to place these settings in another custom area all together.

Now, if you are developing WordPress themes to be sold to the masses. Then yes, by all means use the customizer. Give your customers the ability to build the site they want.

With so many commercial theme developers already packaging plugins with their themes. Then why can’t a customizer plugin be included with that. Why does it have to be in core?

My felling is that unless a feature is bring something that would be beneficial by all or most WordPress users. Then make it a plugin. Keep the bloat down. I know the core WordPress dev teams  like to think they’re making improvements that would be beneficial to all, but even if features are popular should they really be in core, or as a plugin.

Post Meta API

This was announced in November 2013. When I read this, I did a silly little jig in my chair. I have to say, the thing I hate the most in WordPress development is making custom meta boxes and option fields. That’s why I use ACF, but would rather have a system like CMB or Redux built into core. When I heard they were developing a core API for this. Well, I couldn’t have been more stoked.

Well, over a year later and the development really has gone much further than the original commits. There hasn’t been any progress in three months at the time of this post.

I could really care less about front end editing. It doesn’t impress me at all as a developer, but a built in API for custom fields. Yeah, baby. That’s the stuff. No more relying on plugins or third party scripts. No more frustration in writing the same functions over and over again. But… nothing.

Oh, WordPress core devs, please push this one out. This should be easy peasy stuff. Heck you could just make a few tweaks to CMB add that to core, and call it a day. Why is  an API that could really improve the quality of WordPress theme and plugin development being pushed aside for a feature that’s more gimmick that helpful? Arrrrg! Get Er’ Done!

Out With the Old

Lastly, I’d like to address the overall quality of code in the WordPress core. I think it’s really great that backwards compatibility is very important to the core developers, but where do you draw the line?

My thinking is this. We all know it’s very important to keep WordPress and plugins up to date. Especially from a security prospective, so if updating is this important why are we holding onto extremely outdated and poor quality code?  Supporting themes and plugins that aren’t fresh out of the gate is important, but shouldn’t there be a cut off?

As I stated in the beginning of this post. There’s been a lot of WordPress bashing going on lately. One of the things that is pointed out over and over again, is how badly coded WordPress is. Well, I have to admit it’s true. It’s sad to say, but WordPress core if filled with a mix of procedural and OOP code all mangled together. With DRY coding being the standard now a days, WordPress is as WET as a dog out in the rain in the middle of a lake being spayed with a firehose.

What kills me, is that the excuse for this is backwards compatibility, but that’s not how it should work. When you want to support backwards compatibility. You just don’t leave the code as is and work around that. You refactor the code to the current standards and make sure that it is still accessible from an outdated approach. For a certain amount of time, then you depreciate that old nasty code.

There is no reason that the WordPress core couldn’t be refactored to a MVC OOP model, or something alike. All this old code is just straight procedural functions. You can take the components needed from an OOP model and wrap them in functions, so it’s still backwards compatible and easy to implement in themes and plugins.

Yeah, it would take a lot of man hours, it would be hard, and it would take time away from new features, but the overall rewards would so be worth it. WordPress would be more stream line, easier to customize, and it’s life span would be greatly lengthened.

There was a question going around not to long ago asking “What could kill WordPress”? Well, I think this is it. If the core team doesn’t come to the realization that the base code they’re working on is, well let’s face it, old and out of date with current standards. Then sooner than later, WordPress will be left in the dust by some young devs who make the next big CMS.

Adapt or die. There quite a few of us that have seen the next big thing come and go on the internet in it’s short existence. How many of you laugh now about having a MySpace page, going to chat rooms, or having a GeoCities site?

I don’t want WordPress to die. I love WordPress. I love developing for WordPress. I love telling people how great of a CMS WordPress is. It would be a sad day when I have to give up on WordPress because it’s just not relevant anymore.

Conclusion

Well, that’s a but a few of items on my WordPress wish list. You may not agree with me, but take as you will. I’m just one dev with my opinion, but I would love to hear what you’d like to see what WordPress bring in the future.