Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. function __add_global_categories( $term_id )
  2. {
  3. if ( get_current_blog_id() !== BLOG_ID_CURRENT_SITE || ( !$term = get_term( $term_id, 'category' ) ) )
  4. return $term_id; // bail
  5.  
  6. if ( !$term->parent || ( !$parent = get_term( $term->parent, 'category' ) ) )
  7. $parent = null;
  8.  
  9. global $wpdb;
  10.  
  11. $blogs = $wpdb->get_col( "SELECT blog_id FROM {$wpdb->blogs} WHERE site_id = '{$wpdb->siteid}'" );
  12. foreach ( $blogs as $blog ) {
  13. $wpdb->set_blog_id( $blog );
  14.  
  15. if ( $parent && ( $_parent = get_term_by( 'slug', $parent->slug, 'category' ) ) )
  16. $_parent_ID = $_parent->term_id;
  17. else
  18. $_parent_ID = 0;
  19.  
  20. wp_insert_term( $term->name, 'category', array(
  21. 'slug' => $term->slug,
  22. 'parent' => $_parent_ID,
  23. 'description' => $term->description
  24. ));
  25. }
  26.  
  27. $wpdb->set_blog_id( BLOG_ID_CURRENT_SITE );
  28. }
  29. add_action( 'created_category', '__add_global_categories' );
  30.  
  31. <?php function mau_add_network_terms($terms_to_add, $siteids, $testrun = false) {
  32.  
  33. // check if this is multisite install
  34. if ( !is_multisite() )
  35. return 'This is not a multisite WordPress installation.';
  36.  
  37. // very basic input check
  38. if ( empty($terms_to_add) || empty($siteids) || !is_array($terms_to_add) || !is_array($siteids) )
  39. return 'Nah, I eat only arrays!';
  40.  
  41. if ($testrun) $log = '<p><em>No need to get excited. This is just a test run.</em></p>';
  42. else $log = '';
  43.  
  44. // loop thru blogs
  45. foreach ($siteids as $blog_id) :
  46.  
  47. switch_to_blog( absint($blog_id) );
  48.  
  49. $log .= '<h4>'.get_blog_details( $blog_id )->blogname.':</h4>';
  50. $log .= '<ul id="ntlog">';
  51.  
  52. // loop thru taxonomies
  53. foreach ( $terms_to_add as $taxonomy => $terms ) {
  54.  
  55. // check if taxonomy exists
  56. if ( taxonomy_exists($taxonomy) ) {
  57. // get taxonomy name
  58. $tax_name = get_taxonomy($taxonomy);
  59. $tax_name = $tax_name->labels->name;
  60.  
  61. //loop thru terms
  62. foreach ( $terms as $term ) {
  63.  
  64. // check if term exists
  65. if ( term_exists($term, $taxonomy) ) {
  66. $log .= "<li class='notice' ><em>$term already exists in the $tax_name taxonomy - not added!</em></li>";
  67.  
  68. } else {
  69.  
  70. // if it doesn't exist insert the $term to $taxonomy
  71. $term = strip_tags($term);
  72. $taxonomy = strip_tags($taxonomy);
  73. if (!$testrun)
  74. wp_insert_term( $term, $taxonomy );
  75. $log .= "<li><b>$term</b> successfully added to the <b>$tax_name</b> taxonomy</li>";
  76. }
  77. }
  78. } else {
  79. // tell our log that taxonomy doesn't exists
  80. $log .= "<li class='notice'><em>The $tax_name taxonomy doesn't exist! Skipping...</em></li>";
  81. }
  82. }
  83.  
  84. $log .= '</ul>';
  85.  
  86. // we're done here
  87. restore_current_blog();
  88.  
  89. endforeach;
  90. if ($testrun) $log .= '<p><em>No need to get excited. This was just the test run.</em></p>';
  91. return $log;
  92. } ?>
  93.  
  94. add_action('init', 'central_taxonomies');
  95.  
  96. function central_taxonomies () {
  97. global $wpdb;
  98.  
  99. $wpdb->terms = "wp_terms";
  100. $wpdb->term_taxonomy = "wp_term_taxonomy";
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement