Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yoast SEO | How to create custom variables

Tags:

php

wordpress

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!

like image 414
Joystan Fernandes Avatar asked Mar 29 '16 10:03

Joystan Fernandes


People also ask

What is insert variable in Yoast SEO?

Variables in the Yoast SEO plugin give you the power to autogenerate page titles and meta descriptions for your entire website based on a set of rules defined by content type. This is one of the many powerful tools in WordPress that make it a search engine friendly content management system.

Is it worth paying for Yoast premium?

Is Yoast SEO Premium worth it? No, it is not. The multiple focus keywords feature is pretty much useless, redirects can be setup using free redirect plugins, and nothing in Yoast Premium directly improves your SEO. It's not worth $99/year.

What is a snippet variable?

Snippet variables have become a staple of Yoast SEO. These variables make it possible to automate certain processes on your site. They also make it easy to change large batches of meta descriptions for instance, since you only have to change the structure of the variable – the site fills in the data automatically.

How do I add meta tags to WordPress Yoast?

Click on Edit, and then scroll to the bottom of the screen where the Yoast SEO section is located: Enter your primary keyword into the Focus Keyphrase section. Then, click on the Edit Snippet button to enter your meta description.


1 Answers

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.

like image 75
Moshe Harush Avatar answered Sep 22 '22 23:09

Moshe Harush