Well, it’s 2023, and many experts are predicting a recession on the horizon. And while no one knows how bad it will be or if many countries will avoid recession, one thing is sure: companies that can weather the storm will be the ones that can adapt quickly and efficiently.
Despite this pending threat of economic meltdown, many companies persist with anti-WFH policies, offering ultimatums to employees: return to the office or quit.
It’s 2023, and we still have no simple way to insert Gutenberg blocks into WordPress using wp_insert_post. You’re out of luck if you want to pull content from an API and insert it dynamically with ease. There are methods like parse_blocks and render_blocks but they still require a lot of messing around to work with.
The way Gutenberg blocks work is strange. They’re not stored as an array or structured data in the database. They are stored in the HTML as HTML comments.
I recently worked with GitHub Actions, where I generated a JSON file and then needed to add it to the repository. I parsed some Markdown files and then dynamically created a manifest JSON file of them, committing it into the repo every time a push was made.
I hit a permissions roadblock I embarrassingly spent over an hour solving, and I hope you don’t make the same mistake.
name: Build Blog JSON on: push: paths: - 'blog-posts/\*\*/\*.md' jobs: build: runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v3 - name: Get list of Markdown files run: | cd blog-posts files=($(ls \*.md)) json\_array=() for file in "${files[@]}" do date\_string=$(grep -E '^date: ' "$file" | cut -d' ' -f2) # Use the date command to extract the year, month, and date year=$(date -d "$date\_string" +%Y) month=$(date -d "$date\_string" +%m) day=$(date -d "$date\_string" +%d) json\_array+=($(echo "{\\"file\\":\\"$file\\",\\"date\\":\\"$date\_string\\",\\"year\\":\\"$year\\",\\"month\\":\\"$month\\",\\"day\\":\\"$day\\"}")) done echo "[$(IFS=,; echo "${json\_array[\*]}" | jq -s -c 'sort\_by(.date)')]" > ../static/blog.json - name: Remove trailing comma run: | sed -i '$ s/,$//' static/blog.json - name: Commit changes run: | git config --global user.email "no-reply@github.com" git config --global user.name "GitHub Actions" git add static/blog.json git commit -m "Update blog.json" git remote set-url origin https://x-access-token:${{ secrets.GITHUB\_TOKEN }}@github.com/${{ github.repository }} git push env: GITHUB\_TOKEN: ${{ secrets.GITHUB\_TOKEN }} Now, the important part of my action is the Commit changes action. You need to supply an email and name for the committer. In this instance, I just made up something generic. The first important line is setting the origin URL. We are referencing some variables GitHub creates for us automatically. Notably, GITHUB_TOKEN and repository.
In a hilarious read in the Australian Financial Review, a propaganda piece disguised as an article on remote work and office perks has been published titled WFH raises the bar for offices.
I meant to post this last year, so this has been sitting in my drafts. But I found it so comical and biased that I felt compelled to write about it.
Mirvac, a large Australian property developer with a vested interest in getting people back into the office (because it also owns commercial real estate), has set up a trial space for clients and architectural firms. But, showing just how out of touch they are, a paragraph in the linked story reads.
PHP, the programming language that has been declared dead more times than a cat has lives, is still very much alive and kicking. Despite what some elitist developers may say, PHP is not going anywhere anytime soon.
Despite all the naysayers constantly predicting its demise, PHP continues to chug along, powering some of the biggest websites on the internet.
Let’s start by taking a look at the statistics. According to W3Techs, PHP is currently used by 78.9% of all websites with a known server-side programming language. That’s a pretty impressive number, considering that PHP has been around since 1995. To put it in perspective, that’s longer than some developers have been alive. And the reason why it has been around for so long is that it just keeps getting better.
If you have been a reader of my blog for a while, you would know that I am an avid cryptocurrency enthusiast. I believe in the tech more so than the financial side. I find blockchains fascinating because, despite their perceived complexity, you can implement a blockchain in any programming language; Javascript included.
I thought it would be fun to create a blockchain using TypeScript and then iteratively change the code to offer more flexibility, such as the ability to add metadata into the blocks and query the blocks themselves.
In a recent project, I worked with Markdown files that contained metadata at the top for blog posts. I needed to parse the Markdown with JavaScript and make a set of key/value pairs.
As you can see, the metadata is encapsulated in – – – with the metadata contained within.
// Function to parse metadata from a markdown file const parseMarkdownMetadata = markdown => { // Regular expression to match metadata at the beginning of the file const metadataRegex = /^---([\\s\\S]\*?)---/; const metadataMatch = markdown.match(metadataRegex); // If there is no metadata, return an empty object if (!metadataMatch) { return {}; } // Split the metadata into lines const metadataLines = metadataMatch[1].split("\\n"); // Use reduce to accumulate the metadata as an object const metadata = metadataLines.reduce((acc, line) => { // Split the line into key-value pairs const [key, value] = line.split(":").map(part => part.trim()); // If the line is not empty add the key-value pair to the metadata object if(key) acc[key] = value; return acc; }, {}); // Return the metadata object return metadata; }; You can call this function and pass in the markdown file content. It will return an object with the metadata.
In a WordPress project I am building, I needed a way to programmatically update the post status of a post on the edit screen, specifically in the Gutenberg editor.
Before arriving at the solution below, I first struggled to figure this out. I was trying to call the updatePost method with a post object and modified status, then calling savePost but what kept happening was the post would revert to draft status.
When WordPress introduced the Gutenberg editor, it was a mess, to say the least. Everything was turned upside for developers and things that worked in previous versions were completely broken when Gutenberg was released.
One of the things that were broken in WordPress was the ability to hide a meta box on the editor screen. Instead of setting meta_box_cb to false and expecting the box the hidden, nothing happens.
Well, there is a way you can hide meta boxes by using the following snippet of code inside of functions.php
The Advanced Custom Fields plugin for WordPress is invaluable. It has filled a gap in WordPress for the better part of a decade and is one of the first plugins that I install in a new WordPress installation (I have a lifetime developer licence).
The ACF plugin provides a Javascript API available using acf it has numerous methods for interacting with ACF fields in your WordPress installation. You can programmatically get and set field values, observe for changes and react accordingly.