Skip to main content
How to use Hooks
Updated over a year ago

What hooks are in WordPress

In WordPress, we have two hooks – filters and actions. Both allow changing how certain parts of code behave without changing the code of the plugin. This means that you can adjust how the plugin works for your needs.

Why are WordPress hooks so cool?🔥

They allow you to adjust some features for your custom needs. Of course, as plugin code is available, you can make this change directly in the code, but we don't recommend using this method because:

  • It requires advanced programming knowledge

  • It is easy to break the plugin

  • After the next official update of the plugin, your whole changes will be overwritten.

With hooks, you can achieve similar things with less risk and keep your changes after each update.

What do I need to use hooks?

Firstly, the plugin needs to have those hooks in its code. Remember that you can’t change everything in any way you want. There are limitations to what developers allow you. But do not worry; this is in good faith, as some changes may break the plugin, so such things are not editable.

To use hooks, unfortunately, you need basic HTML and PHP knowledge to be able to operate with code because those changes are made with code snippets. Our documentation will provide full knowledge and code example for certain snippets, but it is still important to know how this works.

Are there any risks?

Unfortunately yes. Risks depend on the method you choose (you can find those later in the text). But in general, if you modify code for your needs and you will make a mistake, even a typo, it can stop some features of the plugin or even cause an error on your page, which will make it unreachable to visitors.

We highly recommend leaving making these changes to people with basic programming knowledge or at least choosing Code Snippet Plugin as your insert method to reduce the risk or error. Also, we recommend checking if you have a backup of your page, just to be sure.

Can you help me with adding snippet?

We added hooks to our plugin to allow you to adjust plugin behavior to your needs, but it is not possible for us to debug custom snippets for all customers. Please, remember that you put those snippets at your own risk. Our support team will not help you to insert snippets and will not debug your custom snippets.

Furthermore, if you have any troubles with the plugin, please deactivate all your snippets related to the Surfer plugin and check if the issue is still there before you will reach the support team.

Do I lose my changes after the plugin update?

If you make a change with one of our recommended ways, you won’t. But there is a slight risk that we may change the feature that uses this hook or remove a certain hook completely in some updates, and this may stop your hook from working. But this can happen with a bigger update, like an update from version 1.x.x to 2.x.x, and we will definitely include information about the removed hooks in the change log, so nothing should surprise you.

How to use code snippets on my page?

There are three methods you can use, with different pros and cons. We described all of them, so you can choose the best method for you.

Please remember to have an active Surfer plugin before you will run any snippets. Also, please check if you have the latest version of the Surfer plugin, as hooks were introduced with different updates, so if you have an old version of the plugin, a certain hook may not be available there.

Use Code Snippet Plugin

The first method is to use Code Snippet Plugin like this one:

Pros:

  • The simplest way to include a snippet

  • Code editors in plugins have code verifiers, so it is harder to make a mistake in the code

  • You can easily enable and disable certain snippets

  • If something fails, plugins should be able to disable the malfunctioning snippet instead of showing an error.

🚫Cons:

  • You need to install an additional plugin

Recommended for: Everyone, especially less experienced users.

Steps to insert snippet:

  1. Install the snippet plugin using your favorite method.

  2. Go to the Snippets menu and click “Add New”

  3. Put the title for the snippet and the code, and choose to run the code globally, you can also put the description if you want (please note that fields may be different if you have chosen a different code snippet plugin).

  4. Click “Save Changes and Activate”

And that’s it. You have a working snippet right now.

Insert code in functions.php

The second method requires inserting code directly in the functions.php file in your Theme. To do this, you can edit Theme files via FTP.

Pros:

  • You do not need an additional plugin for this

🚫Cons:

  • If you switch theme or update it, you will lose your snippet

  • To disable the snippet, you need to remove code from the file directly

  • If you make a mistake, your page will throw an error for sure

Recommended for: Users who do not want to install additional plugins.

The steps are following:

  1. Open your favorite FTP client

  2. Connect to your page

  3. Go to: wp-content / themes / <your_theme_name>

  4. Find the functions.php file and edit it with your favorite code/text editor

  5. Put the snippet at the end of the file and save it.

After the file is uploaded to your server, the snippet will be running. Instead of using FTP, you can also edit files from wp-admin using the built-in editor or some file managing plugin. But please note that some servers have restrictions for the built-in editor, so this option might not be available for you.

If you are not sure how to connect to your page via FTP, we recommend using the plugin solution described above or asking your server support for help.

Create your small plugin

The last method is making a small, one-file plugin with code snippets only. This solution is something between the first and second one. It is more complex than the first one, and still, you will have the additional plugin, but it will be lightweight. It will be independent of updates of other plugins or themes.

Pros:

  • You have snippets in a separate plugin, so they won’t stop working because of other plugins or theme update

  • If something is wrong, you can just disable the plugin

🚫Cons:

  • If you do something wrong, your page will throw an error

  • You will still have an additional plugin

  • Most complex to use

Recommended for: Advanced users who have programming knowledge and know what they do.

This method is a little bit more complex, and the steps are following:

1. You need to create a plugin file, create a file with the name index.php (it can be any other name, but .php is mandatory), and put content like that:

<?php
/**
* Plugin Name: [Surfer] My Snippet
* Version: 1.0.0
* Author: Your Name
* Author URI: https://surferseo.com
* Plugin URI: https://surferseo.com
* Description: My Custom Snippet
*/

// Place for snippet


You can choose your own name for the plugin, your own description, version, author, and URLs, but you need to keep this structure.


2. The next step is to put your snippet below this opening code:

<?php
/**
* Plugin Name: [Surfer] My Snippet
* Version: 1.0.0
* Author: Your Name
* Author URI: https://surferseo.com
* Plugin URI: https://surferseo.com
* Description: My Custom Snippet
*/

add_filter( 'surfer_allowed_post_types', 'my_surfer_allowed_post_types' );
function my_surfer_allowed_post_types( $types ) {
return array( 'post', 'page' );
}


You can use multiple snippets in this one file, just put the next snippets below, but remember to use unique function names!

3. Now, put your index file into a directory. The name of the directory is not important, but we recommend starting it with “surfer” to put your custom plugin near the original Surfer plugin in the plugins directory. So it can be “surfer-my-snippet.”

For the next step, you have two options:

4a) Now you can upload your folder into the wp-content / Plugins directory via FTP
4b) You can put your directory into a .ZIP archive and install it like a plugin using: the wp-admin → Plugins → Add New → Upload Plugin menu.

5. Now activate the snippet plugin, and it is done.

Did this answer your question?