Friday, June 18, 2021

How to create YOAST custom variable

 If you wonder how to create custom YOAST variable here is solution:

You have to add following code into functions.php.

First create empty function which will be used to register all YOAST custom variables:

function register_custom_yoast_variables() {}

Now register that function with YOAST so it knows how to load custom variable. Add following code:

add_action('wpseo_register_extra_replacements', 'register_custom_yoast_variables');

Now create function which will return value of your custom variable. For example:

function say_hello() {
    return "hello world";
}

And we are almost ready. Into register_custom_yoast_variables() add code which will actually register function say_hello as custom variable.

Basically add following code:

wpseo_register_var_replacement( '%%say_hello%%', 'say_hello', 'advanced', 'My custom say_hello yoast variable' );

So function register_custom_yoast_variables will now look like this:

function register_custom_yoast_variables() {
    
wpseo_register_var_replacement( '%%say_hello%%', 'say_hello', 'advanced', 'My custom say_hello yoast variable' );
}

Now if you need more custom variables just create php function which will return value and register it in the function register_custom_yoast_variables

Thursday, June 17, 2021

WordPress - How to find parent term of current term

 You have hierarchical categories or custom taxonomy and for SEO reasons or navigation reasons you want to display parent term or the current term.

To get name of the parent term if such is available you can use following code:

Note you have to be on the taxonomy page (archive) for this to work.

function parent_term() { 

$term = get_queried_object();

$parent = "";

if ($term->parent == 0) {

$parent = "";

} else {

$termParent = ($term->parent == 0) ? $term : get_term($term->parent, $term->taxonomy);

$parent = $termParent->name;

}

return $parent;

 

WP - Theme Editor not visible

 If you are asking: Why theme editor is not visible in my WP? or Why I cannot edit functions.php in WordPress? or Why theme editor is missing?

The probable reason is that you have disabled editing files. 

Here are few possibilities how to get your theme editor back:

1. In wp-config.php find following line:

define (‘DISALLOW_FILE_EDIT’, true)

and change it to

define (‘DISALLOW_FILE_EDIT’, false)

2. If you have no access to wp-config.php it means some security plugin blocked this.

In the case you are using iThemes just do following:

- In iThemes Settings go to WordPress Tweaks and enable/disable File Editor - Disable File Editor