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. Adding custom PHP code snippets to your website explains where to add the examples on this page.
Changing the email #
Magic Links sends this email after the request form has answered, often from a background job that doesn’t load the View’s page. Add these filters where they load on every request.
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
);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;
}
);The list leaves out fields inside a Repeater field, also for the field types this filter adds. A Repeater holds rows, so a field inside it has no single address to check a magic link against.
A View that was already set to an Email field inside a Repeater keeps that setting. Its Email Field then reads “[field label] (inside a Repeater: no magic link can be sent for it, choose another field)”.
The addresses in that View’s entries get no magic link, and the {gv_magic_link} merge tag prints nothing for them. Addresses in Allowed Emails can still request a link from the View.
To send links to the entries’ addresses, choose an Email field outside the Repeater under Email Field and click Update.
Renaming the link parameters #
A magic link’s address holds its token in gv_magic and the recipient’s address, encoded, in gv_email. The gk/gravityview/magic-links/query-param/token and gk/gravityview/magic-links/query-param/email filters 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, - and _. Magic Links ignores a name that WordPress or GravityView already uses, or the same name for both, and keeps the default names.
The filters must run when a link is made and when it’s opened. Links sent with the default names keep working after a rename, while 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 );The longer the expiration, the longer a link stays usable if the email reaches someone else.
A longer Link Expiration doesn’t keep a link for editing working past either Edit Window:
- Once the Edit Window on the Edit Entry tab has closed for the entry, the link opens “The deadline for editing this entry has passed.”
- Once the Edit Window on the Magic Links tab has passed, the link opens the Request Edit Access form.
Magic links and the Edit Window settings explains both settings.