skip to Main Content

Was just wondering if there is a way to create a custom variable so that I can add my custom variable created in the Meta title of the page.

Yoast SEO has a list of variables predefined here.

It would be great if I could create a variable of my own. Is there any way to get this?

Thanks in advance!

2

Answers


  1. There is a way, but as far as I know, you must get a Premium account in Yoast Seo. One of the most important functions of both Yoast SEO and Yoast SEO Premium is the possibility to add title templates and meta description templates to the homepage, all (custom) post types, all (custom) taxonomies and other pages.

    Note: Template variables can also be used on individual posts, pages and taxonomies.

    Setting up / changing your templates

    You can change your title & meta templates by going to the admin of your WordPress installation and clicking SEO → Titles & Metas.

    1. Log in to your WordPress website. When you’re logged in, you will be in your ‘Dashboard’. On the left-hand side, you will see a menu. In that menu, click on ‘SEO’.
    2. The ‘SEO’ settings will expand providing you additional options. Click on ‘Titles & Metas’.
    3. Under each tab, you can use these variables to create templates for various pages within your site.
      You can use the variables from the partial list below to create your own templates for the titles and meta-descriptions. The full list of variables is listed on the HELP tab of the plugin. Just go to SEO → Titles & Metas and click the help tab in the top right.

    For more about that and see many images of the process, please visit this page: http://kb.yoast.com/article/146-yoast-wordpress-seo-titles-metas-template-variables

    Login or Signup to reply.
  2. You have two options for this.

    1. Add filter for change exist variable.
    2. Add your new custom variable.

    If you want to change exist variable, you can do it like this:

    // define the wpseo_replacements callback
    function filter_wpseo_replacements( $replacements ) {
        if( isset( $replacements['%%page%%'] ) ){
            $replacements['%%page%%'] = 'Page x of y';
        }
        return $replacements;
    };
    // Add filter
    add_filter( 'wpseo_replacements', 'filter_wpseo_replacements', 10, 1 );
    

    And if do you want to add custom variable you can do it like this:

    // define the custom replacement callback
    function get_myname() {
        return 'My name is Moses';
    }
    
    // define the action for register yoast_variable replacments
    function register_custom_yoast_variables() {
        wpseo_register_var_replacement( '%%myname%%', 'get_myname', 'advanced', 'some help text' );
    }
    
    // Add action
    add_action('wpseo_register_extra_replacements', 'register_custom_yoast_variables');
    

    I hope I was helpful to you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search