• Resolved larissas

    (@larissas)


    Hi, I need to create a custom shortcode for BNFW that displays the date a year from today. I already created the shortcode using add_shortcode() but don’t understand the instructions on the BNFW site about how to add that new shortcode. You suggest using this code as a starting point:

    add_filter('wp_mail', function($args) {
    $args['message'] = do_shortcode($args['message']);
    return $args;
    }, 1, 1);

    But how would I incorporate the new shortcode into that filter? For reference, the code I’m using to generate the new shortcode is:

    function displayFutureDate() {
    	$futureDate = date('F j, Y', strtotime('+1 year'));
    	return $futureDate;
    }
    
    add_shortcode('future_date', 'displayFutureDate');
Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Jack – BNFW

    (@voltronik)

    Hi @larissas,
    Thanks for your message.

    You’re essentially there.
    You’ve written the code for the custom shortcode and you can now use the [future_date] shortcode in your notifications. The filter for wp_mail(), from the BNFW documentation, is really just informing the wp_mail() function that more information is coming so we have to use it after you’ve registered your shortcode.

    I’ll update the documentation now to make this clearer so that when you see this, it should be available on this page: https://betternotificationsforwp.com/documentation/adding-custom-shortcodes/

    Let me know if this helps.

    Thread Starter larissas

    (@larissas)

    Hi! Thank you, this did help and that shortcode is working now, but I have another question! I have another shortcode that requires me to access user meta. The way I have it set up now is like this:

    function displayOrgName() {
    	$org_id = get_user_meta(
    		get_current_user_id(),
    		'cmp_organization',
    		true
    	);
    
    	$org_post = get_post( $org_id );
    	$org_name = $org_post->post_title;
    
    	return $org_name;
    }

    This shortcode works if I add it to a page, but get_current_user_id() doesn’t seem to return the correct ID in BNFW emails. I see that some of the BNFW shortcodes, like [email_user_id], have to access user data. Is there a different way that I should be trying to grab the user ID?

    Plugin Author Jack – BNFW

    (@voltronik)

    Hi @larissas,
    Which user are you trying to get the user ID for:
    Is it the user triggering the email or the user who the email is going to?

    Thread Starter larissas

    (@larissas)

    Hi! In this use case it will be the same person (the email goes to the person who fills out the form), but technically I think the user triggering the email is what we need.

    Plugin Author Jack – BNFW

    (@voltronik)

    Hi @larissas,
    Thanks for clarifying.

    There should already be a shortcode for this (e.g. [user_login]) but it depends which notification you’re using as some aren’t available to use. Here’s an example of a notification that does support them though (scroll to the bottom of the page): https://betternotificationsforwp.com/documentation/notifications/shortcodes/?notification=update-post

    Let me know if this helps.

    Thread Starter larissas

    (@larissas)

    Hi there! The issue is that I need to grab post metadata from a custom post affiliated with the user who triggers the email. The custom post ID is part of the user meta. So right now I’m trying to do it like this:

    function displayOrgName() {
    	$org_id = get_user_meta(
    		get_current_user_id(), //grab custom post ID from user meta
    		'cmp_organization',
    		true
    	);
    
    	$org_post = get_post( $org_id );
    	$org_name = $org_post->post_title;
    
    	return $org_name;
    }

    The above code works if I’m using the shortcode on a post or page, but does not work in an email. So rather than printing user information, I need to access user information and then display post meta.

    Plugin Author Jack – BNFW

    (@voltronik)

    Hi @larissas,
    I think this is because there isn’t necessarily a user available at the time the notification is sent as it’s triggered by a hook.

    I’m not sure how else you can get this without diving into the code in WordPress’s core to see what’s available at the time. I’d like to help more but it’s outside of the scope of support I can provide for the plugin unfortunately. If needed, you may need a developer to help with this.

    Plugin Author Jack – BNFW

    (@voltronik)

    Closing due to inactivity. If you need further help with this, please feel free to re-open this thread.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Help with custom shortcode’ is closed to new replies.