Ask your WordPress questions! Pay money and get answers fast! (more info)

Define a different shortcode for each product? WordPress

  • SOLVED

<strong>Short Story</strong>

I'm trying to display a countdown timer in my theme. I can generate the shortcodes I need for each countdown via a plugin I'm using, but I'm trying to place those shortcodes into my theme in a way that allows each new product to have it's own unique countdown.

So I know that in my theme I can place something like this.

<?php echo do_shortcode("[COUNTDOWN-SHORTCODE]"); ?>

I just need a way for each product I post to define it's own shortcode in the COUNTDOWN-SHORTCODE placeholder above.

I would guess that I can use a custom field to define the unique shortcode for each product, but I don't really understand how to make that happen.

<strong>Extended Example if Required</strong>

To explain it in another way. I can place this code into my .php page and it displays perfectly where I want it:

<?php echo do_shortcode('[jcountdown timetext="2015/9/20 2:46:47" timezone="1" style="slide" color="white" width="0" textgroupspace="19" textspace="0" reflection="false" reflectionopacity="0" reflectionblur="0" daytextnumber="2" displayday="true" displayhour="true" displayminute="true" displaysecond="true" displaylabel="true" onfinishredirecturl=""]2015/9/20 2:46:47[/jcountdown]'); ?>

So I need a way to be able to define the longer shortcode text via the WordPress backend rather than in the PHP file itself.

I hope this makes sense, thanks!

Answers (2)

2015-09-18

dimadin answers:

If I understood you task, you need to have a field on product edit screen where you can set value like

[jcountdown timetext="2015/9/20 2:46:47" timezone="1" style="slide" color="white" width="0" textgroupspace="19" textspace="0" reflection="false" reflectionopacity="0" reflectionblur="0" daytextnumber="2" displayday="true" displayhour="true" displayminute="true" displaysecond="true" displaylabel="true" onfinishredirecturl=""]2015/9/20 2:46:47[/jcountdown]

and then have following code in your template that should display this shortcode

<?php echo do_shortcode("[countdown-shortcode]"); ?>


Here is a code that should do that, place it in your functions.php or file with your custom code:


/**
* Register post meta boxes.
*/
function md_countdown_shortcode_meta_box_post_add_boxes() {
// Register for all public types
foreach ( get_post_types() as $post_type ) {
add_meta_box( 'md-countdown-shortcode', __( 'Countdown Shortcode', 'text_domain' ), 'md_countdown_shortcode_meta_box_post_display', $post_type, 'normal', 'high' );
}
}
add_action( 'add_meta_boxes', 'md_countdown_shortcode_meta_box_post_add_boxes' );


/**
* Display a meta box that allows users to set countdown shortcode.
*/
function md_countdown_shortcode_meta_box_post_display( $object, $box ) {
// Get current value
$current = get_post_meta( $object->ID, '_md_countdown_shortcode', true );

?>
<input type="hidden" name="md-countdown-shortcode-nonce" value="<?php echo wp_create_nonce( 'md-countdown-shortcode-nonce' ); ?>" />

<div class="form-table">
<p>
<label for="_md_countdown_shortcode"><input type="text" name="_md_countdown_shortcode" id="_md_countdown_shortcode" value="<?php echo esc_attr( $current ); ?>" size="70" /></label>
</p>
</div><!-- .form-table -->
<?php
}

/**
* Saves the post meta box settings as post metadata.
*
* @param int $post_id The ID of the current post being saved.
* @param int $post The post object currently being saved.
*/
function md_countdown_shortcode_meta_box_post_save( $post_id, $post ) {
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}

// Get the post type object.
$post_type = get_post_type_object( $post->post_type );

// Check if the current user has permission to edit the post.
if ( ! current_user_can( $post_type->cap->edit_post, $post_id ) ) {
return $post_id;
}

// Get all keys
$keys = array( 'md-countdown-shortcode' );

// Verify the nonce before proceeding
foreach ( $keys as $key ) {
$nonce = $key . '-nonce';
if ( ! isset( $_POST[ $nonce ] ) || ! wp_verify_nonce( $_POST[ $nonce ], $nonce ) ) {
return $post_id;
}
}

// Convert keys to meta keys
foreach ( $keys as &$key ) {
$key = '_' . str_replace( '-', '_', $key );
}

foreach ( $keys as $meta_key ) {
// Get the new meta value
$new_meta_value = isset( $_POST[ $meta_key ] ) ? $_POST[ $meta_key ] : '';
$new_meta_value = current_user_can( 'unfiltered_html' ) ? $new_meta_value : wp_kses_post( $new_meta_value );

// Get the meta value of the custom field key
$meta_value = get_post_meta( $post_id, $meta_key, true );

// If a new meta value was added and there was no previous value, add it
if ( $new_meta_value && '' == $meta_value ) {
add_post_meta( $post_id, $meta_key, $new_meta_value, true );
}

// If a new meta value does not match the old value, update it
elseif ( $new_meta_value && $new_meta_value != $meta_value ) {
update_post_meta( $post_id, $meta_key, $new_meta_value );
}

// If there is no new meta value but an old value exists, delete it
elseif ( '' == $new_meta_value && $meta_value ) {
delete_post_meta( $post_id, $meta_key, $meta_value );
}
}
}
add_action( 'save_post', 'md_countdown_shortcode_meta_box_post_save', 10, 2 );

function md_countdown_shortcode_register_shortcode( $atts ) {
return do_shortcode( get_post_meta( get_the_ID(), '_md_countdown_shortcode', true ) );
}
add_shortcode( 'countdown-shortcode', 'md_countdown_shortcode_register_shortcode' );


What will this code do? This will add field on admin edit screen of every public post type where you can submit your value like

[jcountdown timetext="2015/9/20 2:46:47" timezone="1" style="slide" color="white" width="0" textgroupspace="19" textspace="0" reflection="false" reflectionopacity="0" reflectionblur="0" daytextnumber="2" displayday="true" displayhour="true" displayminute="true" displaysecond="true" displaylabel="true" onfinishredirecturl=""]2015/9/20 2:46:47[/jcountdown]

Since I don't know what is post type of product, I have used all post type though you can change this.

Then I register shortcode [countdown-shortcode] that would display shortcode of current post in a loop. If this is outside of a loop, you can also change this. Please take care of capitalization, you need to use this in template with small letters

<?php echo do_shortcode("[countdown-shortcode]"); ?>


Let me know if this helps you or if not, what is wrong and how can I change accordingly.


Liam comments:

Wow. That worked perfectly. Thank you! The prize is heading your way. Thanks for the detailed, helpful response too!


Liam comments:

Ah I see, I can only vote - in that case, you have my vote!


dimadin comments:

No problem, glad it helped.

If you have any WP problem in the future, you know how to find me.

2015-09-18

Reigel Gallarde answers:

have you tried pasting this shortcode on your dashboard in each product?

[jcountdown timetext="2015/9/20 2:46:47" timezone="1" style="slide" color="white" width="0" textgroupspace="19" textspace="0" reflection="false" reflectionopacity="0" reflectionblur="0" daytextnumber="2" displayday="true" displayhour="true" displayminute="true" displaysecond="true" displaylabel="true" onfinishredirecturl=""]2015/9/20 2:46:47[/jcountdown]