I want to get the authors name as dynamic text

[ad_1]

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?

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!

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer