Created
November 10, 2014 13:06
-
-
Save gabrielmerovingi/5de26a0ca86b60bdbefd to your computer and use it in GitHub Desktop.
Show users myCRED balance with WP Super Cache. Based of the example given in the dynamic-cache-test.php given by the plugin. The example uses %mycredbalance% to indicate where the balance should be shown. If using the myCRED Balance widget, set this tag in the balance format template.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define( 'DYNAMIC_OUTPUT_BUFFER_TAG', '%mycredbalance%' ); // Change this to a secret placeholder tag | |
if ( DYNAMIC_OUTPUT_BUFFER_TAG != '' ) { | |
function dynamic_output_buffer_test( &$cachedata = 0 ) { | |
if ( defined( 'DYNAMIC_OB_TEXT' ) ) | |
return str_replace( DYNAMIC_OUTPUT_BUFFER_TAG, DYNAMIC_OB_TEXT, $cachedata ); | |
if ( is_user_logged_in() ) { | |
ob_start(); | |
if ( function_exists( 'mycred' ) ) { | |
$mycred = mycred(); | |
echo $mycred->format_creds( $mycred->get_users_balance( get_current_user_id() ) ); | |
} | |
else { | |
$balance = get_user_meta( get_current_user_id(), 'mycred_default', true ); | |
if ( $balance == '' ) | |
$balance = 0; | |
echo $balance; | |
} | |
$text = ob_get_contents(); | |
ob_end_clean(); | |
} | |
else { | |
$text = ''; | |
} | |
if ( $cachedata === 0 ) { // called directly from the theme so store the output | |
define( 'DYNAMIC_OB_TEXT', $text ); | |
} else // called via the wpsc_cachedata filter. We only get here in cached pages in wp-cache-phase1.php | |
return str_replace( DYNAMIC_OUTPUT_BUFFER_TAG, $text, $cachedata ); | |
} | |
add_cacheaction( 'wpsc_cachedata', 'dynamic_output_buffer_test' ); | |
function dynamic_output_buffer_init() { | |
add_action( 'wp_footer', 'dynamic_output_buffer_test' ); | |
} | |
add_cacheaction( 'add_cacheaction', 'dynamic_output_buffer_init' ); | |
function dynamic_output_buffer_test_safety( $safety ) { | |
if ( defined( 'DYNAMIC_OB_TEXT' ) ) // this is set when you call dynamic_output_buffer_test() from the theme | |
return 1; // ready to replace tag with dynamic content. | |
else | |
return 0; // tag cannot be replaced. | |
} | |
add_cacheaction( 'wpsc_cachedata_safety', 'dynamic_output_buffer_test_safety' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment