Yoast SEO | How to create custom variables - php

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!

You have two options for this.
Add filter for change exist variable.
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.

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.
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'.
The 'SEO' settings will expand providing you additional options. Click on 'Titles & Metas'.
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

Related

Post name and custom fields in Wordpress Shortcode

I'm using shortcodes in my Wordpress page to display a responsive Google Map centered at specific location. To do it I'm using shortcode supported by external Wordpress plugin. Such shortcode is let's say [map address="New York"].
As all my Wordpress posts titles are names of the cities, I would like to automatize the process and be able to display the titles of the post in place of the address parameter in [map] shortcode.
Is it possible? Is there any way to do it? As far as I know nesting code or variables in shortcode isn't supported by Wordpress but maybe there is some kind of a workaround.
The same behavior I'd like to accomplish for 'custom field' - really similar situation.
I'd appreciate any help and any advice
Try to put this code to functions.php file in your theme:
function mymap_shortcode() {
echo do_shortcode('[map address="'.esc_html( get_the_title() ).'"]');
}
function mymap_shortcodes_init() {
add_shortcode('mymap', 'mymap_shortcode');
}
add_action('init', 'mymap_shortcodes_init');
And then in posts/pages (or even put it directly to template, or create another filter to add that) use shortcode [mymap] that will run [map] shortcode with current page title as parameter.

Archive.php Category.php or Taxonomy.php - Which is correct for my situation?

I'm using Genesis framework and I have this page (http://staging.seedcreativeacademy.co.uk/short-courses/) showing the categories of my custom post type short_courses. I have changed the category name to course_type, by creating a new custom taxonomy.
This is how I want it to work so far (styling needs sorting out admittedly!) Im also using CPT UI plugin.
Now, when I click through to a category, is displays each 'Course in a nice masonry block as you will see here: http://staging.seedcreativeacademy.co.uk/course_type/digital-marketing/
However, I dont want this pages to look like this and I've tried adding custom template for the following:
Archive-short_courses.php & taxonomy-short_courses.php
Archive-course_type.php & taxonomy-course_type.php
But it doesnt seem to alter the layout at all...
Once I pass this hurdle I will want to alter the single.php page for these short courses, but I thought I would start with this first.
Im not sure if genesis blocks this and sets a site wide default? I know if sets a site wide default for the archive settings but I cant find anything about a template, plus i dont know if I shoujld be searching for tutorials on archive.php pages, category.php pager or taxonomy.php pages...
Can someone help me clarify things please?
course_type is a term name, not taxonomy name.
So, these are correct for your case:
category-course_type.php (category-{slug}.php is correct format. So check if course_type is correct slug of that category)
single-short_courses.php
Just in case, try reload permalinks via Settings->permalinks->save after making these changes.
Looks like your theme or some plugin is adding masnory class to body tag, which then is styled by your child theme. You need to filter that class out of your body tag and then might styling goes to non-masonary styling.
Add following code to your taxonomy-course_type.php file, and also make sure you have genesis(); call as the last thing in the template.
add_filter('body_class', 'remove_body_class', 20, 2);
function remove_body_class($wp_classes)
{
foreach($wp_classes as $key => $value)
{
if ($value == 'masonry') unset($wp_classes[$key]);
}
return $wp_classes;
}
Above could should be in custom taxonomy template, which also have genesis(); as last line.

WooCommerce: Single-product-postslug template not working?

First,I know wordpress won't allow you to use template for posts (WooCommerce's product page is built via post). So I look for the Template Hierarchy.
It says there:
single-{post-type}-{slug}.php (Since 4.4). First, WordPress looks for a template for the specific post.
For example, if post type is product and the post slug is dmc-12, WordPress would look for single-product-dmc-12.php.
single-{post-type}.php – If the post type is product, WordPress would look for single-product.php.
single.php – WordPress then falls back to single.php.
singular.php – Then it falls back to singular.php.
index.php – Finally, as mentioned above, WordPress ultimately falls back to index.php.
So I created a template and name it single-product-ccc (ccc is one of my product's slug name), but nothing happened, nothing was affected.
But by creating a template named single-product will affect all of the product pages.
Why is that happening?
I don't get it. Even a single-2313.php (2313 is one post's id) will overwrite the default single.php for that 2313 post.
Why single-product-slug is not working in the same way?
Thanks.
Let me first correct you at one point. You say: "WooCommerce's product page is built via post".
This is not correct. WooCommerce creates a new post type 'product'. You can see all custom posts types that WooCommerce creates here: https://docs.woocommerce.com/document/installed-taxonomies-post-types/
The 'product' post type uses the template single-product.php.
As you say, WordPress Template Hierarchy Docs say that you can create a template for a particular product:
single-{post-type}-{slug}.php (Since 4.4). First, WordPress looks for a template for the specific post. For example, if post type is product and the post slug is dmc-12, WordPress would look for single-product-dmc-12.php.
WooCommerce is a WordPress plugin and as so, it doesn't always follow WordPress rules strictly, and this is one of those cases. But you can use a filter to correct this very easily. In your theme's functions.php simply add:
add_filter( 'template_include', 'custom_single_product_template_include', 50, 1 );
function custom_single_product_template_include( $template ) {
if ( is_singular('product') && (get_the_ID()==30)) {
$template = get_stylesheet_directory() . '/woocommerce/single-product-30.php';
}
return $template;
}
where get_the_ID() is the id of your Product. Now, you can create a new template as for example single-product-30.php
Try adding this in functions.php
function your_theme_add_woocommerce_support() {
add_theme_support( 'woocommerce' );
}
add_action( 'after_setup_theme', 'your_theme_add_woocommerce_support' );
and now onwards, you can use woocommerce/single-product.php for customisation

Using variables in the URL on wordpress menu link

I am using wordpress wp-admin to create the menu. It is under Appearance/menu.
I have a link that points to /members/ but what I really need is a link to /members/$logged_user...
For example /members/user_1 or /members/user_2.
How can I do that?
I dont know if it is important, but I am using buddypress plugin.
I have written a short script for adding dynamic buddypress links in wordpress menu. Hope it helps
I have added custom links in wordpress menu replacing username in link with --username--
example
http://website_name.com/members/--username--/messages/
then add this code in function.php
add_filter( 'nav_menu_link_attributes', 'menu_override', 10, 3 );
function menu_override( $atts, $item, $args ) {
$user = wp_get_current_user();
$newlink = str_replace("--username--", $user->user_login, $atts[href]);
$atts[href] = $newlink;
return $atts;
}
The default menu doesn't really have that option. But you can make your own walker function, searching for the keyword and replacing it with the current user.
See http://codex.wordpress.org/Function_Reference/wp_nav_menu#Using_a_Custom_Walker_Function
But it would probably be faster and more manageable to just place the link outside the menu call with some static html and the needed php.
You can use the BuddyPress Custom Profile Menu plugin to get close to this functionality, otherwise you will have to write custom code that uses actions/filters of wp_nav_menu().

how to filter "link to existing content" suggestion in wordpress?

How can i filter the link given in "link to existing content".
As in the above image. I just want WSP BANNER TO BE SHOWN.
where WSP BANNER & CALENDAR are custom post_type
Any help will be appreciable.
Currently there are no ready filters available for this purpose. A ticket has been posted for the request.Lets hope we get one soon.
Till then you can create your own filter.
Open includes/class-wp-editor.php and make folowing changes at line no 712
$pt_names = apply_filters('custom_insert_link_suggestion_filter',array_keys( $pts ));
we just added a new filter instead of getting all the public post types
Then in your theme add following code to filter the internal link custom post type
function my_filter_function($allowed_post_types)
{
if( condition chek)
{
return array('page','your custom post types');
}
}
add_filter('custom_insert_link_suggestion_filter','my_filter_function',10,1);
There is a plugin that could be helpful for you: B09 Link to Existing Content
It has a filter called "link_to_existing_content_post_types", that enables you to control which post types should be searched.
You can also use it together with this plugin, if you also want to have complete control over the search results: Search Everything.
Consider an example where my site is configured on wp.abc.com and I point my root domain www.abc.com to the new site. I need to update the URL's in General Setting. So when I update the URL, it is reflected in menu items etc. Will it be reflected in the content area links aslo?

Categories