Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [WordPress] Nadpisywanie funkcji pluginów
Forum PHP.pl > Forum > Gotowe rozwiązania > Systemy portalowe i CMS'y
AboutMe
Witam,

Czy Wordpress umożliwia nadpisywanie pewnych funkcji danego pluginu z poziomu functions.php szablonu? Widzę że np. Buddypress daje taką możliwość:

http://codex.buddypress.org/developer-docs/action-reference/

Cytat
BuddyPress follows exactly the same plugin API as WordPress. You?ll find that there are hundreds of actions and filters within BuddyPress that will allow you to hook into and modify existing functionality.

By using the plugin API, you can avoid modifying core BuddyPress files allowing you to upgrade BuddyPress smoothly and not lose your existing changes. It really is a bad idea to modify core files, if you want to extend and modify BuddyPress, you should try your hardest to learn how the plugin API works.

A good place to get started is the WordPress plugin API documentation. This will explain to you how actions and filters work, and how you should properly use them. BuddyPress follows exactly the same principles. In fact, BuddyPress uses this API to integrate into WordPress, not a single core modification is needed. That should tell you all you need to know about the power of the plugin API.


Załóżmy że chciałbym nadpisać funkcję bp_create_excerpt() znajdującą się w wp-content/plugins/buddypress/bp-core/bp-core-template.php

Jak nadpisać taką funkcję nie dotykając plików core? Jeśli skopiuję całą funkcję i wkleję ją do functions.php szablonu to dostaję "Fatal error: Cannot redeclare bp_create_excerpt() (previously declared in wp-content/plugins/buddypress/bp-core/bp-core-template.php)"
Sephirus
IMHO nie ma takiej możliwości by nadpisać istniejącą funkcję z samego PHP.

APD posiada funkcję override_function() lecz nie jest ona wyjątkowo sprytnie zrobiona i wymaga wspomnianego APD. Problem tkwi w programowaniu strukturalnym po prostu bo w obiektówce nie ma z tym problemów.

Czemu nie chcesz ruszać plików CORE pluginu? Możesz zawsze zmienić nazwę tej funkcji dodając do nazwy np. "_old" lub coś w ten deseń. Ewentualnie możesz też zmienić działanie funkcji w CORE właśnie. Innych wyjść nie widzę raczej
AboutMe
Jeśli skasuję całą funkcję z wp-content/plugins/buddypress/bp-core/bp-core-template.php i wkleję ją do functions.php szablonu to działa i mogę dowolnie modywikować parametry typu $length = 200 i nie muszę tego zmieniać za każdym razem gdy aktualizuję plugin. Wygląda na to że trzeba najpierw zdeaktywować bp_create_excerpt i z tego co się orientuję to służy to tego:
  1. remove_action( $tag, $function_to_remove, $priority, $accepted_args );

http://codex.wordpress.org/Function_Reference/remove_action

Tylko nie wiem jakich parametrów użyć żeby zdeaktywować poniższe funkcje

  1. function bp_create_excerpt( $text, $length = 200, $options = array() ) {
  2. // Backward compatibility. The third argument used to be a boolean $filter_shortcodes
  3. $filter_shortcodes_default = is_bool( $options ) ? $options : true;
  4.  
  5. $defaults = array(
  6. 'ending' => __( ' ...', 'buddypress' ),
  7. 'exact' => false,
  8. 'html' => true,
  9. 'filter_shortcodes' => $filter_shortcodes_default
  10. );
  11. $r = wp_parse_args( $options, $defaults );
  12. extract( $r );
  13.  
  14. // Save the original text, to be passed along to the filter
  15. $original_text = $text;
  16.  
  17. // Allow plugins to modify these values globally
  18. $length = apply_filters( 'bp_excerpt_length', $length );
  19. $ending = apply_filters( 'bp_excerpt_append_text', $ending );
  20.  
  21. // Remove shortcodes if necessary
  22. if ( !empty( $filter_shortcodes ) )
  23. $text = strip_shortcodes( $text );
  24.  
  25. // When $html is true, the excerpt should be created without including HTML tags in the
  26. // excerpt length
  27. if ( !empty( $html ) ) {
  28. // The text is short enough. No need to truncate
  29. if ( mb_strlen( preg_replace( '/<.*?>/', '', $text ) ) <= $length ) {
  30. return $text;
  31. }
  32.  
  33. $totalLength = mb_strlen( strip_tags( $ending ) );
  34. $openTags = array();
  35. $truncate = '';
  36.  
  37. // Find all the tags and put them in a stack for later use
  38. preg_match_all( '/(<\/?([\w+]+)[^>]*>)?([^<>]*)/', $text, $tags, PREG_SET_ORDER );
  39. foreach ( $tags as $tag ) {
  40. // Process tags that need to be closed
  41. if ( !preg_match( '/img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param/s', $tag[2] ) ) {
  42. if ( preg_match( '/<[\w]+[^>]*>/s', $tag[0] ) ) {
  43. array_unshift( $openTags, $tag[2] );
  44. } else if ( preg_match('/<\/([\w]+)[^>]*>/s', $tag[0], $closeTag ) ) {
  45. $pos = array_search( $closeTag[1], $openTags );
  46. if ( $pos !== false ) {
  47. array_splice( $openTags, $pos, 1 );
  48. }
  49. }
  50. }
  51. $truncate .= $tag[1];
  52.  
  53. $contentLength = mb_strlen( preg_replace( '/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', ' ', $tag[3] ) );
  54. if ( $contentLength + $totalLength > $length ) {
  55. $left = $length - $totalLength;
  56. $entitiesLength = 0;
  57. if ( preg_match_all( '/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', $tag[3], $entities, PREG_OFFSET_CAPTURE ) ) {
  58. foreach ( $entities[0] as $entity ) {
  59. if ( $entity[1] + 1 - $entitiesLength <= $left ) {
  60. $left--;
  61. $entitiesLength += mb_strlen( $entity[0] );
  62. } else {
  63. break;
  64. }
  65. }
  66. }
  67.  
  68. $truncate .= mb_substr( $tag[3], 0 , $left + $entitiesLength );
  69. break;
  70. } else {
  71. $truncate .= $tag[3];
  72. $totalLength += $contentLength;
  73. }
  74. if ( $totalLength >= $length ) {
  75. break;
  76. }
  77. }
  78. } else {
  79. if ( mb_strlen( $text ) <= $length ) {
  80. return $text;
  81. } else {
  82. $truncate = mb_substr( $text, 0, $length - mb_strlen( $ending ) );
  83. }
  84. }
  85.  
  86. // If $exact is false, we can't break on words
  87. if ( empty( $exact ) ) {
  88. $spacepos = mb_strrpos( $truncate, ' ' );
  89. if ( isset( $spacepos ) ) {
  90. if ( $html ) {
  91. $bits = mb_substr( $truncate, $spacepos );
  92. preg_match_all( '/<\/([a-z]+)>/', $bits, $droppedTags, PREG_SET_ORDER );
  93. if ( !empty( $droppedTags ) ) {
  94. foreach ( $droppedTags as $closingTag ) {
  95. if ( !in_array( $closingTag[1], $openTags ) ) {
  96. array_unshift( $openTags, $closingTag[1] );
  97. }
  98. }
  99. }
  100. }
  101. $truncate = mb_substr( $truncate, 0, $spacepos );
  102. }
  103. }
  104. $truncate .= $ending;
  105.  
  106. if ( $html ) {
  107. foreach ( $openTags as $tag ) {
  108. $truncate .= '</' . $tag . '>';
  109. }
  110. }
  111.  
  112. return apply_filters( 'bp_create_excerpt', $truncate, $original_text, $length, $options );
  113.  
  114. }
  115. add_filter( 'bp_create_excerpt', 'stripslashes_deep' );
  116. add_filter( 'bp_create_excerpt', 'force_balance_tags' );



  1. function bp_core_set_avatar_constants() {
  2. global $bp;
  3.  
  4. if ( !defined( 'BP_AVATAR_THUMB_WIDTH' ) )
  5. define( 'BP_AVATAR_THUMB_WIDTH', 50 );
  6.  
  7. if ( !defined( 'BP_AVATAR_THUMB_HEIGHT' ) )
  8. define( 'BP_AVATAR_THUMB_HEIGHT', 50 );
  9.  
  10. if ( !defined( 'BP_AVATAR_FULL_WIDTH' ) )
  11. define( 'BP_AVATAR_FULL_WIDTH', 200 );
  12.  
  13. if ( !defined( 'BP_AVATAR_FULL_HEIGHT' ) )
  14. define( 'BP_AVATAR_FULL_HEIGHT', 200 );
  15.  
  16. if ( !defined( 'BP_AVATAR_ORIGINAL_MAX_WIDTH' ) )
  17. define( 'BP_AVATAR_ORIGINAL_MAX_WIDTH', 200 );
  18.  
  19. if ( !defined( 'BP_AVATAR_ORIGINAL_MAX_FILESIZE' ) ) {
  20. if ( !isset( $bp->site_options['fileupload_maxk'] ) ) {
  21. define( 'BP_AVATAR_ORIGINAL_MAX_FILESIZE', 120000 ); // 5mb
  22. } else {
  23. define( 'BP_AVATAR_ORIGINAL_MAX_FILESIZE', $bp->site_options['fileupload_maxk'] * 1024 );
  24. }
  25. }
  26.  
  27. if ( !defined( 'BP_AVATAR_DEFAULT' ) )
  28. define( 'BP_AVATAR_DEFAULT', BP_PLUGIN_URL . 'bp-core/images/mystery-man.jpg' );
  29.  
  30. if ( !defined( 'BP_AVATAR_DEFAULT_THUMB' ) )
  31. define( 'BP_AVATAR_DEFAULT_THUMB', BP_PLUGIN_URL . 'bp-core/images/mystery-man-50.jpg' );
  32. }
  33. add_action( 'bp_init', 'bp_core_set_avatar_constants', 3 );


2. funkcję można usunąć dodając do functions.php:

remove_action

ale jak usunąć 1. funkcję to nie mam pojęcia
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.