Category Archives: WordPress Tips

Create Additional Search Keywords for Your Gutenberg Editor Blocks

While working on my Gutenberg Editor Development Course I stumbled upon a cool feature for Gutenberg Blocks that let’s someone to find your block using words not found in the title of your block.

Searching for Blocks in Gutenberg

One of the easiest ways to find a specific block in Gutenberg is by using the search field.  By default, if your title has a keyword someone searches for, it will show your block in the results.

Sometimes though, we want our block to show for keywords that are not included in our title.

gutenberg-search-for-block-feature

Here is an example of searching for “Image” and receiving multiple matching blocks

You can see in the example above that if we search for an “Image” block we get blocks that do not have “Image” in the title.

This is accomplished using the “keywords” parameter when registering your block.

Using the Keyword Setting When Creating a Block in Gutenberg

When you create a block using registerBlockType() you can set a parameter called “keyword.”  This parameter takes an array of words that if searched for, will include your block in the results.

custom-keyword-gutenberg-block.png

Custom keyword allows block to be searched for using keywords not found in the block title

In the example above we have added extra keywords to a custom block.

Here is the code to make this work.  (Notice we are also escaping them using the wp.i18n library for localization).

This allows us to have our block show up for the search terms “zac,” “fire,” and “fuego,” which are not found in our block title.

I would suggest looking at what keywords the default WordPress blocks use and leverage some of those keywords in your blocks.

Learn More About Gutenberg Development

If you would like to learn more about creating custom blocks or developing with Gutenberg in general, please check out my Gutenberg Development Course!

How to Add Custom Icons to Gutenberg Editor Blocks in WordPress Using Dashicons or SVG

While working on my upcoming Gutenberg Editor Development Course I came across the question, “How do you create a custom icon for a block in the new Gutenberg editor.”  This post explains my process in figuring this out 🙂

Block Icons in the Gutenberg Editor

With the new Gutenberg editor in WordPress you have the option to select an icon to go along with your custom block.

Screen Shot 2017-12-07 at 4.51.01 PM

Example of icons with default block.

Using Default WordPress Dashicons as Block Icons

When you use registerBlockType() to create a new block, one of the parameters is “icon”.  You can use any default WordPress Dashicons as an icon.

In the example below we are using one of the default dashicons named “admin-settings“.  That will give us a settings icon like this.

default-gutenberg-icon.png

Example of custom block with ‘admin-settings’ dashicon

Here is the code to make that work (focus just on the icon parameter):

Interestingly, if you look in the Gutenberg Core file ‘components/dashicon/index.js‘ you will see that dashicons are not being used the same way in blocks as they are in as in places like admin menus.  Instead, there is a case statement against each default argument, assigning it the proper SVG path.

gutenberg-dashicon-case-statement.png

Example of Dashicon component in Gutenberg Core assigning default SVG paths.

Creating Custom Gutenberg Block Icons Using SVG Paths

A lot of folks are going to want to create custom icons like this one.

custom-gutenberg-icon.png

Example of Custom Block with Custom SVG Icon

In the example above, I used an SVG icon from this collection here.  You can also create you own custom icons.

What you want to do is get the SVG path for your custom icon and create a custom SVG icon using that path.

Here is the code example from the image above.  Pay attention to the top, where iconEl is created then passed as the value to the registerBlockType() icon parameter.

You can use this model in your own code and simply replace the path ‘d’ value with the path for your own custom icon svg.

To Learn More About Gutenberg Development

To learn more about creating custom blocks and more with the new Gutenberg editor for WordPress, check out my Gutenberg Development Course.

“It works!” DesktopServer not working solution for Apache on Mac

I am not sure what I did to cause this problem, but recently, all of my DesktopServer server sites, including localhost began displaying the message: “It works!”

I contacted the lovely folks at DesktopServer and got this quick fix (for Macs):

This solved the problem and I’m back up and running.  Since I couldn’t find the solution online before I contacted support, I thought I would share 🙂

Local from Flywheel Saves Database SQL Files When Site is Stopped

I was recently following some troubleshooting steps for Local and removed the docker component that Local automatically sets up via an app called VirtualBox, which it installs during setup.

Right after I did this I realized that I may have also deleted all of the databases for my sites.  Turns out I did.

NOTE: If you remove a site in Local as part of testing, I suggest you do not delete the associated files as well (ie app/public etc).

Luckily, Local has a nice feature that automatically backs up all of your database tables whenever you stop a site.

Since Local stalled and wasn’t stopped properly, I did loose a little bit of data, but not too much 🙂

Screen Shot 2017-05-17 at 2.15.18 AM.png

Thanks Clay and the Local folks for helping me figure this out (check out the forum post).

If you’ve been liking Local, but also notice it freezes up or doesn’t work sometimes, I feel you 🙂

I haven’t done this in the past, but I may look into a DB backup solution for local dev just in case of things like this (my files I save via Dropbox).

Learn How I Built the JavaScript for WordPress Master Course and Teaching Site on the “How I Built It Podcast”

“If you’re looking to setup an online course just grab this podcast!”

I love hanging and chatting Joe Casabona.  Naturally, I was excited when I found out he wanted to chat with me on his How I Built it Podcast about how I built my JavaScript for WordPress Master Course.

A lot of folks have asked me content related questions about the course, but this is the first time I really go in depth about actually building the course, from researching content, to building a team, to picking an LMS, all the plugins I used and several of the snags I hit.

It was really fun to talk about all of this and I share a lot of information.  If you’re interested in how things are built and the behind the scenes technologies, I would definitely recommend listening to this episode.

Students may particularly appreciate what has gone into the course.

Episode 9: Zac Gordon & Javascript for WordPress

 

How to Save Wistia Video Player Speed in Local Storage with JavaScript, the Wistia API (and WordPress)

I love Wistia, the professional video hosting company with amazing stats.  I have used them since early on and host all the JavaScript for WordPress videos with them.

They have had an API for a while that lets you control player speed.  Recently, they updated their player  and added speed control right into the controls.

Wistia Video Player Speed

Video Speed Built into Wistia Player

However, there is no caching mechanism for this, so users have to reset the player speed for each video.  This requires 3 clicks, can be a bother, and takes away from the learning experience.

So… this being a JavaScript course and us having covered local storage, I decided to write a little custom code to listen for changes to player speed and save the latest speed in local storage.

Then, when any video on the site loads, it first checks if there is a speed saved in local storage and sets the player speed.

The Basic JavaScript Code

Here is the basic code I used to get this working.  I tried to comment it pretty well so it makes sense what is happening.

// Get the Wistia Player
window._wq = window._wq || [];

// Apply to all Wistia Videos
_wq.push({ id: "_all", onReady: function( video ) {

	// Get speed from local storage
	var localStorePlayerSpeed = JSON.parse( localStorage.getItem( 'video_speed' ) );

	// If local storage speed exisits, set player speed
	if ( localStorePlayerSpeed !== null ) {
		video.playbackRate( localStorePlayerSpeed );
	}

	// Listen for changes in player speed
	video.bind("playbackratechange", function( playerSpeed ) {

		// Save the updated player speed to local storage
	  	localStorage.setItem( 'video_speed', JSON.stringify( playerSpeed ) );

	});
}});

You should be able to just drop this JavaScript into one of your JS files and have it work.

Enqueuing Our JS for WordPress

Since I am using this with WordPress I also have to enqueue this code into my functions.php file.  I would ideally like to release this as a little plugin, but for now adding it to my theme works.

Here is the code I added to my theme functions.php file:

function wistia_customizations() {
	wp_enqueue_script( 'wistia-player', get_stylesheet_directory_uri() . '/js/wistia-player.js', array(), 1.1, true);
}
add_action( 'wp_enqueue_scripts', 'wistia_customizations' );

Of course then I would have my JavaScript in the first code snippet in a file named wistia-player.js.

Taking it Further

Although this does work for my purposes, if a user switches computer or goes from watching on the desktop to mobile it will not remember their last setting.

Taking is one step further could involve saving the player speed as User Meta Data using the REST API.

I’ll hopefully tackle this and release this feature as a little no configuration plugin at some point before the JavaScript for WordPress Master Course is complete.

If you use this or something similar or have faced this problem, please let me know what direction you went!

 

 

Deciding What Development Tools to Teach in my JavaScript for WordPress Master Course

js-dev-tools

Since the beginning of 2016 I have been working on my JavaScript for WordPress Master Course.  In the first part of this course I have a section on JavaScript Development Tools.

For the past few moons I have been trying to figure out exactly what JavaScript Development Tools to include in this section and in what order to cover them. It has been quite a process figuring all of this out, so I thought I would share a bit about it.

First Off, What Tools Do We Include?

The first thing I had to figure out what exactly what tools I should include.  Here is the list I finally came up with:

  1. Coding Environments
  2. Command Line Tools
  3. Code Repositories
  4. Task Runners
  5. Transpilers
  6. Error Linting and Debugging
  7. Testing

For the most part I had a pretty good idea of what to include from the beginning.  However, it did take a while to figure out the best order.  I’ll discuss that at the end, but first I thought I’d share a bit about the specific tools I chose from each type of tool.

Coding Environments

When I taught at Treehouse I used Sublime Text for all of the WordPress Development videos.  However, since then, Atom, from Github, came out and I have grown to like it for it’s sleek feel and powerful extensions.

In my informal surveying of WordPress developers, most folks use one of these two coding environments.  However, a few folks do use IDEs (Integrated Coding Environments) and I knew I had to include one in the course.

For the IDE, I chose WebStorm, the JavaScript IDE from the folks at JetBrains, who make the PHP IDE, PhpStorm.

In my course I primarily use Atom with a number of my favorite packages installed.  I do not really go into Sublime Text because it is so similar to Atom and because my course advisor, Wes Bos, has a book on Sublime Text.  I go into WebStom a bit to show what an IDE can offer and then refer people to Know the Code from course advisor Tonya Mork, who gets deeper into developing with an IDE.

Command Line Tools

One of the most important lessons I learned from doing the Week of REST Bootcamp with my friends at Human Made was that it is important to cover basic command line skills before getting into using other tools: navigating directories, understanding how commands and options work, etc. So I do a bit of that to start.

Luckily you do not need to know too much hard core Unix to use JavaScript Development tools, which is a good thing.  However, when installing and customizing tools it does help to have an understanding of the where different configurations are located and how to configure them for easier use.

When it comes to accessing the command line in the course I like to use Atom with the Terminal Plus Package.  However, I highly recommend folks check out the free Command Line Power User Course from Wes Bos for tips on how to use iTerm on the Mac with ZSH, Z and other goodies.  Although I do not explain how I do it in my course, I do have a customized command prompt 😉

Code Repositories

Two important code repositories stand out for JavaScript Development Tools: NPM and Github.

While npmjs.com is the go to repository for loading modern JavaScript libraries, it is also a development mindset in and of itself.  Rely on both libraries and small reusable bits of code from other developers.  I kind of think of it like as plugins are to WordPress so NPM packages are to modern JavaScript development.

In the course we use NPM quite a lot and really try to get folks used to using it as part of their development flow. I do not go into Github too much, just enough to clone repositories and manage your own projects a bit.  From there I recommend folks checkout the official Try Git interactive tutorial.

Task Runners

Task Runners help you run and manage other command line tools.  Three options clearly stood out for talking about in this course: Grunt, Gulp and Webpack.

In our last native JavaScript project of Part I, we use Grunt.  I chose this because it was the earliest of the bunch and has more similar configuration to Webpack in my opinion.  Although we do not use Gulp, I do show how you can do the same things with Gulp as we do with Grunt, just a little differently.

I don’t get into Webpack until later in the course when we get into JavaScript Libraries and Frameworks in Part II.  I figured it was a good idea to switch to this since most React developers prefer React from what I have gathered.  I also want people to have the experience of switching task runners, since being able to adept and switch between tools is an important skill set for a JavaScript developer.

In terms of what the task runners are doing, it generally breaks down to some combination of bundling files, minifying, linting, watching for file changes, and running a local server with live reloading on file changes.

Transpilers

The section on Transpilers exists primarily to discuss Babel, which we use to convert new JavaScript features into code that current browsers will understand.  Now that JavaScript releases new features on an annual basis, having a tool like Babel is essential for being able to let us use new JavaScript in our code and still have our apps work across environments.

In a later section in the course we go over a few of the features of ES6 and work on rebuilding our custom app, VanillaPress with ES6 conventions.  However, next year, there will be ES7 features and the year after that ES8.  I do not plan on updating the course with every new feature of JavaScript at this point, although I will cover the basics and links to resources like Wes Bos’s ES6 course that covers new features in depth.

Error Linting and Debugging

I love error linting tools 🙂  Something about knowing as I code if I’m making any mistakes saves some time, particularly searching for those silly misspellings and similar type things.  I explain the relationship between JSLint, JSHint and ESLint and show how you could use each with our task runners.  In terms of what to choose, I generally agree that ESLint is a good way to go.

For debugging I show how you can use Chrome Developer Tools to insert breakpoints and walk through your code for debugging, although we also get plenty of experience using console.log for simple stuff.  Although I don’t go into depth with it, I do mention that you can also do this internally with WebStorm.

Testing

This was the hardest section for me to do, mostly because I was not sure what testing framework to pick.  Most developers I talk to in the WordPress ecosystem do not take a Test Driven Development (TDD) approach in their projects.  However, I want folks who take my course to have a good foundation as a JavaScript developer outside of the WordPress world so I did feel it was important to at least get folks familiar with how testing works.

At first I thought to use Jasmine since it was one of the older and simpler testing frameworks.  However, a lot of folks like Mocha.  In the end, I decided to go with Tape, in part due to JS education guru, Eric Elliot’s, article on why he uses Tape over Mocha and in part because I felt it was the simplest of the bunch for someone new to testing.

In Part II of the course I will mention a few other testing frameworks as well, since some framework communities have preferred testing frameworks.

Now, What Order to Teach All of This?

In the end I settled on the order above for covering these different tools, however, it took me a while to settle on this order.

Earlier on I had covered Linting quite early and tried to save Git till the very end.  I also did not have command line tools in it’s own section originally and just talked about it in the Coding Environments section.

In fact, originally most of these sections were part of one big Task Runner or Coding Environments section since you commonly use most of these tools through your task runner or coding environment.

Learning about Tasks Runners before learning about the other tools does not give you much to do with a Task Runner, though.  For that reason we start off with a basic configuration for the Task Runners with a number of tools that we don’t get into until later sections.

This follows a teaching convention of dropping folks into a working project that might not make sense on a micro level but is helpful for understanding macro concepts.  Ultimately I felt this approach worked better than trying to talk about each tool individually before combining them in with a task runner, especially since it would mean running all the tools just from the command line to start, which is not really a common practice.

Did I Miss Something?  Have Opinions Otherwise?

My primary occupation is as a teacher.   I have a development background but do not code JavaScript applications day in and out as my primary gig (although that does appeal to me sometimes).

For this reason I rely a lot on people in the community who work with these tools regularly and encounter limitations and strengths of these (and other) tools in different types of environments and projects.  So, if you have thoughts on these different tools, as well as ones that I did not mention, please leave a comment and share your experience!

Want to Learn More?  Enroll in the JavaScript for WordPress Master Course

If you are newer to all of this I would highly recommend you enroll in my JavaScript for WordPress Master Course, where I go in depth into everything I talk about above, plus a whole lot more!