// Shortcode: [vrs_embed_route id="123"] add_shortcode( 'vrs_embed_route', function( $atts ) { $atts = shortcode_atts( [ 'id' => 0, ], $atts, 'vrs_embed_route' ); $route_id = intval( $atts['id'] ); if ( ! $route_id ) { return '
Invalid route ID.
'; } $route = get_post( $route_id ); if ( ! $route || $route->post_type !== 'vrs_route' ) { return 'Route not found.
'; } // route basic data $title = esc_html( $route->post_title ); $start = get_post_meta( $route_id, '_vrs_route_start_time', true ); $end = get_post_meta( $route_id, '_vrs_route_end_time', true ); // does user have this challenge? if ( ! is_user_logged_in() ) { $login_url = esc_url( wp_login_url( get_permalink() ) ); $html = ' '; return $html; } $user_id = get_current_user_id(); $user_chals = get_user_meta( $user_id, '_vrs_user_challenges', true ) ?: []; // detect if route requires purchase (same approach used elsewhere) $linked = get_posts( [ 'post_type' => 'product', 'posts_per_page' => 1, 'fields' => 'ids', 'meta_query' => [ [ 'key' => '_vrs_linked_route', 'value' => $route_id, 'compare' => '=', ] ], ] ); $requires_purchase = ! empty( $linked ); $purchase_url = $requires_purchase ? get_permalink( $linked[0] ) : ''; $joined = in_array( $route_id, $user_chals, true ); // Build HTML $html = ' '; // Enqueue assets only when the shortcode is used add_action( 'wp_footer', function() use ( $route_id ) { // ensure script added once static $done = false; if ( $done ) return; $done = true; // Register and enqueue script file (assumes you place it in assets/js/) wp_register_script( 'vrs-embed-route-js', plugins_url( 'assets/js/vrs-embed-route.js', __FILE__ ), [ 'jquery' ], '1.0', true ); wp_localize_script( 'vrs-embed-route-js', 'vrsEmbedData', [ 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'vrs_challenge_nonce' ), ] ); wp_enqueue_script( 'vrs-embed-route-js' ); // Minimal inline CSS for the embed (you can move to your plugin CSS) echo ""; } ); return $html; } );