-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't add programmatic Notification #293
Comments
You should coustomize this example code to your case. |
Hi, thank you for the answer. This is my Custom Trigger Class. It works perfectly if I associate manually a carrier to it on Wordpress admin plugin page, but I would define it programmaticaly, so that I can change the recipient mail by code: `
}` I updated the notification with slug "send_Newsletter_Content" but keeps to not work. ` ` Thank you for your help. |
Your trigger slug is defined in constructor and is equal to "MyCustomTrigger", not "send_Newsletter_Content" - this is your action. |
@cappelluti If this is only for controlling the recipients, you could use this filter https://github.com/BracketSpace/Notification/blob/develop/src/classes/Defaults/Carrier/Email.php#L132 @pewu-dev I tried to contact you today on Messanger, if you are not using it can we connect in any other way? :) |
hi @pewu-dev you can find me on Messanger, my name is Enzo Cappelluti (I tried to contact PeWuDev, but aren't you) |
Yes, it works. Another question.
I would like to pass the same variables $t_id end $content to my custom notification.
how do I get $t_id end $content ? Thank you |
Hi,
I’m sorry for insistence but I don’t understand how to follow your suggestion.
I’m using the following class to send email:
class SendNewslettercontent extends \BracketSpace\Notification\Abstracts\Trigger {
public function __construct() {
parent::__construct('SendNewslettercontent', __( 'Send different newsletter (post, video, suppl..)', 'SendNewslettercontent' ));
$this->add_action( 'send_Newsletter_content', 10, 2 );
}
public function action($t_id , $type_content) {
if ( empty( $t_id ) ) {
return false;
}
$this->title = getMyTitle($type_content);
$this->mail_html = getMyBody($t_id, $type_content);
}
public function merge_tags() {
$this->add_merge_tag( new \BracketSpace\Notification\Defaults\MergeTag\StringTag( array(
'slug' => 'title',
'name' => __( 'title email', 'SendNewslettercontent' ),
'resolver' => function( $trigger ) {return $trigger->title;},
)));
$this->add_merge_tag( new \BracketSpace\Notification\Defaults\MergeTag\HTMLTag( array(
'slug' => 'mail_html',
'name' => __( 'Email in HTML', 'SendNewslettercontent' ),
'resolver' => function( $trigger ) {return $trigger->mail_html;},
)));
}
}
In this way I can trigger my sending email and manage its subject and content:
add_action( 'notification/elements', function() {
notification_register_trigger( new SendNewslettercontent() );
} );
do_action( 'send_Newsletter_content', $t_id , $type_content)
I would control the recipient starting by $type_content variable. You suggested me to use this filter https://github.com/BracketSpace/Notification/blob/develop/src/classes/Defaults/Carrier/Email.php#L132 but I don’t understand how in the past code.
Alternatively I tried to add:
add_action( 'notification/trigger/registered', function($trigger) {
if ( SendNewslettercontent!== $trigger->get_slug() ) {
return;
}
$content = '';
foreach ( $trigger->get_merge_tags( 'visible' ) as $merge_tag ) {
$content .= $merge_tag->get_name() . ': {' . $merge_tag->get_slug() . '}' . "\r\n\r\n";
}
notification( [
'title' => 'My programmatic notification',
'trigger' => SendNewslettercontent,
'carriers' => [
'email' => [
'activated' => true,
'enabled' => true,
'subject' => 'HOW DO I GET BY CUSTOM SUBJECT?,
'body' => HOW DO I GET BY BODY?,
'recipients' => [
[
'type' => 'email',
'recipient' => [email protected],
],
],
],
],
'enabled' => true,
] );
} );
This sends correctly email to recipients that I can define by code but I don’t know how to get title and mail_html
Thank you for your help. Very gratefull.
Greetings,
Enzo Cappelluti
Da: Jakub Mikita <[email protected]>
Inviato: lunedì 19 ottobre 2020 18:45
A: BracketSpace/Notification <[email protected]>
Cc: Enzo Cappelluti <[email protected]>; Mention <[email protected]>
Oggetto: Re: [BracketSpace/Notification] Can't add programmatic Notification (#293)
@cappelluti<https://github.com/cappelluti> If this is only for controlling the recipients, you could use this filter https://github.com/BracketSpace/Notification/blob/develop/src/classes/Defaults/Carrier/Email.php#L132
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<#293 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/ABR5ZTFPVVR57H4JWAMVFPDSLRUJDANCNFSM4SARAYTA>.
|
Hi @cappelluti the Trigger class looks ok. To filter the recipients you can just do this: add_filter( 'notification/carrier/email/recipients', function( $recipients, $carrier, $trigger ) {
if ( 'SendNewslettercontent' !== $trigger->get_slug() ) {
return $recipients;
}
// $trigger->mail_html
// $trigger->title
return [ '[email protected]', '[email protected]' ];
}, 10, 3 ); |
Hi @Kubitomakita , Thanks, |
This might be a bit old; but for others landing on this page. Notifications registered using |
Hi,
as I see in documentation page (https://docs.bracketspace.com/notification/developer/notifications/programmatic-notifications) I would like to add a notification (with email carrier) by code, not using WP (admin) > Notifications > Add New Notification.
I have already registered in function.php my custom trigger with:
Now, I would like to associate it to a custom notification declared in function.php. Using the sample in (https://docs.bracketspace.com/notification/developer/notifications/programmatic-notifications) I would expect to see my new notification in the list of WP notification setting page (as it happen adding custom triggers that I can associate to my notifications) but it doesn't.
In particular I tried this code:
Thank you.
Greeting,
Enzo
The text was updated successfully, but these errors were encountered: