Magic Links filters
These filters change the email Magic Links sends, the fields a View can use as its Email Field, the names of a magic link’s parameters, and the Link Expiration choices.
Changing the email #
Subject
Magic Links sends “Your link to edit an entry” or “Your link to delete an entry” as the subject. The gk/gravityview/magic-links/email/subject filter changes it, and gets edit or delete as its second value. This example names the club in both:
/**
* Sets a different subject for edit links and delete links.
*
* @param string $subject The subject Magic Links would use.
* @param string $action "edit" or "delete".
*
* @return string The subject to send.
*/
add_filter(
'gk/gravityview/magic-links/email/subject',
function ( $subject, $action ) {
return 'delete' === $action
? 'Delete your club listing'
: 'Edit your club listing';
},
10,
2
);Note: Adding custom PHP code snippets to your website explains where this code goes.
Body
To write the body yourself, use gk/gravityview/magic-links/email/message. It gets the HTML body Magic Links would send, the link’s address, how long the link works in seconds, and edit or delete. Put the link in your body, or the email can’t be used:
/**
* Replaces the body of the magic link email.
*
* @param string $message The HTML body Magic Links would send.
* @param string $magic_link The link's address.
* @param int $expiration How long the link works, in seconds.
* @param string $action "edit" or "delete".
*
* @return string The HTML body to send.
*/
add_filter(
'gk/gravityview/magic-links/email/message',
function ( $message, $magic_link, $expiration, $action ) {
$label = 'delete' === $action
? 'Delete your listing'
: 'Edit your listing';
return sprintf(
'<p>Here is the link you asked for.</p>'
. '<p><a href="%1$s">%2$s</a></p>'
. '<p>It works for %3$s.</p>',
esc_url( $magic_link ),
esc_html( $label ),
esc_html( human_time_diff( time(), time() + $expiration ) )
);
},
10,
4
);Headers
Every magic link email has one header, Content-Type: text/html; charset=UTF-8, which the HTML body needs. The gk/gravityview/magic-links/email/headers filter adds others. This example adds a Reply-To address:
/**
* Adds a Reply-To address to the magic link email.
*
* @param string[] $headers The email headers.
*
* @return string[] The headers to send.
*/
add_filter(
'gk/gravityview/magic-links/email/headers',
function ( $headers ) {
$headers[] = 'Reply-To: Membership Office <members@example.com>';
return $headers;
}
);Choosing the Email Field #
Under Email Field, a View’s Magic Links settings list the form’s Email fields. The gk/gravityview/magic-links/email-field-types filter adds other field types to that list. This example adds Single Line Text fields:
/**
* Lists Single Line Text fields under Email Field, next to Email fields.
*
* @param string[] $field_types The field types listed. Default: email.
*
* @return string[] The field types to list.
*/
add_filter(
'gk/gravityview/magic-links/email-field-types',
function ( $field_types ) {
$field_types[] = 'text';
return $field_types;
}
);Renaming the link parameters #
A magic link’s address holds its token in gv_magic and the recipient’s address, encoded, in gv_email. In Magic Links 1.3.0 and newer, gk/gravityview/magic-links/query-param/token and gk/gravityview/magic-links/query-param/email rename them:
/**
* Renames the parameters of new magic links to club_key and club_email.
*/
add_filter( 'gk/gravityview/magic-links/query-param/token', function () {
return 'club_key';
} );
add_filter( 'gk/gravityview/magic-links/query-param/email', function () {
return 'club_email';
} );A name can use lowercase letters, numbers, dashes, and underscores. Magic Links ignores a name that WordPress or GravityView already uses, or the same name for both, and keeps the default names. Links sent with the default names keep working after a rename. Links sent with an earlier custom name stop working when you change it again.
If you keep magic link pages out of search results or page caches by their parameters, cover the new names too. The snippet in Keep Magic Links edit and delete pages out of search results reads them from Magic Links.
Adding longer Link Expiration choices #
Link Expiration offers 5 minutes to 12 hours. The choices come from the View’s default settings, so the gravityview/view/settings/defaults filter can add longer ones. This example adds 1 day and 1 week:
/**
* Adds 1 day and 1 week to the Link Expiration choices.
*
* Priority 20 runs after Magic Links adds its settings.
*
* @param array $settings The default View settings.
*
* @return array The settings with the added choices.
*/
add_filter( 'gravityview/view/settings/defaults', function ( $settings ) {
$key = 'magic_link_expiration';
if ( isset( $settings[ $key ]['options'] ) ) {
$settings[ $key ]['options'][ DAY_IN_SECONDS ] = '1 day';
$settings[ $key ]['options'][ WEEK_IN_SECONDS ] = '1 week';
}
return $settings;
}, 20 );A longer expiration leaves a link usable for longer if the email reaches someone else.