2016-05-21 12 views
1

クラス内の関数内の単純な文字列を変更したい。具体的には、最後の機能の "%s"を "https://whatever.com"のようなハードコードされたURLに変更したいだけです。WordPressプラグインで既存のクラスのメソッド/関数を変更する


class WPJMCL_Claim_Free { 

    public function __construct() { 
     add_action('template_redirect', array($this, 'create_claim')); 
     add_action('template_redirect', array($this, 'display_guest_notice')); // I WANT TO CHANGE THIS ONE 
    } 

    public function create_claim() { 
     // CODE I DON'T CARE ABOUT 
    } 


    public function display_guest_notice() { 
     if (! isset($_GET[ 'action' ]) || 'claim_as_guest' != $_GET[ 'action' ]) { 
      return; 
     } 

     if (! isset($_GET[ 'listing_id' ])) { 
      return; 
     } 

     $listing_id = absint($_GET[ 'listing_id' ]); 

     // Add a notice if the theme is using WC 
     if (defined('WC_VERSION')) { 
      wc_add_notice(sprintf(__('Please <a href="%s">log in</a> to claim this listing.', 'wp-job-manager-claim-listing'), wp_login_url(get_permalink($listing_id)))); 
     } 

     do_action('wpjmcl_guest_claim_redirect', $listing_id); 
    } 

} 

私は別のplugin.phpファイルとしてクラスを拡張しようとしましたが、私が欲しいの出力は、一度元のクラスの出力と鉱山を持つ別として、二回起こります。

マイ拡張子:

add_action('init', 'claim_redirect_mods_loader', 0); 

function claim_redirect_mods_loader() { 

if(class_exists('WPJMCL_Claim_Free')){ 

    class my_WPJMCL_Claim_Free extends WPJMCL_Claim_Free { 

     public function __construct() { 
      add_action('template_redirect', array($this, 'display_guest_notice')); 
     } 

     public function display_guest_notice() { 
      if (! isset($_GET[ 'action' ]) || 'claim_as_guest' != $_GET[ 'action' ]) { 
       return; 
      } 

      if (! isset($_GET[ 'listing_id' ])) { 
       return; 
      } 

      $listing_id = absint($_GET[ 'listing_id' ]); 

      // Add a notice if the theme is using WC 
      if (defined('WC_VERSION')) { 
       wc_add_notice(sprintf(__('Please <a href="https://directory.supportpay.com/my-account/">log in or register</a> to claim this listing.', 'wp-job-manager-claim-listing'), wp_login_url(get_permalink($listing_id)))); 
      } 
      do_action('wpjmcl_guest_claim_redirect', $listing_id); 
     } 

    } // End Class 

    new my_WPJMCL_Claim_Free(); 

} // End if 

} 
代わりにクラスを拡張する、私はちょうど元のクラスの中に、元の関数を変更したい考え出したが(私のフィルタが構文的に正しいかどうかわからない

私のfunctions.phpファイル):

global $WPJMCL_Claim_Free; 

function my_display_guest_notice(){ 

    if (! isset($_GET[ 'action' ]) || 'claim_as_guest' != $_GET[ 'action' ]) { 
      return; 
    } 

    if (! isset($_GET[ 'listing_id' ])) { 
     return; 
    } 

    $listing_id = absint($_GET[ 'listing_id' ]); 

    // Add a notice if the theme is using WC 
    if (defined('WC_VERSION')) { 
     wc_add_notice(sprintf(__('Please <a href="https://directory.supportpay.com/my-account/">log in or register</a> to claim this listing.', 'wp-job-manager-claim-listing'), wp_login_url(get_permalink($listing_id)))); 
    } 

    do_action('wpjmcl_guest_claim_redirect', $listing_id); 
} 

add_filter('display_guest_notice', array($WPJMCL_Claim_Free, 'my_display_guest_notice'), 999); 

元の関数を上書きする必要がありますか、私のアプローチが正しいのでしょうか?

答えて

0

1つのオプションは、コードを変更することなくフィルタを追加することです。

add_filter('woocommerce_add_success', 'your_function_name'); 
function your_function_name($message) { 
    $string_to_search = ' to claim this listing.'; 
    if(FALSE === strpos($message, $string_to_search)) { 
      return $message; 
    } 

    return 'Please <a href="https://directory.supportpay.com/my-account/">log in or register</a> to claim this listing.'; 
} 

これを有効にするページに基づいてif文を追加することで、精度をさらに高めることができます。

また、両方の試みで、sprintfの構文が正しくありません。

あなたの元:

wc_add_notice(sprintf(__('Please <a href="https://directory.supportpay.com/my-account/">log in or register</a> to claim this listing.', 'wp-job-manager-claim-listing'), wp_login_url(get_permalink($listing_id)))); 

は次のようになります。

wc_add_notice(sprintf(__('Please <a href="%s">log in</a> to claim this listing.', 'wp-job-manager-claim-listing'), "https://directory.supportpay.com/my-account/")); 
関連する問題