0

I have created a simple shortcode from looking at tutorials:

<?php
/*
Plugin Name: GT NoTranslate Shortcode
Description: Shortcode to wrap a span with the "notranslate" span around text.
Version: 0.1 BETA
Author: Andrew Truckle
Author URI: http://www.trucklesoft.co.uk
*/

function trucklesoft_gtspan_shortcode()
{
    return '<span class="notranslate">' . $content . '</span>';
}

add_shortcode( 'ts', 'trucklesoft_gtspan_shortcode' );
?>

I have placed the code in trucklesoft.php and put it in a folder called trucklesoft in the plugins folder.

I activated the plugin. Yet, when I edit my page and I change the title to:

[ts]This is the heading[/ts]

And preview it it shows as:

[ts]This is the heading[/ts]

Instead of <span class="notranslate">This is the heading</span>.

What am I doing wrong?


I had to make changes to the code in the first instance anyway for it to work in the content of the page:

<?php
/*
Plugin Name: GT NoTranslate Shortcode
Description: Shortcode to wrap a span with the "notranslate" span around text.
Version: 0.1 BETA
Author: Andrew Truckle
Author URI: http://www.trucklesoft.co.uk
*/

function trucklesoft_gtspan_shortcode($atts, $content = null)
{
    return '<span class="notranslate">' . $content . '</span>';
}

add_shortcode( 'ts', 'trucklesoft_gtspan_shortcode' );
?>

The location where I was initially trying to use this plugin was the title. When I view the source it shows it in this location:

<header class="entry-header">
    <h1 class="entry-title">Test</h1>
</header><!-- .entry-header -->
  • Are you using it in the page title? I don’t believe shortcodes are processed in post or page titles. – Jacob Peattie May 13 '18 at 8:36
  • @JacobPeattie See updated question. – Andrew Truckle May 13 '18 at 13:09
2

It looks like you are trying to apply a shortcode to the title field. That is not supported by itself (only shortcodes in the post content are evaluated automatically), but easily remedied by adding it as a filter to get_the_tile in your plugin:

add_filter ('the_title', 'do_shortcode');

(I'm assuming that you ruled out other possible problems by running your shortcode in the content field and establishing that it works properly there)

EDITED The filter is in the function get_the_title, but its name is the_title.

  • Thanks. Please see my updated question. I am not sure how to call my shortcode with this filter. Sorry. – Andrew Truckle May 13 '18 at 12:26
  • I added add_filter ('get_the_title', 'do_shortcode'); to my source file and it seemed to make no difference. – Andrew Truckle May 13 '18 at 12:48
  • 1
    The correct filter is the_title – Jacob Peattie May 13 '18 at 13:39
  • @JacobPeattie You are right. With that change it does the header. My intention is to use this shortcode wherever I need text not translated. So it will be several places in posts/pages and in some headers and menu item text. – Andrew Truckle May 13 '18 at 13:46
  • By default shortcodes are only processed in post/page content (and maybe the text widget?). If you want to use shortcodes in other places you might need other filters or template changes. There's no single thing you can do. Shortcodes aren't really intended for this sort of use. Using a span with a class implies translation is done with JavaScript. Why not just handle selecting the correct elements in JS? – Jacob Peattie May 13 '18 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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