abdulkrm

Improved

Jul 23rd, 2023
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. // Function to add new fields to WP tags
  2. function custom_tag_fields($term) {
  3. // Get term meta data
  4. $term_metas = [
  5. 'entity_name' => get_term_meta($term->term_id, 'entity_name', true),
  6. 'google_entity_url' => get_term_meta($term->term_id, 'google_entity_url', true),
  7. 'wikipedia_url' => get_term_meta($term->term_id, 'wikipedia_url', true),
  8. 'wikidata_url' => get_term_meta($term->term_id, 'wikidata_url', true)
  9. ];
  10.  
  11. // Fields to add
  12. $fields = [
  13. 'entity_name' => 'Entity Name',
  14. 'google_entity_url' => 'Google Entity URL',
  15. 'wikipedia_url' => 'Wikipedia URL',
  16. 'wikidata_url' => 'Wikidata URL'
  17. ];
  18.  
  19. foreach ($fields as $field_id => $label) {
  20. ?>
  21. <tr class="form-field">
  22. <th scope="row" valign="top"><label for="<?php echo $field_id; ?>"><?php _e($label); ?></label></th>
  23. <td>
  24. <input type="text" name="<?php echo $field_id; ?>" id="<?php echo $field_id; ?>" value="<?php echo esc_attr($term_metas[$field_id]); ?>">
  25. </td>
  26. </tr>
  27. <?php
  28. }
  29. }
  30. add_action('post_tag_edit_form_fields', 'custom_tag_fields');
  31.  
  32. // Function to save the new custom fields data
  33. function save_custom_tag_fields($term_id) {
  34. // Fields to save
  35. $fields = [
  36. 'entity_name' => 'sanitize_text_field',
  37. 'google_entity_url' => 'esc_url_raw',
  38. 'wikipedia_url' => 'esc_url_raw',
  39. 'wikidata_url' => 'esc_url_raw'
  40. ];
  41.  
  42. foreach ($fields as $field_id => $sanitize_func) {
  43. if (isset($_POST[$field_id])) {
  44. update_term_meta($term_id, $field_id, $sanitize_func($_POST[$field_id]));
  45. }
  46. }
  47. }
  48. add_action('edited_post_tag', 'save_custom_tag_fields');
  49.  
  50. // Function to add schema to the posts and tag pages
  51. function add_sameAs_schema_rank_math( $data, $jsonld ) {
  52. if(is_single() || is_tag()) {
  53. global $post;
  54. $post_tags = wp_get_post_tags($post->ID);
  55.  
  56. if (!empty($post_tags)) {
  57. foreach ($post_tags as $tag) {
  58. $entity_name = get_term_meta($tag->term_id, 'entity_name', true);
  59. $sameAs_array = array_filter([
  60. get_term_meta($tag->term_id, 'google_entity_url', true),
  61. get_term_meta($tag->term_id, 'wikipedia_url', true),
  62. get_term_meta($tag->term_id, 'wikidata_url', true)
  63. ]); // Remove empty elements
  64.  
  65. if (!empty($entity_name) && !empty($sameAs_array)) {
  66. // Create schema array
  67. $schema = [
  68. "@type" => "Thing",
  69. "name" => $entity_name,
  70. "sameAs" => $sameAs_array
  71. ];
  72. // Add to Article schema on single posts
  73. if(is_single() && isset($data['Article'])) {
  74. $data['Article']['about'][] = $schema;
  75. }
  76. // Add to CollectionPage schema on tag pages
  77. if(is_tag() && isset($data['CollectionPage'])) {
  78. $data['CollectionPage']['about'][] = $schema;
  79. }
  80. }
  81. }
  82. }
  83. }
  84. return $data;
  85. }
  86. add_filter( 'rank_math/json_ld', 'add_sameAs_schema_rank_math', 10, 2 );
  87.  
Advertisement
Add Comment
Please, Sign In to add comment