functions – WordPress simple ‘Hello World’ plugin – problems

Question

I am an old-school embedded C programmer but a WP noob, so please bear with me.

Here is what I have done so far:-

  1. My function prints out ‘Hello World’ and is registered to a shortcode in the OceanWP theme, and stored here:

> wp-content / themes / oceanwp / custom-shortcodes.php:

<?php
// [helloworld]
function helloworld_func( $atts ) {
    return "Hello World";
}
add_shortcode( 'helloworld', 'helloworld_func' );
  1. I then added the shortcode to the following functions.php file:

>wp-content / themes / oceanwp / functions.php:

include ('custom-shortcodes.php');
  1. I created a new WP Post and dropped the shortcode into it:
[helloworld]
  1. This works BUT:
    if I subsequently add any code which gives an error, the page is affected, the whole theme is affected, the whole site is affected and even the whole WP Dashboard shows the error !

(Reminds me of Windows 3.1 when a driver error crashed the whole OS. For me, the whole WP architecture sucks but I am stuck with it.)
/rant

  1. I could create a child theme, but I prefer to:
  1. So, I created a ‘Hello World’ plugin in the same way:

>wp-content / plugins / hello_world / hello_world_plugin.php

/**
 * Plugin Name: HELLO WORLD PLUGIN
 */
// [hello_world_plugin]
function hello_world_plugin_func( $atts ) {
    return "Hello World Plugin";
}
add_shortcode( 'hello_world_plugin', 'hello_world_plugin_func' );

  1. Problem is: I do not know which functions.php file I need to add the following call to:
include ('hello_world_plugin.php');

so all I am seeing on the page is:

“[hello_world_plugin]”

I found the following file, but it does not seem to contain any other includes:

>wp-includes / functions.php

None of the other plugins seem to have a functions.php file, so I am assuming that my plugin does not need its own either…Is that right?

  1. Furthermore, when I try to Activate my plugin, I get the following error on my WP Dashboard and on the whole damn page:
The plugin generated 213 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.

So, here are my questions:-

A) Do I need to activate my Hello World plugin? If so, do I need to call the following:

register_activation_hook()

On some tutorials, I saw similar plugin code, without any such hook…

B) In which functions.php file am I supposed to drop an include statement, so that the Hello World shortcode plugin is ‘executable’?

I am sure the solution is going to be trivial (always is), but after reading through ALL the WP Dev docs (or what seems like it), I still cannot see what I am doing wrong.

Any ideas?

0
quixote 1 month 2022-08-12T04:04:51-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse