Yoast WPSEO Plugin: How to add change frequency selector to tab „advanced“ for sitemap generation

Description

Modification for the YOAST WordPress-SEO Plugin. This adds a new drop down select box for „Change frequency“ to change the Yoast default sitemap value from weekly to a different of the possible values: always, hourly, daily, weekly, monthly, yearly, never.

The new drop box selector for the change frequency of a page or post is placed in tab „advanced“ in the Yoast Meta Box.

 

Test Environment

also tested with Yoast WPSEO v1.5.2.2 (needs an additional line).

Please evaluate other versions. This HowTo is for developers and comes with no warranty.

Changes to class-wpseo-meta.php

Open the file \wp-content\plugins\wordpress-seo\inc\class-wpseo-meta.php

and in function

public static $meta_fields = array()

at around Line 222 (after ’sitemap-prio‘) add the following code

 'sitemap-change-frequency' => array(
   'type' => 'select',
   'title' => 'Change frequency', 
   'default_value' => 'weekly',
   'description' => '', // translation added later
   'options' => array(
     'always' => '',
     'hourly' => '',
     'daily' => '',
     'weekly' => '',
     'monthly' => '',
     'yearly' => '',
     'never' => '',
   ),
 ),

In the same file search for the function get_meta_field_defs() and inside the block

case 'advanced':

search for

/* Don't show the xml sitemap fields, if xml sitemaps aren't enabled */
 if ( $options['enablexmlsitemap'] !== true ) {
   unset(
     $field_defs['sitemap-include'],
     $field_defs['sitemap-prio']
   );
 }

and change to

/* Don't show the xml sitemap fields, if xml sitemaps aren't enabled */
 if ( $options['enablexmlsitemap'] !== true ) {
   unset(
     $field_defs['sitemap-include'],
     $field_defs['sitemap-prio'],
     $field_defs['sitemap-change-frequency']
   );
 }

class-metabox.php

Open the file wp-content\plugins\wordpress-seo\admin\class-metabox.php and look for the function

translate_meta_boxes()

At around line 105 insert the following

self::$meta_fields['advanced']['sitemap-change-frequency']['title'] = __( 'Sitemap Change frequency', 'wordpress-seo' );
 self::$meta_fields['advanced']['sitemap-change-frequency']['description'] = __( 'The Change frequency given to this page in the XML sitemap.', 'wordpress-seo' );
 self::$meta_fields['advanced']['sitemap-change-frequency']['options']['always'] = __( 'Always', 'wordpress-seo' );
 self::$meta_fields['advanced']['sitemap-change-frequency']['options']['hourly'] = __( 'Hourly', 'wordpress-seo' );
 self::$meta_fields['advanced']['sitemap-change-frequency']['options']['daily'] .= __( 'Daily', 'wordpress-seo' );
 self::$meta_fields['advanced']['sitemap-change-frequency']['options']['weekly'] .= __( 'Weekly', 'wordpress-seo' );
 self::$meta_fields['advanced']['sitemap-change-frequency']['options']['monthly'] .= __( 'Monthly', 'wordpress-seo' );
 self::$meta_fields['advanced']['sitemap-change-frequency']['options']['yearly'] .= __( 'Yearly', 'wordpress-seo' ); 
 self::$meta_fields['advanced']['sitemap-change-frequency']['options']['never'] .= __( 'Never', 'wordpress-seo' );

This adds an extra Meta tag to the Yoast WPSEO Database entries

To extract those entries and write them to the sitemap.xml you have to change

class-sitemaps.xml

Open \wp-content\plugins\wordpress-seo\inc\class-sitemaps.php and at around line 784 search for

$url['chf'] = $this->filter_frequency( $post_type . '_single', 'weekly', $url['loc'] );

and replace with

$url['chf'] =WPSEO_Meta::get_value( 'sitemap-change-frequency', $p->ID );

Other versions of Yoast WPSEO

For other versions of Yoast WPSEO Plugin you might have to replace more lines, where the array variable $url[‚chf‘] gets assigned.

If using Version 1.5.2.2 please also replace at around line 550:

$url['chf'] = 'weekly';

with the substitution

$url['chf'] = $freq = WPSEO_Meta::get_value( 'sitemap-change-frequency', $p->ID );