• Resolved msteidl

    (@msteidl)


    I would like to have the Name of the author in a contact form I am using.

    I tried [dynamic_text dynamictext-50 “CF7_get_post_var key=’post_author'” ] but this returns only the ID, not the name of the author.

    How could I get the name here?

    Plus – if this is possible – I am using Events Manager Plugin and I’d love also to have the name of the contact person of the respective Event as dynamic text. Is there any easy way to do this?

    Regards
    Michaela

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Tessa (they/them), AuRise Creative

    (@tessawatkinsllc)

    We don’t have a built-in shortcode for that but it can be done with a custom shortcode!

    Plop this code snippet either in your theme’s functions.php or in a code snippet:

    function msteidl_author_name() {
    // Use DTX helper function to get the current posts' author ID
    $user_id = intval(wpcf7dtx_get_post_var(array('key' => 'post_author')));

    if(!$user_id) return ''; // Bail on failure

    // Get the user info
    $author = get_userdata($user_id);

    if($author === false) return ''; // Bail on failure

    // Return the author's "Display Name"
    return esc_attr(sanitize_text_field($author->get('display_name')));
    }
    add_shortcode('msteidl_author_name', 'msteidl_author_name');

    Then update the form tag in your form template to this:

    [dynamic_text dynamictext-50 "msteidl_author_name"]

    As for the Events Manager plugin, let me see if I can grab the free version to see how to snag that in a shortcode as well. Is this the one, Events Manager – Calendar, Bookings, Tickets, and More! by Pixelite?

    Plugin Author Tessa (they/them), AuRise Creative

    (@tessawatkinsllc)

    While digging through it’s documentation, I found references to these placeholders used for the contact person in the various templates. Looking at the source code, those placeholders are associated with the event_owner which is also the same as the post author for the event, so technically speaking, the code snippet from earlier should also work when the CF7 form is used on the event pages, especially if the event owner is a registered user in your WordPress website.

    Sources:

    However, because there can be anonymous users, it’s probably best to use their get_contact() function instead of assuming the event author is the same as the event contact, so that custom shortcode would look like this:

    function msteidl_event_contact() {
    // Bail if the function doesn't exist, this is to prevent fatal errors for deactivating events manager plugin
    if(!function_exists('em_get_event')) return '';

    // Get the current event
    $event = em_get_event(); // Returns object of class EM_Event

    // Get the contact
    $contact = $event->get_contact(); // Returns object of class EM_Person

    // Return their name
    return esc_attr(trim(sanitize_text_field($contact->get_name())));
    }
    add_shortcode('msteidl_event_contact', 'msteidl_event_contact');

    Definitions:

    Links to the code and such is mostly for others that come across this thread or for when I want to reference it in the future, so if you’re not tech savvy, that’s okay ??

    Hope this sorts out your needs!

    Thread Starter msteidl

    (@msteidl)

    Dear Tessa,

    this was a huge help for me. I really appreciate your support on this topic.

    Warm Regards

    Michaela

    Plugin Author Tessa (they/them), AuRise Creative

    (@tessawatkinsllc)

    Awesome, glad I could help!

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.