Plugin schrijven voorbeeld

Onderstaand voorbeeld is een complete plugin die de titel van een post aanpast.

  1. maak een map mijn-plugin in wp-content/plugins/
  2. maak in de map een bestand mijn-plugin.php

    <?php
    
    /*
    * Plugin Name:       Mijn plugin
    * Plugin URI:        http://www.mijn-domein.nl/
    * Description:       Een korte beschrijving van de plugin.
    * Version:           1.0.0
    * Author:            Jouw naam
    * Author URI:        http://www.mijn-domein.nl/jouw-naam
    * License:           GPL-2.0+
    * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
    */
    
    // laad de stylesheet
    function mijnPluginStyle()
    {
        wp_register_style('mijn_plugin_style', plugin_dir_path(__FILE__) . 'css/style.css', false, '0.0.1');
        wp_enqueue_style('mijn_plugin_style');
    }
    
    add_action( 'wp_enqueue_scripts', 'mijnPluginStyle' );
    
    // laad het script
    function mijnPluginScript()
    {
        wp_register_style('mijn_plugin_script', plugin_dir_path(__FILE__) . 'js/script.js', false, '0.0.1');
        wp_enqueue_style('mijn_plugin_script');
    }
    
    add_action( 'wp_enqueue_scripts', 'mijnPluginScript' );
    
    // voeg loop start hier op elke pagina waar een loop start
    Function mijnPluginLoopStart()
    {
        echo "<h1 class='loop-start'>Loop start hier</h1>";
    }
    
    // Voeg het woord telefoon toe voor elke titel
    add_action('loop_start', 'mijnPluginLoopStart');
    
    function verander_de_titel($title)
    {
          $title = 'Telefoon: ' . $title;
    
          return $title;
    }
    
    add_filter('the_title', 'verander_de_titel');
  3. Maak nu in de plugin map de mappen /css/ en /js/ en maak daarin de style.css en script.js aan zoals in bovenstaande code benoemd.
  4. Beiden bestanden mogen leeg zijn…

Download dit voorbeeld

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.