How to create custom text variables

Guide on how to create custom text variables for sharing content.

In this article


Text variables are an advanced way to customize sharing content. With their help you can create unique sharing content for each image that includes unique data related to each shared image.

For example, when sharing to Twitter ( X ) we can display the value of image alt attribute together with category name of the post where this image is located.

Default text variables in use

Default text variables in use

But it may happen that the default text variables are not enough. For example, we want to share the post author name when sharing an image. By default there are no {{page_author}} text variables.

Good news is that we can create such one with some custom code snippets. As well as any other text variable of our choice. Examples of how to do that you will find below.

Custom text variables in use

Custom text variables in use

Creating custom text variables

Example - creating {{page_author}} text variable

In this example we will create {{page_author}} text variable. Also we will set this variable value to be visible when sharing any site image. We will do this via Content Sources options of the plugin settings page.

Here is a step by step guide of how to do that:

1. Creating custom text variables via code snippet. You need to place this code somewhere outside the plugins folder. For example, inside functions.php file of your theme or you can use any plugin for adding custom code snippets.

add_filter( 'sti_custom_text_vars', 'my_sti_custom_text_vars' );
function my_sti_custom_text_vars( $variables ) {
    $variables[] = array(
        'example' => 'page_author',
        'desc' => __( 'Page author name', 'share-this-image' ),
        'var' => 'page_author',
        'func' => 'sti_get_page_author_value'
    return $variables;

function sti_get_page_author_value() {
    global $wp_query;
    $value = '';
    $page_id = $wp_query->get_queried_object_id();
    $author_id = get_post_field ('post_author', $page_id);
    if ( $author_id ) {
        $author_name = get_the_author_meta( 'display_name', $author_id );
        if ( $author_name ) {
            $value = $author_name;
    return $value;

Let's look at this code a bit. It is using a built-in plugin filter sti_custom_text_vars to add a custom text variable. This filter accepts an array of custom variables. Example of such an array you see inside my_sti_custom_text_vars function.

The most important parts of $variables array is var value that specify text variable name and func that target to function that will render this text variable and includes all logic of what value for this variable to display.

In our example it is sti_get_page_author_value function that returns page/post author ( or empty string if no author is found ).

2. Open plugin settings page -> Content tab. Find Title Source option and add Default Title as a main source for sharing titles.

Setting up the source field for the title

Setting up the source field for the title

This will force the plugin to use the value from Default Title field as a default sharing content.

3. On the same page find Default Title option and set any custom text here. Don't forget to use the newly created {{page_author}} text variable.

{{page_author}} text variable as part of sharing content

{{page_author}} text variable as part of sharing content

4. Now just save all your changes and test your sharing buttons. Shared title field now must contain the page author name inside it.

Page author value as part of sharing content

Page author value as part of sharing content

Note: Don't forget that you also can customize sharing content per button basis.

Note: Another way to customize sharing content with custom values is to use custom content sources. Read more about this here: How to create custom content sources.