WordPress hooks explained: actions and filters
Last updated: 31 December 2025
WordPress hooks zijn het fundament van aanpasbaarheid. Ze stellen je in staat WordPress te modificeren zonder kernbestanden te wijzigen. Dit artikel legt uit wat hooks zijn en hoe je ze effectief gebruikt.
Wat Zijn Hooks?
Hooks zijn specifieke punten in WordPress waar je eigen code kunt "inhaken". Er zijn twee typen:
Actions: Voer iets uit op een bepaald moment Filters: Wijzig data voordat die wordt gebruikt
Analogie
Denk aan hooks als aansluitpunten:
- Actions zijn stopcontacten: je plugt iets in en het doet iets
- Filters zijn waterleidingen: data stroomt erdoorheen en je kunt het aanpassen
Actions
Actions voeren code uit op specifieke momenten:
// Een action toevoegen
add_action('hook_naam', 'jouw_functie', 10, 2);
// Parameters:
// 1. Hook naam
// 2. Callback functie
// 3. Prioriteit (standaard 10, lager = eerder)
// 4. Aantal argumenten
Veelgebruikte Actions
// Bij WordPress initialisatie
add_action('init', function() {
// Registreer custom post types, [taxonomieën](/kennisbank/wordpress-taxonomieen-uitleg)
});
// In de <head> sectie
add_action('wp_head', function() {
echo '<meta name="author" content="Jouw Naam">';
});
// Bij het laden van scripts
add_action('wp_enqueue_scripts', function() {
wp_enqueue_style('mijn-stijl', get_stylesheet_uri());
wp_enqueue_script('mijn-script', get_template_directory_uri() . '/js/app.js');
});
// Na het opslaan van een post
add_action('save_post', function($post_id, $post) {
// Doe iets na het opslaan
}, 10, 2);
// Bij inloggen
add_action('wp_login', function($username, $user) {
// Log inlogactiviteit
}, 10, 2);
Eigen Actions Maken
// Definieer een hook in je code
do_action('mijn_custom_hook', $parameter1, $parameter2);
// Anderen kunnen er nu op inhaken
add_action('mijn_custom_hook', function($param1, $param2) {
// Custom code
}, 10, 2);
Filters
Filters wijzigen data:
// Een filter toevoegen
add_filter('hook_naam', 'jouw_functie', 10, 2);
// BELANGRIJK: Filters moeten altijd iets returnen!
Veelgebruikte Filters
// Wijzig de titel
add_filter('the_title', function($title) {
return strtoupper($title); // Maak hoofdletters
});
// Wijzig de content
add_filter('the_content', function($content) {
// Voeg tekst toe aan het einde
return $content . '<p>Bedankt voor het lezen!</p>';
});
// Wijzig excerpt lengte
add_filter('excerpt_length', function($length) {
return 30; // 30 woorden
});
// Wijzig "Read more" tekst
add_filter('excerpt_more', function($more) {
return '... <a href="' . get_permalink() . '">Lees verder</a>';
});
// Wijzig admin footer tekst
add_filter('admin_footer_text', function($text) {
return 'Powered by WordPress | Beheerd door Jouw Bedrijf';
});
// Body classes toevoegen
add_filter('body_class', function($classes) {
if (is_user_logged_in()) {
$classes[] = 'logged-in-user';
}
return $classes;
});
Eigen Filters Maken
// Definieer een filter
$waarde = apply_filters('mijn_custom_filter', $originele_waarde, $extra_param);
// Anderen kunnen nu filteren
add_filter('mijn_custom_filter', function($waarde, $param) {
return $waarde * 2;
}, 10, 2);
Prioriteit en Volgorde
De prioriteit bepaalt wanneer je hook uitgevoerd wordt:
// Prioriteit 5 - wordt eerst uitgevoerd
add_action('init', 'functie_eerst', 5);
// Prioriteit 10 (standaard)
add_action('init', 'functie_normaal', 10);
// Prioriteit 99 - wordt laatst uitgevoerd
add_action('init', 'functie_laatst', 99);
Praktisch Voorbeeld
// Je wilt de output van een andere plugin aanpassen
// Die plugin gebruikt prioriteit 10, dus gebruik 11
add_filter('andere_plugin_output', 'mijn_aanpassing', 11);
Hooks Verwijderen
// Verwijder een action
remove_action('wp_head', 'wp_generator');
// Verwijder een filter
remove_filter('the_content', 'wpautop');
// Verwijder met specifieke prioriteit
remove_action('init', 'functie_naam', 15);
Anonieme Functies Verwijderen
Anonieme functies zijn lastig te verwijderen:
// Dit werkt NIET
add_action('init', function() { /* code */ });
remove_action('init', function() { /* code */ }); // Werkt niet!
// Oplossing: gebruik named functions
function mijn_init_functie() { /* code */ }
add_action('init', 'mijn_init_functie');
remove_action('init', 'mijn_init_functie'); // Werkt wel
Hooks in Thema vs Plugin
functions.php (Thema)
// Thema-specifieke aanpassingen
add_action('after_setup_theme', function() {
add_theme_support('post-thumbnails');
add_theme_support('title-tag');
register_nav_menus([
'primary' => 'Hoofdmenu',
'footer' => 'Footermenu'
]);
});
Plugin
// Plugin-specifieke hooks
register_activation_hook(__FILE__, 'mijn_plugin_activate');
register_deactivation_hook(__FILE__, 'mijn_plugin_deactivate');
function mijn_plugin_activate() {
// Maak database tabellen, flush rewrite rules
flush_rewrite_rules();
}
Veelgebruikte Hook Patronen
Shortcode Maken
add_shortcode('contact_knop', function($atts) {
$atts = shortcode_atts([
'tekst' => 'Neem contact op',
'url' => '/contact'
], $atts);
return '<a href="' . esc_url($atts['url']) . '" class="button">' .
esc_html($atts['tekst']) . '</a>';
});
// Gebruik: [contact_knop tekst="Bel ons" url="/bel"]
Custom Post Type Registreren
add_action('init', function() {
register_post_type('portfolio', [
'label' => 'Portfolio',
'public' => true,
'has_archive' => true,
'supports' => ['title', 'editor', 'thumbnail'],
'menu_icon' => 'dashicons-portfolio'
]);
});
Admin Kolommen Toevoegen
// Voeg kolom toe
add_filter('manage_posts_columns', function($columns) {
$columns['thumbnail'] = 'Afbeelding';
return $columns;
});
// Vul kolom
add_action('manage_posts_custom_column', function($column, $post_id) {
if ($column === 'thumbnail') {
echo get_the_post_thumbnail($post_id, [50, 50]);
}
}, 10, 2);
AJAX Handler
// Voor ingelogde gebruikers
add_action('wp_ajax_mijn_actie', 'handle_ajax');
// Voor niet-ingelogde gebruikers
add_action('wp_ajax_nopriv_mijn_actie', 'handle_ajax');
function handle_ajax() {
check_ajax_referer('mijn_nonce');
$result = ['success' => true, 'data' => 'Resultaat'];
wp_send_json($result);
}
Debugging Hooks
Welke Hooks Worden Uitgevoerd?
add_action('all', function($hook) {
if (strpos($hook, 'wp_') === 0) {
error_log("Hook: {$hook}");
}
});
Hook Inspectie
// Bekijk alle callbacks op een hook
global $wp_filter;
print_r($wp_filter['the_content']);
Query Monitor Plugin
Installeer Query Monitor om hooks visueel te inspecteren in de admin bar.
Best Practices
1. Gebruik Specifieke Hooks
// Slecht: te algemeen
add_action('init', 'laad_mijn_assets');
// Beter: specifiekere hook
add_action('wp_enqueue_scripts', 'laad_mijn_assets');
2. Check Context
add_action('admin_init', function() {
// Alleen op specifieke admin pagina
if (get_current_screen()->id !== 'edit-post') return;
// Je code
});
3. Prefix Je Functies
// Voorkom conflicten met unieke prefix
function mijnbedrijf_custom_function() {
// code
}
add_action('init', 'mijnbedrijf_custom_function');
4. Return Bij Filters
// FOUT: vergeten te returnen
add_filter('the_title', function($title) {
$title = 'Prefix: ' . $title;
// Oeps, geen return!
});
// GOED
add_filter('the_title', function($title) {
return 'Prefix: ' . $title;
});
More information: WordPress.org documentatie
Frequently Asked Questions
How long does it take to implement this?
Implementation time varies per situation. Simple configurations can be done within an hour, more complex setups may take several hours to a day.
What are the costs?
Costs depend on your hosting provider and package. Many basic features are included for free, advanced features may incur additional costs.
Do I need technical knowledge?
You need little technical knowledge for the basics. Most hosting providers offer extensive documentation and support to help you.
Was this article helpful?
Compare hosting packages directly to find the best choice for your situation.
Related articles
What is web hosting? Explanation for beginners
Discover what web hosting is and how it works. Complete explanation about servers, domains and different hosting types for beginners.
What is VPS Hosting?
VPS hosting explained: what is a Virtual Private Server, who is it suitable for and what are the advantages compared to shared hosting?
What is an SSL Certificate?
Everything about SSL certificates: what is SSL, why do you need it and how do you recognize a secure website? Essential for every website.
What is Uptime in Web Hosting?
What does uptime mean in web hosting? Learn about uptime percentages, SLA guarantees and why 99.9% uptime is important for your website.
How much storage do I need for my website?
Discover how much disk space you really need for your website. Practical guide with examples per website type.