Setting a Custom SEO Title Using Yoast Wordpress SEO API - php

I've just installed this plugin on my site and it means that my custom titles that were previously set no longer work. Here's the link to the brief Wordpress SEO API doc: [http://yoast.com/wordpress/seo/api-docs/]
Previously, I did this:
$pagetitle = '' . $design['name'] . ' | Free Design from My Site';
I have a custom URL rewrite that takes a WP page /design/ and appends the design name to /design/design-name so now I've installed WP SEO, the page title is whatever the title of /design/ is and isn't specific.
According to the docs in the link above, I tried this:
function wpseo_design_detail_title($pagetitle) {
$pagetitle = $design['name'] . ' | Free Web Template from My Site';
}
add_filter( 'wpseo_title', 'wpseo_design_detail_title' );
That does remove the previous /design/ title but instead it just has the sites URL (i.e. the page title is blank).
I'm probably missing something really simple, right?

This is how it's done:
function YourFunctionName( $title ) {
 
$title = 'Your Webpage Title';
return $title;
}
add_filter( 'wpseo_title', 'YourFunctionName' );
But #user1502679 suggested, you can use the WordPress' wp_title filter (but it only works if WordPress SEO by Yoast is disabled):
function YourFunctionName( $title, $sep ) {
$title = 'Your Webpage Title';
return $title;
}
add_filter( 'wp_title', 'YourFunctionName' );

why you use Yoast? just add a filter to wp_title

Related

WordPress Pages - Adding subdirectory to permalink for specific pages

Currently, I have my permalink structure set up such as https://example.com/blog/%postname%/ which makes it so that whenever a blog post is loaded, the URL shows /blog/. I would also like to add the subdirectory /try/ for specific pages. I have tried to use a plugin to add categories to pages so I can create a category and add it that way, but it seems that this is not possible because the "Custom Structure" is already set as noted above.
Does anyone know of a way to add a subdirectory /try/ to specific pages for a WordPress website so the URL for certain pages would be such as https://example.com/try/page-title
Thanks.
First we have to add rewrite_rule
add_rewrite_rule(
'^try/([^/]+)/?',
'index.php?name=$matches[1]',
'top'
);
Then we need to use the post_link filter to change the link format for certain posts
add_filter( 'post_link', 'custom_post_permalink', 10, 4 );
function custom_post_permalink( $link, $post = 0 ) {
$post_cur_id = $post->ID;
$post_cur_slug = $post->post_name;
//array of post_ids with try in permalink
$try_array = array(1,7,22,78);
if(in_array($post_cur_id, $try_array)) {
$slug = '/try/' . $post_cur_slug . '/';
$link = home_url($slug, 'https');
}
return $link;
}
The array $try_array should contain post_id's in which we change the link format

In WordPress, how can I replace the_title() with get_the_category_by_ID()?

I am a new developer and I just read in the WordPress Code Reference site that the_title() has been deprecated and get_the_category_by_ID should be used instead.
The thing is that I just can't find a way to replace it so that it successfully displays the title on each blog post. In the examples it seems to be used only for the categories.
Can anybody give me an example of how to do this properly, please?
You can replace the title using a wp hook. You can put this code, or inside the function.php on your theme; or in your plugin example:
function my_custom_replace_title( $title, $id = null ) {
/* Put here your condition, you get the post ID on "$id", and the current title on "$title"*/
// by category
//if ( in_category('XXX', $id ) ) {
// return '';
//}
//if ($title === 'custom text' ){
// return 'this is my new title';
// }
$title = 'My Custom Title - Forced'; // This is to test it.
return $title;
}
add_filter( 'the_title', 'my_custom_replace_title', 10, 2 );
I recommended to read https://codex.wordpress.org/Plugin_API/Filter_Reference/the_title
I hope help you.

Custom Shortcodes in Yoast SEO

I would like to use custom shortcodes in Yoast SEO Plugin, but I can't bring it to work. I want to put custom time specifications in the meta title.
This is my shortcode:
function time_yoast_shortcode() {
$year = date('Y');
return $year;
}
add_shortcode('yyyy', 'time_yoast_shortcode')
This is how I want the title in the Yoast to look like: THIS IS MY EXAMPLE POST in the year [yyyy]
Any ideas how I can make my shortcode work in Yoast?
For anyone looking to add shortcode support for Yoast SEO's Meta Title, here's how I handled it. This code adds full shortcode support for Meta Title and adds a custom shortcode [year] to output the current year. Add this code to functions.php:
// Add shortcode support to Yoast Meta Title using 'wpseo_title' filter
add_filter('wpseo_title', 'filter_wpseo_title');
function filter_wpseo_title($title) {
$title = do_shortcode($title);
return $title;
}
// Add [year] shortcode (outputs current date in YYYY format)
add_shortcode('year', 'year_shortcode');
function year_shortcode() {
$year = date('Y');
return $year;
}
If you'd like your post to show the date at the end of your SEO Title of Yoast try: %%date%% within the SEO Title field of the yoast plugin on that particular post. For just the year use %%currentyear%%.
In the event you'd like to do this globally you can do this within the post titles & metas section of the plugin settings.
Here is a full list of their varables
We've created a WordPress plugin to do/run/show shortcodes for Yoast SEO. It is currently in version 2.1.1. Unfortunately we cannot upload it to the WordPress repository due to possible copyright infringement (which is not true). So you may download, use parts of our code and use install the whole plugin directly from here:
https://denra.com/wordpress/plugins/do-shortcodes-for-yoast-seo/
We will continue updating the plugin with new features when needed but you may need to keep the URL to the plugin page on our website because we cannot upload to WordPress.org
Add this code in functions.php file and it will work:
// For adding [year] shortcode in WordPress Posts
add_shortcode( 'year', 'sc_year' );
function sc_year(){
return date( 'Y' );
}
add_filter( 'single_post_title', 'my_shortcode_title' );
add_filter( 'the_title', 'my_shortcode_title' );
function my_shortcode_title( $title ){
return do_shortcode( $title );
}
add_filter( 'wpseo_title', 'do_shortcode' );
add_filter( 'wpseo_metadesc', 'do_shortcode' );
add_filter( 'the_title', 'do_shortcode' );
add_shortcode( 'year' , 'current_year' );
function current_year() {
$year = date("Y");
return "$year";
}
Now you can use [year] in a title to post current year.
i am writing this code by considering you have yoast seo plugin

Setting a custom canonical URL in Yoast Wordpress SEO Plugin

Reposted due to no replies.
I'm having some trouble setting a custom canonical title using the Wordpress SEO API: http://yoast.com/wordpress-seo-api-docs/
I have a custom post type called designs which uses a custom URL rewrite. It takes the base page /design/ and adds the design name to it like /design/a-design/. The canonical in Wordpress SEO by default is the /design/ page.
What I want to do is write a function which determines if it is a design page and return a different canonical. I can test whether it's a design page by doing if ($design == ""){ and I tried to use the custom permalink URL, but the function just removes the canonical completely.
Here's my basic function:
function design_canonical(){
if ($design == "") {
// Leave blank and Yoast SEO will use default canonical for posts/pages
}
else {
return $design['detailslink'];
}
}
add_filter( 'wpseo_canonical', 'design_canonical' )
Quite clearly doing something wrong, but I'm not entirely sure what.
Thoughts?
You could try something like:
function design_canonical($url) {
global $post;
if ( get_post_type( $post->ID ) == 'design' ) {
return site_url( '/design/' . $post->post_name );
} else {
// Do nothing and Yoast SEO will use default canonical for posts/pages
return $url;
}
}
add_filter( 'wpseo_canonical', 'design_canonical' );
Hi i couldn't answer to the above post so I just make another one.
I tried to use the answer from stealthyninja for a similar problem and it almost worked. Except the last part: the empty else statement. It renders no output if the rule doesn't match.
Maybe Yoast updated his plugin on this within the last 2 years so I thought I should mention it here.
The following Code-Snippet solved my problem:
function design_canonical($url) {
global $post;
if ( get_post_type( $post->ID ) == 'design' ) {
return site_url( '/design/' . $post->post_name );
} else {
return $url;
}
}
add_filter( 'wpseo_canonical', 'design_canonical' );

Changing wp_title from inside my Wordpress Plugin

nearly finished my wp plugin for an estate agent,
I spidered the site for 404's etc, i noticed that my property details pages were spider'd in which all 45 page titles were : ( details | sitename ) (page titles are shown dynamicly from an ID being passed via querystring)
now I've got my nice urls fixed, urls look like this...
wpsite.com/details/20043/property+for+sale+in+this+area
In Which...
propid = 20043
propname = property+for+sale+in+this+area
both of these are querystring values which are used to re-write the urls.
'query_vars' => array('propid', 'propname'),
'rules' =>
array( '(.+?)/([^/]+)/([^/]+)/?$' => 'index.php?pagename=$matches[1]&propid=$matches[2]&propname=$matches[3]' )
);
Now when the property details page is loaded im trying to hook into the wordpress filter
wp_title but this isnt working the way i expected..
this is the code im using to generate the titles
function wp_myplugin_property_title()
{
$wp_acquaint_id = get_option("wp_system_id");
$propid = get_query_var('propid');
if(isset($propid)){
$seotitle = wp_myplugin_seo_title($propid);
}else{
$seotitle = "TEST Title";
}
return $seotitle;
}
if( is_page('details') ){
add_filter('wp_title', wp_myplugin_property_title, 100);
}
the function used within that function: wp_myplugin_seo_title($propid) generates the actual title i want to use...
function wp_myplugin_seo_title($propid)
{
$wp_acquaint_id = get_option("wp_acquaint_id");
$xml = wp_myplugin_get_property($propid);
foreach($xml->PropertiesDataSet->Properties as $node) {
include('xml_loop.php');
if($bedrooms==0){ }else{ $seo_title.= $bedrooms." bedroom "; }
$seo_title.= wp_myplugin_get_property_type($type_id)." "; //ie:flat
$seo_title.= str_replace("(","",$street);
$seo_title.= " ".$town." | ".get_bloginfo('name');
}
return $seo_title;
}
Im finding that with the if(is_page()) in place around the filter the page title dosnt change and if i remove the is_page, the prop details page title works fine but!!!
while on the property listing page the page title is looping through all the properties on that page and producing a page title around 1000 char long..!
I have been hunting around for a better way to deal with this but any help would be great..
Cheers
Marty
ps: currently running wordpress seo by Yoast!
thats why ive set the priority as 100 in the add_filter just to see if it would overwrite the title..
Using is_page in functions.php doesn't work, as it runs before wp knows what page it's going to render, or even if it's a page to begin with. Stick the is_page() inside the function, and it should work. Like:
function wp_myplugin_property_title()
{
if( is_page('details') ){
$wp_acquaint_id = get_option("wp_system_id");
$propid = get_query_var('propid');
if(isset($propid)){
$seotitle = wp_myplugin_seo_title($propid);
}else{
$seotitle = "TEST Title";
}
return $seotitle;
}
}
add_filter('wp_title', wp_myplugin_property_title, 100);

Categories