Skip to content

Instantly share code, notes, and snippets.

@pagelab
Created April 26, 2025 02:06
Show Gist options
  • Save pagelab/02afd61f454a79eb125a7c5a78d21eaf to your computer and use it in GitHub Desktop.
Save pagelab/02afd61f454a79eb125a7c5a78d21eaf to your computer and use it in GitHub Desktop.
New shortcode with corrections.
<?php
// Add Shortcode
function subform() {
// Get the current user
$current_user = wp_get_current_user();
// Check if user is logged in and get the nickname
if ($current_user->exists()) {
$user_display_name = $current_user->nickname;
// If nickname is empty, fall back to display_name
if (empty($user_display_name)) {
$user_display_name = $current_user->display_name;
}
} else {
$user_display_name = 'Guest';
}
// Get the blog name
$blog_name = get_bloginfo('name');
// Build the form output
$output = '<p>Hey ' . esc_html($user_display_name) . ', welcome to ' . esc_html($blog_name) . '! You can subscribe to our newsletter here:</p>';
$output .= '<form action="/thank-you">';
$output .= '<label for="email">Enter your email:</label>';
$output .= '<input type="email" id="email" name="email">';
$output .= '<input type="submit" value="Submit">';
$output .= '</form>';
// Return the output
return $output;
}
add_shortcode('subscriptionform', 'subform');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment