0

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:-

Added a Shortcode in a Theme

  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:

Try to Add a Shortcode as a Plugin

  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?

5
  • (A) The register_activation_hook is optional, so you don't need to use it. (B) You don't need a functions.php file, you can just use your main plugin file hello_world_plugin.php for this. What are you seeing when viewing the shortcode in your front-end? Are you seeing the exact shortcode or the return output? By the looks of it, your shortcode should work and output your return output.
    – DeltaG
    Aug 12, 2022 at 12:14
  • Also, please check your hello_world_plugin.php for spaces or characters after your php closing tag. This might be the problem for the "The plugin generated 213 characters of unexpected output during activation." message.
    – DeltaG
    Aug 12, 2022 at 12:17
  • Find out what errors with wordpress.org/support/article/debugging-in-wordpress and also avoid same code in plugin and theme because of name collisions, unless using namespaces.
    – birgire
    Aug 12, 2022 at 14:36
  • Does the plugin file (wp-content / plugins / hello_world / hello_world_plugin.php) have <?php at the top? It seems like an obvious thing but it's a mistake I've made more than once.
    – Pat J
    Aug 12, 2022 at 15:21
  • Looks like I was missing the <?php at the top of the file. I can now activate it and it returns the content, rather than just the name of the shortcode. THANKS ALL. I am going to go back to elementary school now...
    – quixote
    Aug 12, 2022 at 16:26

2 Answers 2

3

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

None. You don't do this. The file with the Plugin Name: HELLO WORLD PLUGIN comment is your main plugin file. To load it you activate the plugin in Plugins. This is all described clearly in the Plugin Handbook:

Now that you’re editing your new plugin’s PHP file, you’ll need to add a plugin header comment. This is a specially formatted PHP block comment that contains metadata about the plugin, such as its name, author, version, license, etc. The plugin header comment must comply with the header requirements, and at the very least, contain the name of the plugin.

Only one file in the plugin’s folder should have the header comment — if the plugin has multiple PHP files, only one of those files should have the header comment.

After you save the file, you should be able to see your plugin listed in your WordPress site. Log in to your WordPress site, and click Plugins on the left navigation pane of your WordPress Admin. This page displays a listing of all the plugins your WordPress site has. Your new plugin should now be in that list!

To address some of your other comments:

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.

It sounds like your plugin is missing an opening PHP tag, <?php, so when your plugin is activated the web server is loading your code as text and printing it to the browser.

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

If you write code with errors you will get errors. WordPress is not an operating system and plugins are not drivers. The same thing happens if you put bad code into a Laravel app, an Express.js app, or a million other frameworks. You are writing code that is running on the same server as WordPress. If that code has a fatal error in it then of course it's going to affect the rest of the site. That has nothing to do with WordPress's architecture.

2
  • Thank you so much for your detailed responses, all. I am now going to stand on the 'naughty step' for the next week...
    – quixote
    Aug 12, 2022 at 16:28
  • My point is that an error in one module should not bring down the entire admin panel
    – quixote
    Aug 13, 2022 at 10:53
0

I was missing the <?php at the top of the file. Thanks to all for your help !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.