Tag100 Days to Offload

A 100-days of writing project where you offload ideas, thoughts and just what’s going on. Helps with the whole isolation thing, and helps people get back into the idea of long-form writing and reading.

WordPress Attachments

I’ve been using WordPress for a long time. It’s a great platform for building websites. Especially blogs like mine. It gives you all the tooling you need to be successful, quickly. With the vast ecosystem of plugins available, you can do practically anything without needing to do much coding at all.

This is all not to say that it doesn’t suffer from warts that have accumulated over the years. One of the biggest, for me, is the Media manager. This tool allows you to helpfully upload images and other file types into WordPress, access them and manipulate them, from many different places. This is great, and it works well. Except the parts where it treats each file as a post and gives that resulting post a full vanity URL at the root of your domain. For example: If I uploaded a file called “hello-world.jpg”, a vanity URL for this file would be created at https://degruchy.org/hello-world.jpg/. Note the trailing slash. Yes, this is a full page that is dedicated to showing that one picture plus any metadata associated with it (often little to none).

This situation has been around for years. So long that I doubt they even consider fixing it, given the amount of crap that is likely deeply intertwined with it. Making media it’s own type of content, tracked differently is likely a major upset to the ecosystem and not something they’re willing to do lightly. Thus our situation.

Remediation

Thankfully, we are able to remedy this situation ourselves, with little code of our own. Keep in mind, you want to do this in a child theme, so that if your theme gets updated to a shiny new version, you don’t loose your changes.

Template Files

Because WordPress handily exposes template types in generally easy to figure out names, we can actually define how the attachment page is to be designed. This would normally be an awesome way to modify images on a photoblog or portfolio, but in our case, we don’t need it.

<?php
    /**
     * Attachment page. Redirect to file url, these are useless.
     */
    wp_redirect(
        wp_get_attachment_url(), # Actual Attachment URL
        301                      # Redirect code: 301 Moved Permanently
    );
?>

That’s it. Create a new file in your child-theme called attachment.php and paste that in. What this does is tell WordPress to create a redirection. To what? The file itself. That way, if someone does somehow land on .../hello-world.jpg/, they’ll just be pushed to the file itself. The redirect also tells routers, caching services and all manner of other network equipment that this redirect should be followed always because it’s a “Moved Permanently” code.

There. Now we don’t have to worry about those pesky top level URLs being entirely useless. You can, also, remove the links there already, but that is a bit of database surgery that you likely want to avoid. Instead, you can prevent them from being created in the first place. In your child-theme’s functions.php, add:

...

function twentytwenty_child_remove_attachment_url( $link, $attachment_id )
{
    return wp_get_attachment_url( $attachment_id );
}
add_filter( 'attachment_link', 'twentytwenty_child_remove_attachment_url', 10, 2 );
...

Obviously, you’ll want to use a more specific name for your function, rather than twentytwenty_child_remove_attachment_url. Just make sure both the function name, and the filter reference are changed. I leave the naming up to the reader to best implement.

This will tell WordPress that any time a new file is uploaded, look at this function to help build an attachment link. The function then tells WordPress to do nothing (i.e. don’t make a fancy vanity URL). They’ll still be accessible by their post ID (i.e.: .../post=1234), but that’s better than crowding the top level “space” with useless crap.

Note: As was pointed out to me, this code will remove permalinks to your media completely (see: below). However, my intention is to have the file URL be the canonical source for that file. This fits with my desire. You can feel free to just add the attachment.php file and ignore the permalink removal.

As you can see, this newly uploaded image does not have a permalink URL.

Balancing Humors

As a someone who is introverted, dealing with people is often difficult. Having to meet new people, or interact with them at parties is mentally tiring and something I dread having to do. So, how do I deal with it? Especially since my job is interacting with lots of people who I don’t know, and who are often upset by technology.

Humor.

It’s my defense mechanism, my way of connecting on a more basic level and it’s a good way to get people to relate to you. Humor helps diffuse situations and it more importantly, it helps me stay sane. Laughing about computer problems and other stupid absurdities often staves off the lingering stresses that the regular world pushes into your consciousness.

Pragmatism vs. Idealism

I spend an awful lot of time thinking about and promoting free and open source software and ideals. I try and use as much of it as I can day-to-day. I have Debian (Stable) installed on my only laptop. I use Firefox where possible. I turn in documents either in PDF or ODT (or whatever the format equivalent). I even seek out open source online tools or services. To that end, I even have a virtual RMS application installed to nag me about non-free package installed.

Ugh, I know RMS. I can’t really use this laptop without the firmware, though!

My Dirty Non-Free Secret

Hi, my name is Nathan. I use non-free software regularly (Hi, Nathan).

I wish I could say that I was a good little free-software supporter. I am a FSF member and keep my membership in good standing. But it’s all a lie.

Yes, I am also an iPhone user. (shocked gasps). I also use Office 365, heck, I even like it (boos, angry grumbling). I also own and play games on the Nintendo Switch. I know, I know, I’m sorry. I can’t help it. My wife is not a believer, nor is my iPad loving son. I like these services too much to give them up or implement them myself. I don’t have the resources (time, money or otherwise). I wish I did.

I’d like to think that this is about pragmatic choices. I need to have access to my email, texts and other smartphone conveniences, but there exist alternatives for even that. I value my privacy, but apparently not that much.

So, am I wrong to be living in both worlds? Is it weird to want to be purely in the idealistic world? I wish I knew

Writers Block

I don’t know what to write about. Here’s some stuff I’ve been doing:

  • Using just my browser at work with Email, Teams, OneDrive, OneNote and more (using the new Edge)
  • Looking at servers for home hosting
  • Playing lots of Animal Crossing
  • Doing school work
  • Not losing weight
  • Not getting enough sleep
  • Feeling pretty blah

Binding Emacs

Emacs is quite a beast. It’s default keybindings (non-exhaustive) list is formidable. While many of us enthusiasts are able to get around using the defaults (which are found in more places than just Emacs), or just cheat and layer a (arguably superior) input method on top, you’ll often find yourself tweaking the defaults a bit.

This is one of those nice bits about Emacs. You can tweak it almost endlessly. Keybindings can be remapped, or even supplanted. If you wanted to take any binding that mapped to undo, and instead pass it to your own custom function. You can do that. You don’t even need to know what those bindings are ahead of time. Any package that comes along and also adds a map to undo, will instead have it pointed correctly.

My Bindings

My Emacs Custom Bindings (some of)

I tend to break my keybindings down into two general categories: Personal and package. My personal bindings are for things I make myself (functions) and changing around the default bindings to my liking. This tends to be a smaller list, since I’m focusing on just what is available. Packages, since I use use-package, are bound with the package. The reason for this is to ensure that if I add or delete a package from my Emacs configuration, the whole thing is contained in a block of configuration, instead of spread out across multiple files.

(use-package embrace
  :ensure t
  :after (expand-region)
  :bind (("C-c E" . embrace-commander)
	 ("C-c e" . embrace-add)))

This is something I use when I implement the complicated mappings for dired:

(use-package dired
  :bind (:map dired-mode-map
	      ;; Reuse the same dired window
	      ("RET" . dired-find-alternate-file)
	      ("^"   . (lambda()
			 (interactive)
			 (find-alternate-file "..")))
	      ;; Use 'open' to open the file with the user's choice
	      ("E"   . ndegruchy/open-in-external-app)
	      ;; Get the file size(s)
	      ("; d" . dired-get-size)
	      ;; Toggle omit
	      ("; o" . dired-omit-mode)
	      ;; Close the frame, useful when using dired by itself
	      ("; q" . delete-frame))
  :config
  (setq dired-listing-switches "--group-directories-first -alh"
	dired-dwim-target      t)
  (setq-default dired-omit-files-p t)
  (require 'dired-x)
  (require 'dired+)
  (put 'dired-find-alternate-file 'disabled nil)
  :hook (dired-mode . dired-hide-details-mode))

Keeping things organized is awesome, especially if you end up endlessly tweaking, like I do.

Emacs is what you make of it. While the defaults are great and are somewhat useful as-is, you’re going to run into situations where the defined bindings are less than optimal. I actually unbound a couple of keys because the way I type, when I hit M-x, I end up hitting M-z accidentally (my keyboard has a very shallow key travel).

If you do end up adventuring into the den of bindings for Emacs, take the ‘bind-key’ package/elisp with you. It helps make the binding, unbinding and defining of maps easier.

I hope you learn something from this. Customizing Emacs is a fun activity, but it can be a confusing exercise.

© 2020 Verily

Theme by Anders NorénUp ↑