Created
September 15, 2023 01:38
-
-
Save crazyyy/896a22d9a226d70f3c5d0de38b0d556a to your computer and use it in GitHub Desktop.
WordPress SEO Yoast improvements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Set the custom Open Graph image URL for specific conditions. | |
* Add custom Twitter Social Share images for Yoast SEO for Archive pages | |
* | |
* @param string $image The default Open Graph image URL. | |
* @return string The custom Open Graph image URL, or the default URL if not found. | |
*/ | |
function artkai_set_custom_category_og_image($image) { | |
if (is_post_type_archive('product') || (is_category('categoryname') && is_post_type_archive('archivename'))) { | |
$term = get_queried_object(); | |
$custom_image = get_field('opengraph-image', $term); | |
if ($custom_image) { | |
return $custom_image['url']; | |
} | |
} | |
return $image; | |
} | |
add_filter('wpseo_opengraph_image', 'artkai_set_custom_category_og_image'); | |
add_filter('wpseo_twitter_image', 'artkai_set_custom_category_og_image'); | |
/** | |
* Override the Open Graph image URL if not set for single posts. | |
* | |
* @param string $img The original Open Graph image URL. | |
* @return string The modified Open Graph image URL. | |
*/ | |
// function artkai_seo_override_ogp_img($img) { | |
// if (is_single()) { | |
// $post_id = get_the_id(); | |
// $og_image = get_post_meta($post_id, '_yoast_wpseo_opengraph-image', true); | |
// | |
// if (empty($og_image) && !has_post_thumbnail($post_id)) { | |
// $img = "https://image.paypay.ne.jp/page/common/images/bnr_ogp_01.png"; | |
// } | |
// } | |
// | |
// return $img; | |
// } | |
// ToDo: add_filter('wpseo_opengraph_image', 'artkai_seo_override_ogp_img', 10, 1); | |
/** | |
* Modify the social share descriptions for Yoast SEO on specific archive pages. | |
* | |
* @param string $desc The original description. | |
* @return string The modified description. | |
*/ | |
// function artkai_yoast_seo_social_share_descriptions($desc) { | |
// if (is_post_type_archive('posttypename') || is_category('categoryname')) { | |
// // Retrieve the description from the ACF field 'social-descriptions' | |
// $desc = get_field('social-descriptions'); | |
// } | |
// return $desc; | |
// } | |
// Add custom Facebook Social Share descriptions for Yoast SEO for Archive pages | |
// ToDo: add_filter('wpseo_opengraph_desc', 'artkai_yoast_seo_social_share_descriptions'); | |
// Add custom Twitter Social Share descriptions for Yoast SEO for Archive pages | |
// ToDo: add_filter('wpseo_metadesc', 'artkai_yoast_seo_social_share_descriptions'); | |
/** | |
* OpenGraph add an og:image:width and og:image:height for FB async load of og:image issues | |
* WordPress plugin that outputs og:image:width and og:image:height tags because Facebook needs them when sharing URLs for the first time. | |
* https://github.com/Yoast/wordpress-seo/issues/2151 | |
* https://developers.facebook.com/docs/sharing/webmasters/optimizing#cachingimages | |
* https://gist.github.com/bdeleasa/0099bc17eb9f528d0f46cd54304654ae | |
*/ | |
// function artkai_seo_og_image_fix() { | |
// global $wpseo_og; | |
// | |
// // Get an array with images | |
// $opengraph_images = new WPSEO_OpenGraph_Image($wpseo_og->options); | |
// | |
// foreach ($opengraph_images->get_images() as $img) { | |
// // Convert URL of image to local path | |
// $upload_dir = wp_upload_dir(); | |
// $img_src = str_replace($upload_dir['url'], $upload_dir['path'], $img); | |
// $size = getimagesize($img_src); | |
// | |
// // Add og:image:width and og:image:height tags | |
// $wpseo_og->og_tag('og:image:width', $size[0]); | |
// $wpseo_og->og_tag('og:image:height', $size[1]); | |
// } | |
// } | |
// Hook the function to the wpseo_opengraph filter | |
// ToDo: add_filter('wpseo_opengraph', 'artkai_seo_og_image_fix'); | |
/** | |
* Replace domain for stylesheet to XML. | |
* @param string $stylesheet The original stylesheet URL. | |
* @return string The modified stylesheet URL. | |
*/ | |
function artkai_replace_wpseo_stylesheet_url($stylesheet) { | |
// Replace the domain in the stylesheet URL | |
$stylesheet = str_replace(ARTKAI_CUSTOM_FRONT_DOMAIN, ARTKAI_API_DOMAIN, $stylesheet); | |
return $stylesheet; | |
} | |
// Add filter for wpseo_stylesheet_url | |
add_filter('wpseo_stylesheet_url', 'artkai_replace_wpseo_stylesheet_url', 10, 1); | |
/** | |
* Replace domain for sitemap index links. | |
* @param array $links The original sitemap index links. | |
* @return array The modified sitemap index links. | |
*/ | |
function artkai_replace_wpseo_sitemap_index_links($links) { | |
// Define the search and replace strings | |
$search = ARTKAI_CUSTOM_FRONT_DOMAIN; | |
$replace = ARTKAI_API_DOMAIN; | |
// Modify each link in the array | |
$modified_links = array_map(function($link) use ($search, $replace) { | |
$link['loc'] = str_replace($search, $replace, $link['loc']); | |
return $link; | |
}, $links); | |
return $modified_links; | |
} | |
// Add filter for wpseo_sitemap_index_links | |
add_filter('wpseo_sitemap_index_links', 'artkai_replace_wpseo_sitemap_index_links', 10, 1); | |
/** | |
* Modify the canonical URL and other related tags generated by Yoast SEO. | |
* This function replaces the base domain of the canonical URL and other related tags from 'api.artkai.io' to 'artkai.io'. | |
* @param string $content The original content of the tag. | |
* @return string The modified content. | |
*/ | |
function artkai_custom_canonical($content) { | |
// Define the search and replace strings | |
$search = ARTKAI_API_DOMAIN; | |
$replace = ARTKAI_CUSTOM_FRONT_DOMAIN; | |
// Replace the domain in the content | |
$content = str_replace($search, $replace, $content); | |
return $content; | |
} | |
// Apply filters for canonical URL and other related tags | |
add_filter('wpseo_canonical', 'artkai_custom_canonical'); | |
add_filter('wpseo_opengraph_url', 'artkai_custom_canonical'); | |
/** | |
* Alter the OpenGraph and Twitter image for a single post. | |
* WordPress SEO uses the full-featured image for og:image and Twitter image output on singular pages. | |
* If the post doesn't have a featured image, it looks for the first image in the post content. | |
*/ | |
function artkai_alter_og_and_twitter_images($img) { | |
global $post; | |
global $wpseo_og; | |
// Check if opengraph-image is not set for the post | |
if (!WPSEO_Meta::get_value('opengraph-image', $post->ID)) { | |
// Get the featured image if it exists | |
if (has_post_thumbnail($post->ID)) { | |
$featured_image_url = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full'); | |
return $featured_image_url[0]; | |
} | |
// If no featured image, find the first image in the post content | |
$content = $post->post_content; | |
preg_match('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches); | |
if (!empty($matches) && isset($matches[1])) { | |
return $matches[1]; | |
} | |
// If no images are found, return a default value or handle the case accordingly | |
return $wpseo_og->options['og_default_image']; // Or any other default value | |
} | |
return $img; | |
} | |
add_filter('wpseo_opengraph_image', 'artkai_alter_og_and_twitter_images', 10, 1); | |
add_filter('wpseo_twitter_image', 'artkai_alter_og_and_twitter_images', 10, 1); | |
/** | |
* Modify the Open Graph and Twitter Card image URLs generated by Yoast SEO. | |
* This function replaces the base domain of the image URLs from 'artkai.io' to 'api.artkai.io'. | |
* @param string $image The original image URL. | |
* @return string The modified image URL. | |
*/ | |
function artkai_custom_og_image( $image ) { | |
// Check if the image URL contains the old domain | |
if ( strpos( $image, ARTKAI_CUSTOM_FRONT_DOMAIN ) !== false ) { | |
// Replace the old domain with the new domain | |
$image = str_replace( ARTKAI_CUSTOM_FRONT_DOMAIN, ARTKAI_API_DOMAIN, $image ); | |
} | |
return $image; | |
} | |
// Apply filters for Open Graph and Twitter Card image URLs | |
add_filter( 'wpseo_opengraph_image', 'artkai_custom_og_image' ); | |
add_filter( 'wpseo_twitter_image', 'artkai_custom_og_image' ); | |
/** | |
* Modify the content of the Open Graph and Twitter tags generated by Yoast SEO. | |
* @param string $content The content of the tag. | |
* @return string The modified content. | |
*/ | |
function artkai_modify_yoast_seo_tags_content( $content ) { | |
// Check if the content contains the old domain | |
if ( strpos( $content, ARTKAI_API_DOMAIN ) !== false ) { | |
// Replace the old domain with the new domain | |
$content = str_replace( ARTKAI_API_DOMAIN, ARTKAI_CUSTOM_FRONT_DOMAIN, $content ); | |
} | |
return $content; | |
} | |
add_filter( 'wpseo_opengraph_desc', 'artkai_modify_yoast_seo_tags_content' ); | |
add_filter( 'wpseo_twitter_description', 'artkai_modify_yoast_seo_tags_content' ); | |
/** | |
* Modify the Yoast SEO search URL by replacing the domain. | |
* @param string $search_url The original search URL. | |
* @return string The modified search URL. | |
*/ | |
function artkai_modify_yoast_search_url( $search_url ) { | |
// Check if the search URL contains the old domain | |
if ( strpos( $search_url, ARTKAI_API_DOMAIN ) !== false ) { | |
// Replace the old domain with the new domain | |
$search_url = str_replace( ARTKAI_API_DOMAIN, ARTKAI_CUSTOM_FRONT_DOMAIN, $search_url ); | |
} | |
return $search_url; | |
} | |
add_filter('wpseo_json_ld_search_url', 'artkai_modify_yoast_search_url'); | |
/** | |
* Modify the Rich Schema output generated by Yoast SEO. | |
* @param array $data The original JSON-LD data. | |
* @return array The modified JSON-LD data. | |
*/ | |
function artkai_modify_rich_schema( $data ) { | |
foreach ( $data as &$item ) { | |
if ( isset($item['@type']) ) { | |
if ( $item['@type'] !== 'ImageObject' ) { | |
// Replace in @id, id, url, contentUrl, urlTemplate | |
foreach ( ['@id', 'id', 'url', 'contentUrl', 'urlTemplate'] as $key ) { | |
if ( isset($item[$key]) ) { | |
$item[$key] = str_replace( ARTKAI_API_DOMAIN, ARTKAI_CUSTOM_FRONT_DOMAIN, $item[$key] ); | |
} | |
} | |
} else { | |
// Only replace in @id for ImageObject | |
if ( isset($item['@id']) ) { | |
$item['@id'] = str_replace( ARTKAI_API_DOMAIN, ARTKAI_CUSTOM_FRONT_DOMAIN, $item['@id'] ); | |
} | |
} | |
} | |
} | |
return $data; | |
} | |
add_filter( 'wpseo_json_ld_output', 'artkai_modify_rich_schema', 10, 1 ); | |
/** | |
* Replaces hostname in all images with the CDN one. | |
* @param array $data Schema.org graph. | |
* @param Meta_Tags_Context $context Context object. | |
* @return array The altered Schema.org graph. | |
*/ | |
function artkai_change_image_urls_to_cdn( $data, $context ) { | |
foreach ( $data as $key => $value ) { | |
if ( $value['@type'] === 'ImageObject' ) { | |
$data[$key]['contentUrl'] = str_replace( ARTKAI_API_DOMAIN, ARTKAI_CUSTOM_FRONT_DOMAIN, $value['contentUrl'] ); | |
} | |
} | |
return $data; | |
} | |
// Note: disabled. Alternative | |
//add_filter( 'wpseo_schema_graph', 'artkai_change_image_urls_to_cdn', 10, 2 ); | |
/** | |
* Modify canonical URL for 'post' post type using Yoast SEO filter. | |
* @param string $canonical The original canonical URL. | |
* @return string The modified canonical URL. | |
*/ | |
function artkai_modify_blog_post_canonical( $canonical ) { | |
global $post; | |
// Check if the current post type is 'post' and modify the canonical URL | |
if ( $post && 'post' === $post->post_type ) { | |
$canonical = home_url( '/blog/' . $post->post_name ); | |
} | |
return $canonical; | |
} | |
add_filter( 'wpseo_canonical', 'artkai_modify_blog_post_canonical', 5 ); | |
/** | |
* Modify Open Graph URL for 'post' post type using Yoast SEO filter. | |
* @param string $og_url The original Open Graph URL. | |
* @return string The modified Open Graph URL. | |
*/ | |
function artkai_modify_blog_post_og_url( $og_url ) { | |
global $post; | |
// Check if the current post type is 'post' and modify the Open Graph URL | |
if ( $post && 'post' === $post->post_type ) { | |
$og_url = home_url( '/blog/' . $post->post_name ); | |
} | |
return $og_url; | |
} | |
add_filter( 'wpseo_opengraph_url', 'artkai_modify_blog_post_og_url', 5 ); | |
/** | |
* Fix any broken replaced URLs in Yoast links. | |
* @param string $link The original link. | |
* @return string The fixed link. | |
*/ | |
function artkai_fix_yoast_urls_items( $link ) { | |
return str_replace( | |
array( | |
'api.api.artkai.io', | |
'2022-api.demo.artkai.dev' // add any other weird URLs that might show up | |
), | |
'api.artkai.io', | |
$link | |
); | |
} | |
add_filter( 'wpseo_canonical', 'artkai_fix_yoast_urls_items' ); | |
add_filter( 'wpseo_next_rel_link', 'artkai_fix_yoast_urls_items' ); | |
add_filter( 'wpseo_prev_rel_link', 'artkai_fix_yoast_urls_items' ); | |
add_filter( 'wpseo_opengraph_url', 'artkai_fix_yoast_urls_items' ); | |
add_filter( 'wpseo_opengraph_image', 'artkai_fix_yoast_urls_items' ); | |
} else { | |
/** | |
* Modify canonical URL for 'post' post type using WordPress built-in filter. | |
* @param string $link The original canonical URL. | |
* @param WP_Post $post The post object. | |
* @return string The modified canonical URL. | |
*/ | |
function artkai_modify_blog_post_rel_canonical( $link, $post ) { | |
// Check if the current post type is 'post' and modify the canonical URL | |
if ( 'post' === $post->post_type ) { | |
$link = home_url( '/blog/' . $post->post_name ); | |
} | |
return $link; | |
} | |
add_filter( 'get_canonical_url', 'artkai_modify_blog_post_rel_canonical', 5, 2 ); | |
} | |
//The given code is a WordPress function that modifies the breadcrumbs displayed on a website. Breadcrumbs are a navigation aid that helps users understand their current location within a website's hierarchy. | |
// | |
//The function "dgtlnk_breadcrumbs" takes an array of breadcrumb links as input and returns a modified version of the array. | |
// | |
//Here is a step-by-step explanation of the code: | |
// | |
//1. The function begins by accessing the global variable $post, which represents the current WordPress post or page being displayed. | |
// | |
//2. It then checks if the current page is a WooCommerce page by using the "is_woocommerce()" function. WooCommerce is a popular e-commerce plugin for WordPress. | |
// | |
//3. If the current page is a WooCommerce page, the function proceeds to add a new breadcrumb link to the array of links. | |
// | |
//4. The new breadcrumb link is an associative array with two elements: "url" and "text". The "url" element is set to the WooCommerce shop page URL, obtained using the "woocommerce_shop_page()" function. The "text" element is set to the string 'Shop'. | |
// | |
//5. The "array_splice()" function is used to modify the original array of breadcrumb links. It removes a portion of the array and replaces it with the newly added breadcrumb link. | |
// | |
//6. In this case, the "array_splice()" function is called with the following parameters: | |
// - The original array of breadcrumb links ($links). | |
// - The starting index for removal (1). | |
// - The number of elements to remove (-2, meaning all elements except the first and last). | |
// - The new breadcrumb link to insert ($breadcrumb). | |
// | |
//7. Finally, the modified array of breadcrumb links is returned by the function. | |
// | |
//8. The "add_filter()" function is used to register the "dgtlnk_breadcrumbs" function as a filter for the "wpseo_breadcrumb_links" hook. This means that whenever the breadcrumbs are generated, the "dgtlnk_breadcrumbs" function will be called to modify the breadcrumb links. | |
function dgtlnk_breadcrumbs( $links ) { | |
global $post; | |
if ( is_woocoomerce() ) { | |
$breadcrumb[] = array( | |
'url' => woocoommerce_shop_page(), | |
'text' => 'Shop', | |
); | |
array_splice( $links, 1, -2, $breadcrumb ); | |
} | |
return $links; | |
} | |
add_filter( 'wpseo_breadcrumb_links', 'dgtlnk_breadcrumbs' ); | |
//The code is a WordPress function that overrides the Yoast SEO plugin's breadcrumb trail for a specific condition. | |
// | |
//The function "wpse_100012_override_yoast_breadcrumb_trail" takes in an array of breadcrumb links as a parameter and modifies it based on the condition. | |
// | |
//Inside the function, the global variable "$post" is used to check if the current page is a product category. If it is, a new breadcrumb link is added to the array "$breadcrumb" with a custom URL and text. | |
// | |
//The "array_splice" function is then used to replace a portion of the original breadcrumb links array with the new "$breadcrumb" array. The parameters for "array_splice" specify the starting index (0), the number of elements to remove (-1 to remove all elements except the last one), and the replacement array. | |
// | |
//Finally, the modified breadcrumb links array is returned. | |
// | |
//The last line adds the "wpse_100012_override_yoast_breadcrumb_trail" function as a filter to the "wpseo_breadcrumb_links" hook. This ensures that the function is executed when the Yoast SEO plugin generates the breadcrumb trail. | |
function wpse_100012_override_yoast_breadcrumb_trail( $links ) { | |
global $post; | |
if( is_product_category() ) { | |
$breadcrumb[] = array( | |
'url' => get_permalink( 'my-category-custom-prefix' ), | |
'text' => 'MY_CATEGORY_CUSTOM_PREFIX' | |
); | |
array_splice( $links, 0, -1, $breadcrumb ); | |
} | |
return $links; | |
} | |
add_filter( 'wpseo_breadcrumb_links', 'wpse_100012_override_yoast_breadcrumb_trail' ); | |
//This code is a WordPress filter function that modifies the Yoast SEO plugin's breadcrumb links. | |
// | |
//The function `yoast_seo_breadcrumb_append_link` receives an array of breadcrumb links as a parameter and appends a new link to it. | |
// | |
//First, it checks if the current page is a single post with the IDs 11081, 11082, or 11096 using the `is_single()` function. If the condition is true, it proceeds to create a new breadcrumb link. | |
// | |
//The new breadcrumb link is an associative array with two elements: 'url' and 'text'. The 'url' element is set to the URL of the page '/ferry-to-norway-from-the-uk/' using the `site_url()` function. The 'text' element is set to 'Ferry to Norway'. | |
// | |
//Then, the `array_splice()` function is used to insert the new breadcrumb link into the `$links` array. It replaces the elements starting from index 1 and ending 2 positions before the end of the array with the `$breadcrumb` array. | |
// | |
//Finally, the modified `$links` array is returned from the function. | |
// | |
//The `add_filter()` function is used to add the `yoast_seo_breadcrumb_append_link` function as a filter to the 'wpseo_breadcrumb_links' hook. This ensures that the function is executed when the Yoast SEO plugin generates the breadcrumb links. | |
function yoast_seo_breadcrumb_append_link( $links ) { | |
global $post; | |
if ( is_single( 11081, 11082, 11096, ) ) { | |
$breadcrumb[] = array( | |
'url' => site_url( '/ferry-to-norway-from-the-uk/' ), | |
'text' => 'Ferry to Norway', | |
); | |
array_splice( $links, 1, -2, $breadcrumb ); | |
} | |
return $links; | |
} | |
add_filter( 'wpseo_breadcrumb_links', 'yoast_seo_breadcrumb_append_link' ); | |
/* | |
* Add shop link to the Yoast SEO breadcrumbs for a WooCommerce shop page. | |
* Credit: https://wordpress.stackexchange.com/users/8495/rjb | |
* Last Tested: Apr 20 2017 using Yoast SEO 4.6 on WordPress 4.7.3 | |
*/ | |
add_filter( 'wpseo_breadcrumb_links', 'wpseo_breadcrumb_add_woo_shop_link' ); | |
function wpseo_breadcrumb_add_woo_shop_link( $links ) { | |
global $post; | |
if ( is_woocommerce() ) { | |
$breadcrumb[] = array( | |
'url' => get_permalink( woocommerce_get_page_id( 'shop' ) ), | |
'text' => 'Shop', | |
); | |
array_splice( $links, 1, -2, $breadcrumb ); | |
} | |
return $links; | |
} | |
//The given code is a function that appends a breadcrumb link to the Yoast SEO breadcrumbs. It checks if the current page is a single post with ID 123456 and if so, it adds a "Blog" link to the breadcrumbs. | |
// | |
//Here is a step-by-step explanation of the code: | |
// | |
//1. The function `artk_yoast_seo_breadcrumb_append_link` is defined, which takes an array of breadcrumb links as an argument. | |
// | |
//2. The global variable `$post` is accessed to get information about the current post. | |
// | |
//3. The `is_single` function is used to check if the current page is a single post with the ID 123456. If it is, the following code block is executed. | |
// | |
//4. Inside the code block, a new array called `$breadcrumb` is created. | |
// | |
//5. The `$breadcrumb` array is populated with two key-value pairs. The 'url' key is assigned the value of the site URL with '/blog/' appended to it, and the 'text' key is assigned the value 'Blog'. | |
// | |
//6. The `array_splice` function is used to insert the `$breadcrumb` array into the `$links` array. It starts at index 1 (the second element) and removes all elements except the last two. Then it inserts the `$breadcrumb` array at that position. | |
// | |
//7. The modified `$links` array is returned. | |
// | |
//8. The `add_filter` function is used to add the `artk_yoast_seo_breadcrumb_append_link` function as a filter to the 'wpseo_breadcrumb_links' hook. This ensures that the function is called when the Yoast SEO breadcrumbs are generated. | |
function artk_yoast_seo_breadcrumb_append_link( $links ) { | |
// https://gist.github.com/amboutwe/ea0791e184668a5c7bd7bbe357c598e9 | |
global $post; | |
if ( is_single ( 123456 ) ) { | |
$breadcrumb[] = array( | |
'url' => site_url( '/blog/' ), | |
'text' => 'Blog', | |
); | |
array_splice( $links, 1, -2, $breadcrumb ); | |
} | |
return $links; | |
} | |
add_filter( 'wpseo_breadcrumb_links', 'artk_yoast_seo_breadcrumb_append_link' ); | |
//The code is a WordPress function that overrides the default Yoast breadcrumb trail for certain pages. It adds a "Blog" link to the breadcrumb trail for the home page, single blog post pages, and archive pages. | |
// | |
//Here is a step-by-step explanation of the code: | |
// | |
//1. The function `wpse_100012_override_yoast_breadcrumb_trail__second` is defined. | |
//2. The `$links` parameter is passed to the function, which represents the existing breadcrumb trail links. | |
//3. The global `$post` variable is accessed to check the current page. | |
//4. The `is_home()` function is used to check if the current page is the home page. | |
//5. The `is_singular( 'post' )` function is used to check if the current page is a single blog post page. | |
//6. The `is_archive()` function is used to check if the current page is an archive page. | |
//7. If any of the above conditions are true, the following code block is executed: | |
// - An empty array `$breadcrumb` is created. | |
//- An array element is added to `$breadcrumb` with the URL of the blog page and the text "Blog". | |
//- The `get_option( 'page_for_posts' )` function is used to get the URL of the blog page. | |
//- The `array_splice()` function is used to replace a portion of the `$links` array with the `$breadcrumb` array. The portion being replaced starts at index 1 and ends at the second-to-last index. | |
//8. The modified `$links` array is returned. | |
//9. The `wpse_100012_override_yoast_breadcrumb_trail__second` function is added as a filter to the `wpseo_breadcrumb_links` hook. This ensures that the function is called when the breadcrumb trail is generated by the Yoast SEO plugin. | |
function wpse_100012_override_yoast_breadcrumb_trail__second( $links ) { | |
global $post; | |
// https://wordpress.stackexchange.com/questions/100012/how-to-add-a-page-to-the-yoast-breadcrumbs | |
if ( is_home() || is_singular( 'post' ) || is_archive() ) { | |
$breadcrumb[] = array( | |
'url' => get_permalink( get_option( 'page_for_posts' ) ), | |
'text' => 'Blog', | |
); | |
array_splice( $links, 1, -2, $breadcrumb ); | |
} | |
return $links; | |
} | |
add_filter( 'wpseo_breadcrumb_links', 'wpse_100012_override_yoast_breadcrumb_trail__second' ); | |
//The following code is a function called "override_yoast_breadcrumb_links" that modifies the Yoast SEO plugin's breadcrumb links. It checks if the current page is a singular post, and if so, it adds a new breadcrumb link to the array of links. This new link is for the "Blog" page and its URL is obtained using the "get_permalink" function with the ID of the page set as the "page_for_posts" option. | |
// | |
//The "array_splice" function is then used to replace a portion of the original breadcrumb links array with the newly added link. The parameters for "array_splice" specify that the replacement should start at index 1 (excluding the first link) and remove all elements except the last two. The new breadcrumb link is inserted at the position where the original portion was removed. | |
// | |
//Finally, the modified breadcrumb links array is returned by the function. | |
// | |
//The last line of code adds the "override_yoast_breadcrumb_links" function as a filter to the "wpseo_breadcrumb_links" hook, so that it is executed when the Yoast SEO plugin generates the breadcrumb links. | |
function override_yoast_breadcrumb_links( $links ) { | |
global $post; | |
// https://gist.github.com/mtruitt/178296e2ff4176a1bdf0a78cbb55d9f4 | |
if ( is_singular( 'post' ) ) { | |
$breadcrumb[] = array( | |
// 'url' => get_permalink( get_option( 'page_for_posts' ) ), | |
'url' => site_url( '/blog/' ), | |
'text' => 'Blog', | |
); | |
array_splice( $links, 1, -2, $breadcrumb ); | |
} | |
return $links; | |
} | |
add_filter( 'wpseo_breadcrumb_links', 'override_yoast_breadcrumb_links' ); | |
function yoast_seo_breadcrumb_append_link_232( $links ) { | |
global $post; | |
if (get_post_type($post->ID) == 'custom-post-type') { | |
$links = array( | |
array('url' => site_url('/'), 'text' => 'Home'), | |
array('url' => site_url('/team-members'), 'text' => 'Team Members'), | |
array('url' => get_the_permalink($post->ID), 'text' => $post->post_title) | |
); | |
} | |
return $links; | |
} | |
add_filter( 'wpseo_breadcrumb_links', 'yoast_seo_breadcrumb_append_link_232' ); | |
function qr_add_breadcrumb( $links ) { | |
$breadcrumb[] = array( | |
'url' => 'URL', | |
'text' => 'Text', | |
); | |
array_splice( $links, 1, -2, $breadcrumb ); | |
return $links; | |
} | |
// Add an element after "Home" to Yoast SEO breadcrumbs. (WordPress) | |
add_filter( 'wpseo_breadcrumb_links', 'qr_add_breadcrumb' ); | |
// Remove "Home" link from Yoast SEO breadcrumbs. (WordPress) | |
add_filter('wpseo_breadcrumb_links', 'qr_remove_home_breadcrumb'); | |
function qr_remove_home_breadcrumb($links) { | |
if ($links[0]['url'] == get_home_url()) { array_shift($links); } | |
return $links; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment