Wp Coupons And Deals Shortcodes

Below, you’ll find a detailed guide on how to add the Wp Coupons And Deals Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Wp Coupons And Deals Plugin shortcodes not to show or not to work correctly.

Before starting, here is an overview of the Wp Coupons And Deals Plugin and the shortcodes it provides:

Plugin Icon
WP Coupons and Deals – WordPress Coupon Plugin

"WP Coupons and Deals is a dynamic WordPress Coupon Plugin. It allows you to effortlessly create, manage, and display engaging and responsive coupons and deals on your WordPress site."

★★★★☆ (122) Active Installs: 2000+ Tested with: 6.3.2 PHP Version: 5.6
Included Shortcodes:
  • [wpcd_coupon]
  • [wpcd_code]

Wp Coupons And Deals [wpcd_coupon] Shortcode

The WP Coupons and Deals shortcode is a powerful tool that aids in displaying specific coupons on your website. It fetches coupons based on the provided ID, controls the number of coupons displayed, and hides expired ones. This shortcode also supports various coupon templates, allows view count tracking, and ensures compatibility with AMP pages.

Shortcode: [wpcd_coupon]

Parameters

Here is a list of all possible wpcd_coupon shortcode parameters and attributes:

  • id – The unique identifier of the specific coupon
  • total – Dictates the number of coupons to display

Examples and Usage

Basic example – Display a single coupon using its ID

[wpcd_coupon id=1 /]

Advanced examples

Display a specific number of coupons using the ‘total’ attribute. In this example, we will display 5 coupons.

[wpcd_coupon total=5 /]

Display a specific coupon and limit the number of coupons that are displayed. Here, we are displaying the coupon with ID 2, and limiting the total number of displayed coupons to 3.

[wpcd_coupon id=2 total=3 /]

Please note that the ‘id’ attribute refers to the specific coupon you want to display, while the ‘total’ attribute limits the number of coupons that are displayed.

PHP Function Code

In case you have difficulties debugging what causing issues with [wpcd_coupon] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'wpcd_coupon', array( __CLASS__, 'wpcd_coupon' ) );

Shortcode PHP function:

function wpcd_coupon( $atts )
    {
        global  $wpcd_atts ;
        global  $wpcd_coupon ;
        /**
         * These are the shortcode attributes.
         *
         * @since 1.0
         */
        $wpcd_atts = shortcode_atts( array(
            'id'    => '',
            'total' => '-1',
        ), $atts, 'wpcd_coupon' );
        /**
         * Arguments to be used for a custom Query.
         *
         * @since 1.0
         */
        $wpcd_arg = array(
            'p'              => esc_attr( $wpcd_atts['id'] ),
            'posts_per_page' => esc_attr( $wpcd_atts['total'] ),
            'post_type'      => 'wpcd_coupons',
            'post_status'    => 'publish',
        );
        /**
         * New custom query to get post and post data
         * from the custom coupon post type.
         *
         * @since 1.0
         */
        $wpcd_coupon = new WP_Query( $wpcd_arg );
        $output = '';
        //Hide expired coupon feature
        $today = date( 'd-m-Y' );
        $hide_expired_coupon = get_option( 'wpcd_hide-expired-coupon' );
        $enable_stats = get_option( 'wpcd_enable-stats-count' );
        while ( $wpcd_coupon->have_posts() ) {
            $wpcd_coupon->the_post();
            global  $coupon_id ;
            $template = new WPCD_Template_Loader();
            $coupon_id = get_the_ID();
            $expire_date = get_post_meta( $coupon_id, 'coupon_details_expire-date', true );
            $coupon_template = get_post_meta( $coupon_id, 'coupon_details_coupon-template', true );
            $coupon_type = get_post_meta( $coupon_id, 'coupon_details_coupon-type', true );
            
            if ( !empty($enable_stats) && $enable_stats == 'on' ) {
                $view_count = get_post_meta( $coupon_id, 'coupon_view_count', true );
                
                if ( empty($view_count) || !is_numeric( $view_count ) ) {
                    $view_count = 1;
                } else {
                    $view_count = intval( $view_count ) + 1;
                }
                
                update_post_meta( $coupon_id, 'coupon_view_count', $view_count );
            }
            
            
            if ( $coupon_type === 'Image' ) {
                ob_start();
                $template->get_template_part( 'shortcode-image' );
                $output = ob_get_clean();
                if ( WPCD_Amp::wpcd_amp_is() ) {
                    WPCD_Amp::instance()->setCss( 'shortcode_image' );
                }
                wp_reset_postdata();
                return $output;
            }
            
            // Hide expired coupon feature (default is Not to hide).
            
            if ( !empty($hide_expired_coupon) || $hide_expired_coupon == "on" ) {
                $expire_date = get_post_meta( $coupon_id, 'coupon_details_expire-date', true );
                
                if ( !empty($expire_date) ) {
                    if ( (string) (int) $expire_date != $expire_date ) {
                        $expire_date = strtotime( $expire_date );
                    }
                    
                    if ( $coupon_template !== 'Template Four' ) {
                        if ( $expire_date < strtotime( $today ) ) {
                            continue;
                        }
                    } else {
                        $second_expire_date = get_post_meta( $coupon_id, 'coupon_details_second-expire-date', true );
                        $third_expire_date = get_post_meta( $coupon_id, 'coupon_details_third-expire-date', true );
                        if ( $expire_date < strtotime( $today ) && $second_expire_date < strtotime( $today ) && $third_expire_date < strtotime( $today ) ) {
                            continue;
                        }
                    }
                
                }
            
            }
            
            
            if ( wcad_fs()->is_plan__premium_only( 'pro' ) or wcad_fs()->can_use_premium_code() ) {
                
                if ( $coupon_template == 'Template One' ) {
                    $argcss = 'shortcode_one';
                    ob_start();
                    $template->get_template_part( 'shortcode-one__premium_only' );
                    $output = ob_get_clean();
                } elseif ( $coupon_template == 'Template Two' ) {
                    $argcss = 'shortcode_two';
                    ob_start();
                    $template->get_template_part( 'shortcode-two__premium_only' );
                    $output = ob_get_clean();
                } elseif ( $coupon_template == 'Template Three' ) {
                    $argcss = 'shortcode_three';
                    ob_start();
                    $template->get_template_part( 'shortcode-three__premium_only' );
                    $output = ob_get_clean();
                } elseif ( $coupon_template == 'Template Four' ) {
                    $argcss = 'shortcode_four';
                    ob_start();
                    $template->get_template_part( 'shortcode-four__premium_only' );
                    $output = ob_get_clean();
                } elseif ( $coupon_template == 'Template Five' ) {
                    $argcss = 'shortcode_five';
                    ob_start();
                    $template->get_template_part( 'shortcode-five__premium_only' );
                    $output = ob_get_clean();
                } elseif ( $coupon_template == 'Template Six' ) {
                    $argcss = 'shortcode_six';
                    ob_start();
                    $template->get_template_part( 'shortcode-six__premium_only' );
                    $output = ob_get_clean();
                } elseif ( $coupon_template == 'Template Seven' ) {
                    $argcss = 'shortcode_seven';
                    ob_start();
                    $template->get_template_part( 'shortcode-seven__premium_only' );
                    $output = ob_get_clean();
                } elseif ( $coupon_template == 'Template Eight' ) {
                    $argcss = 'shortcode_eight';
                    ob_start();
                    $template->get_template_part( 'shortcode-eight__premium_only' );
                    $output = ob_get_clean();
                } elseif ( $coupon_template == 'Template Nine' ) {
                    $argcss = 'shortcode_nine';
                    ob_start();
                    $template->get_template_part( 'shortcode-nine__premium_only' );
                    $output = ob_get_clean();
                } else {
                    $argcss = 'shortcode_default';
                    ob_start();
                    $template->get_template_part( 'shortcode-default' );
                    $output = ob_get_clean();
                }
            
            } else {
                $argcss = 'shortcode_default';
                ob_start();
                $template->get_template_part( 'shortcode-default' );
                $output = ob_get_clean();
            }
            
            
            if ( WPCD_Amp::wpcd_amp_is() ) {
                WPCD_Amp::instance()->setCss( 'shortcode_common' );
                WPCD_Amp::instance()->setCss( $argcss );
                $user_stylesheets = WPCD_Assets::wpcd_stylesheets( true, $coupon_id, $coupon_template );
                WPCD_Amp::instance()->setCss( $user_stylesheets, false );
            }
        
        }
        wp_reset_postdata();
        return $output;
    }

Code file location:

wp-coupons-and-deals/wp-coupons-and-deals/includes/classes/wpcd-short-code.php

Wp Coupons And Deals [wpcd_code] Shortcode

The WP Coupons and Deals shortcode is a tool that generates a specific coupon code. It retrieves data from the custom coupon post type using a custom query. The shortcode attributes, ‘id’ and ‘total’, are customizable. The ‘id’ refers to the specific coupon, while ‘total’ limits the number of posts per page. After execution, it returns the generated coupon code.

Shortcode: [wpcd_code]

Parameters

Here is a list of all possible wpcd_code shortcode parameters and attributes:

  • id – The unique identifier of the specific coupon
  • total – Number of coupons to display, ‘-1’ shows all

Examples and Usage

Basic example – Show a single coupon by its ID.

[wpcd_code id=1 /]

Advanced examples

Display multiple coupons by their IDs, showing up to 5 coupons at a time.

[wpcd_code id="1,2,3,4,5" total=5 /]

Display all published coupons. The ‘-1’ in the ‘total’ attribute means there is no limit on the number of coupons displayed.

[wpcd_code total=-1 /]

In these examples, the ‘id’ attribute is used to specify the IDs of the coupons you want to display, and the ‘total’ attribute is used to limit the number of coupons displayed at once. If the ‘total’ attribute is set to ‘-1’, all published coupons will be displayed.

PHP Function Code

In case you have difficulties debugging what causing issues with [wpcd_code] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'wpcd_code', array( __CLASS__, 'wpcd_coupon_code' ) );

Shortcode PHP function:

function wpcd_coupon_code( $atts )
    {
        global  $wpcd_code_atts ;
        global  $wpcd_coupon_code ;
        /**
         * These are the shortcode attributes.
         *
         * @since 1.4
         */
        $wpcd_code_atts = shortcode_atts( array(
            'id'    => '',
            'total' => '-1',
        ), $atts, 'wpcd_code' );
        /**
         * Arguments to be used for a custom Query.
         *
         * @since 1.4
         */
        $wpcd_code_arg = array(
            'p'              => esc_attr( $wpcd_code_atts['id'] ),
            'posts_per_page' => esc_attr( $wpcd_code_atts['total'] ),
            'post_type'      => 'wpcd_coupons',
            'post_status'    => 'publish',
        );
        /**
         * New custom query to get post and post data
         * from the custom coupon post type.
         *
         * @since 1.4
         */
        $wpcd_coupon_code = new WP_Query( $wpcd_code_arg );
        $template = new WPCD_Template_Loader();
        ob_start();
        $template->get_template_part( 'shortcode-code' );
        // Return Variables
        $output = ob_get_clean();
        if ( WPCD_Amp::wpcd_amp_is() ) {
            WPCD_Amp::instance()->setCss( 'shortcode_common' );
        }
        wp_reset_postdata();
        return $output;
    }

Code file location:

wp-coupons-and-deals/wp-coupons-and-deals/includes/classes/wpcd-short-code.php

Conclusion

Now that you’ve learned how to embed the Wp Coupons And Deals Plugin shortcodes, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *