Shortcode Class
Creating a Custom Shortcode Class
class YourNewShortcodes extends \YayMail\Abstracts\BaseShortcode { private static $instance; public static function instance() { if ( null === static::$instance ) { static::$instance = new static(); } return static::$instance; } protected function __construct() { /** * This refers to the array of template IDs where shortcodes can be applied. * We can assign it in 1 of 4 ways * 1. Special Array Templates ID: Examples include new_order, customer_processing_order. * 2. YAYMAIL_WITH_ORDER_EMAILS: Shortcodes applicable only for templates containing order information. * 3. YAYMAIL_NON_ORDER_EMAILS: Shortcodes applicable only for templates without order information. * 4. YAYMAIL_ALL_EMAILS: Shortcodes applicable to all email templates. */ $this->available_email_ids = [ YAYMAIL_ALL_EMAILS ]; parent::__construct(); } public function get_shortcodes() { // Define your function here } }- Function:
get_shortcodespublic function get_shortcodes() { $shortcodes = []; $shortcodes[] = [ 'name' => 'new_shortcode_1', 'description' => 'Custom shortcode 1', 'group' => 'Custom Shortcode', 'callback' => [ $this, 'new_shortcode_1_render_function' ], ]; $shortcodes[] = [ 'name' => 'yaymail_new_shortcode_2', 'description' => 'Custom shortcode 2', 'attributes' => [ 'text_link' => 'here', ], 'group' => 'Custom Shortcode', 'callback' => [ $this, 'new_shortcode_2_render_function' ], ]; return $shortcodes; } public function new_shortcode_1_render_function( $data ) { $render_data = isset( $data['render_data'] ) ? $data['render_data'] : []; $is_placeholder = isset( $data['is_placeholder'] ) ? $data['is_placeholder'] : false; $is_sample = isset( $render_data['is_sample'] ) ? $render_data['is_sample'] : false; $is_customized_preview = isset( $render_data['is_customized_preview'] ) ? $render_data['is_customized_preview'] : false; $content = 'Shortcode content sample'; return $content; } public function new_shortcode_2_render_function( $data, $shortcode_atts = [] ) { $template = ! empty( $data['template'] ) ? $data['template'] : null; $is_placeholder = isset( $data['is_placeholder'] ) ? $data['is_placeholder'] : false; $text_link = isset( $shortcode_atts['text_link'] ) ? $shortcode_atts['text_link'] : ''; if ( empty( $template ) ) { $text_link_color = YAYMAIL_COLOR_WC_DEFAULT; } else { $text_link_color = $template->get_text_link_color(); } return '<a style="color: ' . esc_attr( $text_link_color ) . ';" href="' . esc_url( get_home_url() ) . '"> ' . $text_link . ' </a>'; }
Email render data
Last updated
Was this helpful?