Want to change a simple string in a function within a class. More specifically, I just want to change the “%s” in the last function to a hard-coded URL like “https://whatever.com“.
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 );
}
}
I’ve tried extending the class as a separate plugin.php file, but the output I want happens twice, once as the original class output and another with mine.
My extension:
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
}
Instead of extending the class, figured I’d just modify the original function within the original class but not sure if my filter is syntactically correct (in my functions.php file):
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 );
Should I be overwriting the original function and/or is my approach correct?
One option here is to add a filter without having to modify any code.
You could further enhance the accuracy by adding an if statement based on the page(s) you want this to take effect on.
Also, in both of your attempts, the
sprintf
syntax is incorrect.Your original:
Should be: