Wordpress Button Click Event



hourglass_empty

In short, the browser raises an event when the user clicks on a button. When that happens, our code listens for the event and then responds by displaying a dialog. Of course, other libraries, frameworks, or vanilla JavaScript afford this same functionality. Tracking the “Button onClick” event is useful for Facebook Pixel, Google Analytics or Google Tag Manager. Note: It’s assumed that you are already familiar with JavaScript, and using the developer tool in browsers to inspect elements. To learn more about it, read the Inspecting elements and Inserting Custom JavaScript Codes articles. Follow the steps bellow to track Button onClick events. Installing MonsterInsights on WordPress. The first step is to install and activate MonsterInsights on.

3min Read

Struggling to add buttons to your WordPress site? Well, since this platform doesn’t have any default feature that allows you to create one, we have to get some help from plugins. In this tutorial, we’ll show you how to use both WordPress button shortcode and non-shortcode plugins.

What are Shortcodes?

Before we begin the tutorial, we should understand what shortcodes are.

Essentially, shortcodes are simple code wrapped in square brackets — [like this one] — that enable you to add more functionality to your WordPress posts and pages.

Creating a button in wordpress

To use a WordPress button shortcode, simply type them in the post editor and WordPress will render the code and display the result on your website.

The beautiful thing about WordPress button shortcodes is that you can make one on your own if the available codes don’t meet your needs.

In this particular case, we’ll cover creating a WordPress button shortcode to add buttons on your website. But as writing code from scratch is difficult for most people, we’ll teach you how to do it by using plugins instead.

Later on, we’ll also give you an alternative to shortcode plugins if you want a different solution.

So let’s get started, shall we?

How to Add Buttons in WordPress

There’s no doubt that website buttons are a great way to encourage people to perform a specific action such as sign up, log in, download a file, and so on. For example, Amazon uses buttons to make it easy for visitors to add an item to their cart and make a purchase.

You can also insert a button like the one in the example above.

Creating A Button Shortcode Using Plugins

Among many WordPress shortcode button plugins available, our top pick has to be MaxButtons. It has more than 100.000 active installations, is compatible with various WordPress versions, and more importantly, is very easy to use so anyone can utilize it without a problem.

Wordpress

To get started, download and activate the plugin. Then, head over to the MaxButtons menu. Once you’re on the plugin’s dashboard, click Add New and you’ll be taken to the Button Editor. The first thing you need to fill out is the basic information such as the button name (to identify and find a button if you create multiple), URL (the link within the button), and the button text (the text on the button).

In this first section, you can also change the font, text colors, text color hover, and the dimension of the button. All the changes you make can be seen in the live preview on the right side of your screen.

Scroll down, and you’ll see even more options for customizing the look of your buttons.

When you’re satisfied with the button style, click Save at the top of the page.

Now, go back to the MaxButtons dashboard, and you’ll see the shortcode for the button you just created. In the example below, the shortcode is [maxbutton id=”1”].

Inserting Buttons with Shortcode

Button

When you want to insert the button into the WordPress editor, simply copy and paste the shortcode. Alternatively, you can click Add Button and choose the button you want to insert:

After that, modify the button URL and the button text according to what you need for that specific post. That way, you don’t have to create different shortcodes for different posts.

And that’s it! You just created your first ever WordPress button! Cod ghosts multiplayer crack. You may not see the result on the WordPress editor, because the actual button will appear on the post when it’s published.

Alternative to Shortcode Plugins

The biggest drawback of WordPress button shortcode plugins like MaxButtons is flexibility. If you want to add a button with a slight style change, you have to go to the plugin’s dashboard and edit it from there.

So as an alternative, the best bet is to use a non-shortcode plugin like Forget About Shortcode Buttons.

Just like the name suggests, Forget About Shortcode Buttons does not rely on shortcodes. You can create a button immediately in the WordPress editor and style everything from there. Just click the Insert Button icon and you can start modifying your button right away.

The options are pretty much the same as MaxButtons — border style, background color, and the button size. But there’s an extra feature here that you’ll absolutely love: the ability to add an icon to your button. It’s a nice little touch to make your button more unique and beautiful.

If you want to use your current button again in the future, you don’t have to start all over again! Just save it by clicking the Floppy Disk icon. You can later reuse it from the Templates tab whenever you need it.

Once you’re done, click Update to insert the button.

Conclusion

Buttons are very beneficial for any website. They effectively grab attention and contain call-to-action texts that encourage people to perform an action.

As WordPress doesn’t provide any default feature to insert a button to your website, you’ll have to use some additional plugins. There are WordPress button shortcode-based and non-shortcode-based plugins. In this tutorial, you learned how to use both. Which one suits you best? It’s up to you to decide!

The WordPress visual editor is a fine tool for most articles. And it’s only getting better with every subsequent revision. However, not all blog posts are dominated by plaintext. Sometimes when you need to add more complex objects to your posts such as images with specific attributes, I-frames, and most importantly – code, the visual editor starts to lack finesse. For this reason, many individuals prefer to either write or at least modify their code using the HTML editor marked by the “Text” tab.

Using this, content creators can insert very specific HTML tags and style their elements the way they please. Since the visual editor merely guesses what you actually wish to display, there’s a lot of room for error. The HTML editor delivers pinpoint accuracy and I myself use it on a regular basis. However, many individuals find the list of tags above the editor to be somewhat insufficient. These are the buttons that I’m referring to:

Everyone’s needs are different, so the WordPress team has only included those tags which it thinks will be of most value to everyone. However, in the spirit of WordPress’s flexibility, they have also provided us a means of adding custom tags of our own. For example, say you have a specific class that you want to include on specific HTML elements. You can create a WordPress “Quicktag” that provides you with a button outputting any kind of opening and closing tags you want.

Let’s take a look at how to do this using two tags that I regularly use – pre, and div.

Adding Quicktags

Quicktags in the WordPress editor are not generated via PHP, but via JavaScript instead. This means that we won’t be using PHP functions but rather JavaScript ones. Specifically, we’re going to manipulate the object called QTags that gets created whenever its functionality is called for on the editing screen. The JavaScript file responsible for creating QTags is called “quicktags”. So we first check whether or not the “quicktags” JavaScript file is enqueued and ready via:

Once you’re satisfied that the script has been loaded, we use the following Javascript function below to generate a new button. In this example, I’m creating a “pre” tag

This calls the “addButton” function of Qtags to add a new button. You can get the complete documentation of the function on the WordPress Codex page. Only the first two arguments are required, namely the HTML ID and the string to be displayed on the button. The next two arguments provide the opening and closing tags. The one after that deals with something called an “accesskey” and has been deprecated, so I leave it blank, and the “Pre tag” is the pop-up that appears when you hover over the button. Finally, we have the “priority” number which indicates the position at which you want the button to appear. Putting all of this together, we have the final piece of code here:

Wordpress Button Click Event App

I hook into the ” admin_print_footer_scripts” action tag and output a piece of JavaScript code – note how I close the PHP tags in between and reopen them for the ending brackets. You can add this either to a functions.php file or any place where you insert custom PHP code. After I save my changes, I get the following output on my HTML editor:

Wordpress Button Plugin

And you can see below that pressing the “div” button outputs the opening <div> complete with my custom class and everything.

For those who constantly need to create special custom HTML tags for large parts of their posts, the Quicktags functionality is tremendously useful.

Related