How can I add a custom shortcode?

I’m having trouble adding the most basic of shortcodes, and I’m wondering if I’m doing something wrong (based on several tutorials I’ve read), or if I have to do something special to get it to work with X Theme.

Adding some code like this to my functions.php child theme ends up just displaying the shortcode itself, not the output:

function s100atest() {
return ‘hello’;
}
add_shortcode(‘certificatenumber’, ‘s100atest’);

or

function s100atest() {
$s100aOutput = ‘hello’;
return $s100aOutput;
}
add_shortcode(‘certificatenumber’, ‘s100atest’);

I’ve seen some tutorials that use ‘init’ and things that talk about registering the shortcode with your theme and stuff, so we’ve also tried:

function s100atest() {
return ‘hello’;
}
add_action( ‘init’, ‘register_shortcodes’);

function register_shortcodes(){
add_shortcode(‘certificatenumber’, ‘s100atest’);
}

None of them seem to work… they just output [certificatenumber] in the text, so WordPress isn’t seeing it as an actual shortcode. What am I missing? Thanks!

Hello @cgWerks,

Thanks for writing in! :slight_smile:

I can see that you’ve used the add_shortcode() function properly.

It might be that there is some code in functions.php that is triggering this not to work or there might be an error on the element you have declared this shortcode. I’m not really sure.

Can you share us the following login details in a secure note so we can take a closer look?

  • Website URL
  • Wordpress Admin username/password

Thank you.

The code is in the child-theme functions.php, and the shortcode doesn’t work anywhere, even just added to the end of a typical page. I’ll try to attach the login info to a secure note. Thanks!

Hi,

I copied your code to one of my test site and the it seems to work.

There is something in your site that is preventing it from working.

You could try testing for a plugin conflict. You can do this by deactivating all third party plugins, and seeing if the problem remains. If it’s fixed, you’ll know a plugin caused the problem, and you can narrow down which one by reactivating them one at a time.

Let us know how it goes!

I’m still having no success in getting it to work. Turning off all the plugins didn’t work on my install. The only thing that works is switching to Twenty-Seventeen theme (though, then I’m adding the code directly to the functions.php file, instead of the child).

Should I be able to add shortcodes via the X-Theme child functions.php?

Hello there,

Yes, you should add the shortcodes on the functions.php of your X child theme. When you re-activate the child theme, please don’t forget to activate Cornerstone on the plugins page.

Let us know how it goes.

Sorry, that didn’t work. As soon as I switch from Twenty-Seventeen to X, and re-enable Cornerstone, then the shortcode just shows as the raw bracketed shortcode text instead of returning the result.

Hi there,

Please also provide us with your FTP details so that we can check your site files.

Thank you.

I attached the FTP info above.

(BTW, it might be a nice idea to add a link to a little video or tutorial in your standard responses about how the secure notes work. Maybe it’s just the UI-guy in me, but it might be confusing to have to leave a response, then see that button to add the secure note.)

1 Like

Hi,

The sftp login provided doesn’t seem to work.

I tried activating your main xtheme then added the code in the functions.php and it seems to work.

So it looks like there something in your child theme that prevents it from working.

Can you try downloading a fresh copy of child theme and install it to your site.

Then add just the code for your shortcode in functions.php to test.

Don’t forget to back up your old child theme files.

Thanks

Sorry about the SFTP, I guess the host uses a non-standard port number.

OK, yes, there does seem to be something in the child functions PHP that is making it not work, or rather, the position in the file seems to be the issue. I’m not sure what is causing it at this point, but by moving the shortcode code higher in the file, I was able to make it work.

I wasn’t aware that the position of code mattered in functions.php, but I suppose this indicates some problem with the code chunk right above where it stops functioning? Anyway, thanks for your help!

Hi There,

There are some syntax errors in your code:

// Disable hint when login fail. ie. don't tell if username or password was wrong.
// -------------------------------------------------------------------------------

function one_default_error_message() {
    return 'Something is wrong!';
}
add_filter( 'login_errors', 'one_default_error_message' );

$forgot_link = wp_lostpassword_url( get_permalink() );
return "Something is wrong! <a href='$forgot_link' title='Lost Password'>Forgot your password?</a>";


// Remove version from parameter tags.
// -----------------------------------

function remove_wp_version_from_scripts( $src ) {
    if ( strpos( $src, 'ver=' ) )
        $src = remove_query_arg( 'ver', $src );
        return $src;
    }
add_filter( 'style_loader_src', 'remove_wp_version_from_scripts', 9999 );
add_filter( 'script_loader_src', 'remove_wp_version_from_scripts', 9999 );

The correct code should be:

// Disable hint when login fail. ie. don't tell if username or password was wrong.
// -------------------------------------------------------------------------------

function one_default_error_message() {
    $forgot_link = wp_lostpassword_url( get_permalink() );
	return "Something is wrong! <a href='$forgot_link' title='Lost Password'>Forgot your password?</a>";
}
add_filter( 'login_errors', 'one_default_error_message' );


// Remove version from parameter tags.
// -----------------------------------

function remove_wp_version_from_scripts( $src ) {
    if ( strpos( $src, 'ver=' ) ){
        $src = remove_query_arg( 'ver', $src );
        return $src;
    }
}
add_filter( 'style_loader_src', 'remove_wp_version_from_scripts', 9999 );
add_filter( 'script_loader_src', 'remove_wp_version_from_scripts', 9999 );

Regards!

1 Like

Oh my, wow, thank you so much! I guess that’s what I get for snagging code snippets from the web without carefully checking them out. This is the first time they’ve caused any issues. I guess I got really lucky! Thanks again.

You’re most welcome.

1 Like

Hello there,

I’ve seen that you’ve withdrawn your post. Is there anything else we can help you with?

Just a side note, if your post is a new topic not related to the one we have on this thread, kindly create a new thread so we can focus on catering your issues there.

Thank you.

I totally agree with your suggestion about the secure note. Thanks.

1 Like

Sorry, I withdrew it as I had responded too hastily. :slight_smile:

When I transferred the code to the production site, it broke it so I thought maybe there was still some problem with the corrected code. But, I found the production site was running a plugin that was conflicting with “Remove version from parameter tags” code. All fixed.

Glad it’s fixed now and thanks for sharing!

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.