• I am trying to write my first plugin.

    So I read some tutorials and figured I’d start with a simple plugin to make sure I understood everything I read. I just wanted to echo “Hello World” on the screen. Simple.

    I created the plugin and saw it in my admin. I activated it. So far, so good. Then… nothing happens. My code is extremely simple. I’m sure someone will look at it and see the problem.

    I did some more research online and I tried to echo the message several ways:

    <?php
    
    /*
    Plugin Name: Hello
    */
    
    function hello_world() {
     echo "Hello!";
      print("Hello World");
    }
    
    add_action('wp_print_scripts', 'hello_world');
    
    add_action('get_header', 'hello_world');
    
    add_action('get_footer', 'hello_world');
    
    add_action('wp_header', 'hello_world');
    
    add_action('wp_footer', 'hello_world');
    
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • I don’t think that you’re doing anything wrong, other than you’re selecting the wrong hooks.

    For example, the documentation for get_header (https://codex.wordpress.org/Plugin_API/Action_Reference/get_header) says

    The following example will enqueue stylesheets conditionally for a different headers. This is just one example of how you may use the hook, and will use a secondary template file of header-new.php

    Have you looked at the source code of your page after running your plugin? I bet you’ll find something interesting.

    Read this page (it’s a long one) very carefully. Lots of actions there to hook to:

    https://codex.wordpress.org/Plugin_API/Action_Reference

    Where did you want this to appear, anyway? In a post or page? You could achieve that easily by turning it into a shortcode.

    Do let us know how you get on. Developing for WordPress is rather addictive.

    Moderator bcworkz

    (@bcworkz)

    Read the list of actions?? That’s like reading a dictionary! 😛

    It works for some people apparently. The hooks are usually fairly well named. You might find what you’re looking for by searching in
    http://developer.wordpress.org/reference

    I usually find the right hook by looking through source code of a function whose return I wish to alter. Nearly all hooks have inline docs so they’re easy to find when looking at source with syntax highlighting.

    For example, if you look inside a template loop, you’ll likely see a call to the_content(). Look at the source code.
    https://developer.wordpress.org/reference/functions/the_content/#source-code

    Hey look! A filter called ‘the_content’. We can see it passes the current content to your callback, then after fixing ‘]]>’, echoes out whatever your callback returns.

    Try adding your callback to ‘the_content’ filter (not action, filter). Have it return “Hello world!” instead of echoing. See what effect that has on your site 😉

    You will discover the result is not very useful, but it illustrates the power (and danger) of filters. Have fun!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Help with my first simple "Hello World" Plugin’ is closed to new replies.