Category: development

  • Gutenberg and Markdown

    Gutenberg and Markdown

    Update:

    Jetpack now has its own built-in Markdown block:

    I’d highly recommend using it instead 🙂


    Original post below:

    I’ve been playing around with Gutenberg a tiny bit recently and have realized that, at least in my case, it kind of eats the Jetpack Markdown module and doesn’t offer full markdown support.

    I honestly know nothing about writing blocks for Gutenberg, but luckily GitHub user nuzzio does 🙂

    This recent, but now closed PR to Jetpack was pretty much everything necessary to build a working block.

    I’ve put my code up on GitHub as a WordPress plugin:

    Like I said, I don’t know what I’m doing–so consider this plugin super beta.

  • Auto-Upgrading users in WordPress

    Auto-Upgrading users in WordPress

    I made a small site recently where I wanted all newly registered users from a specific email domain to automatically be administrators (this is a terrible idea, don’t do it).  The user registration was restricted by Single-Sign-On and 2-Factor Authentication, so I felt relatively safe doing this, especially since it was only a “for fun” project.

    The interesting bit of code that upgraded users to admins is as follows:

    add_action( 'user_register', 'upgrade_email_to_admin', 10, 1 );
    function upgrade_email_to_admin( $user_id ) {
    $user = get_user_by( 'ID', $user_id );
    if ( false !== $user ) {
    $email = $user->data->user_email;
    
       // Only example.com please.
        if ( false === strpos( $email, '@example.com' ) ) {
            return;
        }
    
        $roles = $user->roles;
    
        if ( ! in_array( 'administrator', $roles, true ) ) {
            $user_update = array();
            $user_update['ID'] = $user_id;
            $user_update['role'] = 'administrator';
            wp_update_user( $user_update );
        }
    }
    Code language: PHP (php)

    This is 100% insecure, please do not do this 🙂

  • Validating WordPress attachments with WP-CLI

    Validating WordPress attachments with WP-CLI

    I recently worked on migrating a site to a different server and for one reason or another, some of the images did not come over properly. While I could have just re-downloaded and re-imported all of the media, it would have taken quite a while since the media library was well over 100Gb. Instead, I opted to use WP-CLI to help find what images were missing:

    (more…)