Below, you’ll find a detailed guide on how to add the Rafflepress 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 Rafflepress Plugin shortcodes not to show or not to work correctly.
Before starting, here is an overview of the Rafflepress Plugin and the shortcodes it provides:
"Giveaways and Contests by RafflePress is a dynamic plugin that boosts your website traffic, expands your email subscriber base, and increases your social media followers through engaging contests and giveaways."
- [rafflepress]
- [rafflepress_latest_giveaway]
- [rafflepress_gutenberg]
Rafflepress [rafflepress] Shortcode
The RafflePress shortcode is used to display a specific raffle on a WordPress page or post. By inputting the raffle’s ID into the shortcode, you can easily embed the raffle onto your website.
Shortcode: [rafflepress]
Parameters
Here is a list of all possible rafflepress shortcode parameters and attributes:
id
– Unique identifier for the specific giveawaymin_height
– Sets the minimum height of the giveaway iframegiframe
– Determines if the giveaway will be displayed in an iframe
Examples and Usage
Basic example – Displaying a RafflePress giveaway using the ID parameter.
[rafflepress id=1 /]
Advanced examples
Displaying a RafflePress giveaway using the ID parameter and setting a minimum height for the giveaway.
[rafflepress id=1 min_height='500px' /]
Displaying a RafflePress giveaway using the ID parameter, setting a minimum height for the giveaway, and enabling the Google iframe.
[rafflepress id=1 min_height='500px' giframe='true' /]
These shortcodes allow you to embed RafflePress giveaways in your WordPress posts and pages, with the flexibility to customize the display based on your preferences.
PHP Function Code
In case you have difficulties debugging what causing issues with [rafflepress]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'rafflepress', 'rafflepress_lite_display_shortcode' );
Shortcode PHP function:
function rafflepress_lite_display_shortcode( $atts ) {
wp_enqueue_script( 'rafflepress-iframeresizer-frontend' );
$a = shortcode_atts(
array(
'id' => '0',
'min_height' => '',
'giframe' => 'false',
),
$atts
);
// Sanitize input.
$a['id'] = sanitize_text_field( wp_unslash( $a['id'] ) );
$a['min_height'] = sanitize_text_field( wp_unslash( $a['min_height'] ) );
$a['giframe'] = sanitize_text_field( wp_unslash( $a['giframe'] ) );
global $wpdb;
$id = absint( $a['id'] );
// Get Giveaway
$tablename = $wpdb->prefix . 'rafflepress_giveaways';
$sql = "SELECT active FROM $tablename WHERE id = %d";
$safe_sql = $wpdb->prepare( $sql, $id );
$active = $wpdb->get_var( $safe_sql );
$ref = '';
if ( ! empty( $_GET['rpr'] ) ) {
$ref = $_GET['rpr'];
}
$parent_url = ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http' ) . "://$_SERVER[HTTP_HOST]" . strtok( $_SERVER['REQUEST_URI'], '?' );
if ( ! get_option( 'permalink_structure' ) ) {
$parent_url = ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http' ) . "://$_SERVER[HTTP_HOST]" . $_SERVER['REQUEST_URI'];
}
ob_start();
?>
<?php
// wp_print_scripts('rafflepress-if-shortcode');
?>
<style>
.rafflepress-giveaway-iframe-wrapper iframe {
width: 1px;
min-width: 100%;
*width: 100%;
<?php if ( ! empty( $_GET['context'] ) && $_GET['context'] == 'edit' ) { ?>
height: 600px;
<?php } else { ?>
height: 600px;
<?php } ?>
}
.rafflepress_iframe_loading {
background-image: url('data:image/gif;base64,R0lGODlhEAAQAPIAAP///wAAAMLCwkJCQgAAAGJiYoKCgpKSkiH/C05FVFNDQVBFMi4wAwEAAAAh/hpDcmVhdGVkIHdpdGggYWpheGxvYWQuaW5mbwAh+QQJCgAAACwAAAAAEAAQAAADMwi63P4wyklrE2MIOggZnAdOmGYJRbExwroUmcG2LmDEwnHQLVsYOd2mBzkYDAdKa+dIAAAh+QQJCgAAACwAAAAAEAAQAAADNAi63P5OjCEgG4QMu7DmikRxQlFUYDEZIGBMRVsaqHwctXXf7WEYB4Ag1xjihkMZsiUkKhIAIfkECQoAAAAsAAAAABAAEAAAAzYIujIjK8pByJDMlFYvBoVjHA70GU7xSUJhmKtwHPAKzLO9HMaoKwJZ7Rf8AYPDDzKpZBqfvwQAIfkECQoAAAAsAAAAABAAEAAAAzMIumIlK8oyhpHsnFZfhYumCYUhDAQxRIdhHBGqRoKw0R8DYlJd8z0fMDgsGo/IpHI5TAAAIfkECQoAAAAsAAAAABAAEAAAAzIIunInK0rnZBTwGPNMgQwmdsNgXGJUlIWEuR5oWUIpz8pAEAMe6TwfwyYsGo/IpFKSAAAh+QQJCgAAACwAAAAAEAAQAAADMwi6IMKQORfjdOe82p4wGccc4CEuQradylesojEMBgsUc2G7sDX3lQGBMLAJibufbSlKAAAh+QQJCgAAACwAAAAAEAAQAAADMgi63P7wCRHZnFVdmgHu2nFwlWCI3WGc3TSWhUFGxTAUkGCbtgENBMJAEJsxgMLWzpEAACH5BAkKAAAALAAAAAAQABAAAAMyCLrc/jDKSatlQtScKdceCAjDII7HcQ4EMTCpyrCuUBjCYRgHVtqlAiB1YhiCnlsRkAAAOwAAAAAAAAAAAA==') !important;
background-repeat: no-repeat !important;
background-position: center 100px !important;
height: 100%;
}
</style>
<?php $iframe_uid = mt_rand( 10000000, 99999999 ); ?>
<div id="rafflepress-giveaway-iframe-wrapper-<?php echo $iframe_uid; ?>" class="rafflepress-giveaway-iframe-wrapper rafflepress_iframe_loading"></div>
<script>
function rafflepress_getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return "";
if (!results[2]) return "";
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
function insertIframe( ID, src, minHeight) {
var wrapperID = 'rafflepress-giveaway-iframe-wrapper-'+ID;
var iframe = document.createElement('iframe');
iframe.setAttribute('id', 'rafflepress-'+ID);
iframe.setAttribute('class', 'rafflepress-iframe');
iframe.setAttribute('src', src);
iframe.setAttribute('frameborder', '0');
iframe.setAttribute('scrolling', 'no');
iframe.setAttribute('allowtransparency', 'true');
if (minHeight) {
iframe.setAttribute('style', 'min-height:'+minHeight);
}
iframe.setAttribute('onload', 'rafflepress_resize_iframe_'+ID+'(this)');
document.getElementById(wrapperID).appendChild(iframe);
}
// phpcs:disable
insertIframe( '<?php echo $iframe_uid; ?>','<?php echo trailingslashit( home_url() ) . '?rafflepress_page=rafflepress_render&rafflepress_id=' . urlencode($id) . '&iframe=1&giframe=' . urlencode($a['giframe']) . '&rpr=' . urlencode($ref) . '&parent_url=' . urlencode( $parent_url ); ?>&<?php echo mt_rand( 1, 99999 ); ?>&rp-email='+rafflepress_getParameterByName('rp-email',location.href)+'&rp-name='+rafflepress_getParameterByName('rp-name',location.href),'<?php echo esc_html( $a['min_height'] ); ?>' );
// phpcs:enable
</script>
<script>
function rafflepress_resize_iframe_<?php echo $iframe_uid; ?>(){
iFrameResize({
log: false,
onMessage: function(messageData) {
if (messageData.message == 'rafflepress_loaded') {
var el = document.getElementById('rafflepress-giveaway-iframe-wrapper-<?php echo $iframe_uid; ?>');
var className = "rafflepress_iframe_loading";
if (el.classList)
el.classList.remove(className);
else
el.className = el.className.replace(new RegExp('(^|\\b)' + className.split(' ').join('|') +
'(\\b|$)', 'gi'), ' ');
}
}
}, '#rafflepress-<?php echo $iframe_uid; ?>');
};
</script>
<?php
$output = ob_get_clean();
if ( empty( $active ) ) {
return '';
} else {
return $output;
}
}
Code file location:
rafflepress/rafflepress/app/rafflepress.php
Rafflepress [rafflepress_latest_giveaway] Shortcode
The RafflePress Latest Giveaway shortcode is designed to display the most recent active giveaway on your website. This shortcode fetches the latest active giveaway from the ‘rafflepress_giveaways’ table in your database and displays it on the front-end. It’s a handy tool for promoting your latest giveaways.
Shortcode: [rafflepress_latest_giveaway]
Parameters
Here is a list of all possible rafflepress_latest_giveaway shortcode parameters and attributes:
id
– The unique identifier of the latest active rafflepress giveaway
Examples and Usage
Basic example – The basic usage of the ‘rafflepress_latest_giveaway’ shortcode. This shortcode will display the latest active giveaway from the RafflePress plugin.
[rafflepress_latest_giveaway /]
Advanced examples
Using the ‘rafflepress_latest_giveaway’ shortcode with a custom attribute. In the following example, the ‘id’ attribute is used to display a specific giveaway by its ID. If the specified giveaway is active, it will be displayed; otherwise, the latest active giveaway will be displayed.
[rafflepress_latest_giveaway id=5 /]
Another advanced usage of the ‘rafflepress_latest_giveaway’ shortcode is by combining it with other shortcodes. In the following example, the ‘rafflepress_latest_giveaway’ shortcode is used in conjunction with the ‘column’ shortcode to display the latest active giveaway in a specific column of a page or post.
[column md="6"] [rafflepress_latest_giveaway /] [/column]
PHP Function Code
In case you have difficulties debugging what causing issues with [rafflepress_latest_giveaway]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'rafflepress_latest_giveaway', 'rafflepress_lite_display_shortcode_latest' );
Shortcode PHP function:
function rafflepress_lite_display_shortcode_latest( $atts ) {
global $wpdb;
// Get Giveaway
$tablename = $wpdb->prefix . 'rafflepress_giveaways';
$sql = 'SELECT id FROM wp_rafflepress_giveaways WHERE active=1 order by created_at desc limit 1';
$id = $wpdb->get_var( $sql );
$atts = array( 'id' => $id );
$output = rafflepress_lite_display_shortcode( $atts );
return $output;
}
Code file location:
rafflepress/rafflepress/app/rafflepress.php
Rafflepress [rafflepress_gutenberg] Shortcode
The RafflePress Gutenberg shortcode is used to display giveaways on your website. It’s integrated with the Gutenberg editor for seamless usage. This shortcode enqueues the ‘rafflepress_iframeresizer_frontend’ script, sanitizes input, fetches the active giveaway from the database, and finally displays it in an iframe.
Shortcode: [rafflepress_gutenberg]
Parameters
Here is a list of all possible rafflepress_gutenberg shortcode parameters and attributes:
id
– Specifies the unique identifier of the giveaway.min_height
– Sets the minimum height for the iframe.giframe
– Determines if the iframe should be used in Gutenberg editor.
Examples and Usage
Basic Example – Displaying a RafflePress giveaway using the ID of the giveaway.
[rafflepress_gutenberg id=1 /]
Advanced Examples
Displaying a RafflePress giveaway using the ID and setting a minimum height for the iframe.
[rafflepress_gutenberg id=1 min_height="500px" /]
Displaying a RafflePress giveaway using the ID, setting a minimum height for the iframe, and enabling the Gutenberg iframe.
[rafflepress_gutenberg id=1 min_height="500px" giframe="true" /]
PHP Function Code
In case you have difficulties debugging what causing issues with [rafflepress_gutenberg]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'rafflepress_gutenberg', 'rafflepress_lite_display_gutenberg_shortcode' );
Shortcode PHP function:
function rafflepress_lite_display_gutenberg_shortcode( $atts ) {
wp_enqueue_script( 'rafflepress-iframeresizer-frontend' );
$a = shortcode_atts(
array(
'id' => '0',
'min_height' => '',
'giframe' => 'false',
),
$atts
);
// Sanitize input.
$a['id'] = sanitize_text_field( wp_unslash( $a['id'] ) );
$a['min_height'] = sanitize_text_field( wp_unslash( $a['min_height'] ) );
$a['giframe'] = sanitize_text_field( wp_unslash( $a['giframe'] ) );
global $wpdb;
$id = absint( $a['id'] );
// Get Giveaway
$tablename = $wpdb->prefix . 'rafflepress_giveaways';
$sql = "SELECT active FROM $tablename WHERE id = %d";
$safe_sql = $wpdb->prepare( $sql, $id );
$active = $wpdb->get_var( $safe_sql );
$ref = '';
if ( ! empty( $_GET['rpr'] ) ) {
$ref = $_GET['rpr'];
}
$parent_url = ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http' ) . "://$_SERVER[HTTP_HOST]" . strtok( $_SERVER['REQUEST_URI'], '?' );
ob_start();
$style = '';
if ( ! empty( $a['min_height'] ) ) {
$style = 'style="min-height:' . esc_html( $a['min_height'] ) . '"';
}
?>
<?php
// wp_print_scripts('rafflepress-if-shortcode');
?>
<style>
.rafflepress-giveaway-iframe-wrapper iframe {
width: 1px;
min-width: 100%;
*width: 100%;
height: 600px;
}
.rafflepress_iframe_loading {
background-image: url('data:image/gif;base64,R0lGODlhEAAQAPIAAP///wAAAMLCwkJCQgAAAGJiYoKCgpKSkiH/C05FVFNDQVBFMi4wAwEAAAAh/hpDcmVhdGVkIHdpdGggYWpheGxvYWQuaW5mbwAh+QQJCgAAACwAAAAAEAAQAAADMwi63P4wyklrE2MIOggZnAdOmGYJRbExwroUmcG2LmDEwnHQLVsYOd2mBzkYDAdKa+dIAAAh+QQJCgAAACwAAAAAEAAQAAADNAi63P5OjCEgG4QMu7DmikRxQlFUYDEZIGBMRVsaqHwctXXf7WEYB4Ag1xjihkMZsiUkKhIAIfkECQoAAAAsAAAAABAAEAAAAzYIujIjK8pByJDMlFYvBoVjHA70GU7xSUJhmKtwHPAKzLO9HMaoKwJZ7Rf8AYPDDzKpZBqfvwQAIfkECQoAAAAsAAAAABAAEAAAAzMIumIlK8oyhpHsnFZfhYumCYUhDAQxRIdhHBGqRoKw0R8DYlJd8z0fMDgsGo/IpHI5TAAAIfkECQoAAAAsAAAAABAAEAAAAzIIunInK0rnZBTwGPNMgQwmdsNgXGJUlIWEuR5oWUIpz8pAEAMe6TwfwyYsGo/IpFKSAAAh+QQJCgAAACwAAAAAEAAQAAADMwi6IMKQORfjdOe82p4wGccc4CEuQradylesojEMBgsUc2G7sDX3lQGBMLAJibufbSlKAAAh+QQJCgAAACwAAAAAEAAQAAADMgi63P7wCRHZnFVdmgHu2nFwlWCI3WGc3TSWhUFGxTAUkGCbtgENBMJAEJsxgMLWzpEAACH5BAkKAAAALAAAAAAQABAAAAMyCLrc/jDKSatlQtScKdceCAjDII7HcQ4EMTCpyrCuUBjCYRgHVtqlAiB1YhiCnlsRkAAAOwAAAAAAAAAAAA==') !important;
background-repeat: no-repeat !important;
background-position: center 100px !important;
height: 100%;
}
</style>
<?php $iframe_uid = mt_rand( 10000000, 99999999 ); ?>
<div id="rafflepress-giveaway-iframe-wrapper-<?php echo $iframe_uid; ?>" class="rafflepress-giveaway-iframe-wrapper rafflepress_iframe_loading">
<?php
// Iframe is inserted with insertIframe() on front end to avoid 3rd-party scripts from lazy-loading.
// However, echoing the iframe is needed to render preview in the blocks editor
$is_gb_editor = defined( 'REST_REQUEST' ) && REST_REQUEST && ! empty( $_REQUEST['context'] ) && 'edit' === $_REQUEST['context'];
if ( $is_gb_editor ) {
$iframe = '<iframe id="rafflepress-' . $iframe_uid . '" ' .
'src="' . home_url() . '/rafflepress/' . $id . '?iframe=1&giframe=' . $a['giframe'] .
'&rpr=' . $ref . '&parent_url=' . urlencode( $parent_url ) . '&' . mt_rand( 1, 99999 ) . '" ' .
'frameborder="0" scrolling="no" allowtransparency="true" ' . $style . ' ' .
// 'onload="rafflepress_resize_iframe_' . $iframe_uid . '(this)"' . // causes error & unnecessary due to overlay
'></iframe>';
echo $iframe;
}
?>
</div>
<script>
function insertIframe( ID, src, minHeight) {
var wrapperID = 'rafflepress-giveaway-iframe-wrapper-'+ID;
var iframe = document.createElement('iframe');
iframe.setAttribute('id', 'rafflepress-'+ID);
iframe.setAttribute('class', 'rafflepress-iframe');
iframe.setAttribute('src', src);
iframe.setAttribute('frameborder', '0');
iframe.setAttribute('scrolling', 'no');
iframe.setAttribute('allowtransparency', 'true');
if (minHeight) {
iframe.setAttribute('style', 'min-height:'+minHeight);
}
iframe.setAttribute('onload', 'rafflepress_resize_iframe_'+ID+'(this)');
document.getElementById(wrapperID).appendChild(iframe);
}
// phpcs:disable
insertIframe( '<?php echo $iframe_uid; ?>','<?php echo trailingslashit( home_url() ) . '?rafflepress_page=rafflepress_render&rafflepress_id=' . urlencode($id) . '&iframe=1&giframe=' . urlencode($a['giframe']) . '&rpr=' . urlencode($ref) . '&parent_url=' . urlencode( $parent_url ); ?>&<?php echo mt_rand( 1, 99999 ); ?>','<?php echo esc_html( $a['min_height'] ); ?>' );
// phpcs:enable
</script>
<script>
function rafflepress_resize_iframe_<?php echo $iframe_uid; ?>(){
iFrameResize({
log: false,
onMessage: function(messageData) {
if (messageData.message == 'rafflepress_loaded') {
var el = document.getElementById('rafflepress-giveaway-iframe-wrapper-<?php echo $iframe_uid; ?>');
var className = "rafflepress_iframe_loading";
if (el.classList)
el.classList.remove(className);
else
el.className = el.className.replace(new RegExp('(^|\\b)' + className.split(' ').join('|') +
'(\\b|$)', 'gi'), ' ');
}
}
}, '#rafflepress-<?php echo $iframe_uid; ?>');
};
</script>
<?php
$output = ob_get_clean();
if ( empty( $active ) ) {
return '';
} else {
return $output;
}
}
Code file location:
rafflepress/rafflepress/app/rafflepress.php
Conclusion
Now that you’ve learned how to embed the Rafflepress 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.
Leave a Reply