0
/home/<website>/public_html/wp-content/themes/basic/functions.php

At the very end of the file, I have included these 8 lines:

// Add Shortcode
function custom_shortcode() {

    return "Hello world.";

}
add_shortcode( 'test', 'custom_shortcode' );

Doesn't work and I can't figure out why.

I am self-hosted running the free Basic theme. I have the following plugins:

  • Cache Enabler,
  • Disable Emojis,
  • Remove Google Fonts References,
  • UpdraftPlus - Backup/Restore,
  • WP Fastest Cache,
  • WP Statistics.

I've tried various different ways of doing this and have looked up several guides. They all say to do exactly what I'm doing and that it will work, but when I do it it does not work.

2
  • 1
    See the WordPress Handbook regarding Shortcodes and adding them using the init hook.
    – Howdy_McGee
    Commented Sep 22, 2017 at 19:10
  • where into what content do you enter the [test] shortcode? have you verified that you have edited the correct file?
    – Michael
    Commented Sep 23, 2017 at 2:14

2 Answers 2

0

That simply is too late for a shortcode. Just try putting it in your themes functions.php file.

Aside from that, using shortcodes imo are not a good idea. When you decide to change it, it will still stay in the database. This means either going through all places in the DB to clean up the now abandoned shortcode, cluttering your content (and wherever else you enabled the use of shortcodes) with [test] or disabling it by replacing the shortcode with something like an empty string, which means a massive parsing overhead.

3
  • I have done as you suggested and there is no change. functions.php did not exist in the /themes directory so I created it, I don't know enough about php/wordpress to know if there's anything else I should do so I just copy/paste the code and it still didn't work. I will keep in mind what you said about shortcodes, but I only need one and it is very simple.
    – M57
    Commented Sep 22, 2017 at 19:10
  • @M57 Your theme would have the functions.php file, NOT the /theme directory. So if your theme's folder is bob, it would go in wp-content/themes/bob .
    – Lisa
    Commented Sep 22, 2017 at 19:49
  • That's what I originally did, kaiser said to also try finding/creating a file called functions.php and putting the code there. I have done that and it still does not work. I have followed all of all the instructions I can find, and it is not working. I do not know why.
    – M57
    Commented Sep 22, 2017 at 20:00
0

I was originally using the Basic theme with a few plugins. Disabled the plugins, issue persisted. Changed from Basic theme to twentyseventeen. Went in to the plugins.php file in the twentyseventeen theme and copy/pasted my exact code. Now it's working fine.

I don't know enough to know why the Basic theme did not allow me to make a shortcode, but that is the answer to this dilemma: Basic theme will not allow you to make a shortcode the typical way. Whether it's possible at all is for me to figure out in the future (unless someone else happens to know).

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.